How can I write/invoke a custom Liquid Tag with two parameters?
Context: Jekyll 2.1.1
I have some pages that are presented as a tab set. My page front matter is allowed to define extra tabs for some pages, like this:
---
blah: blah
extra-tabs:
- A page: a-page.md
- Another page: other-page.md
---
I can solve this thanks to Iterate over hashes in Liquid Templates. But ...
I have an additional constraint: when rendering a-page.md, the tab displayed should look different (<li class="active">...). While this can be solved by the techniques linked above, it's ugly:
{% for extra_tab_hash in page.extra-tabs %}
{% for extra_tab in extra_tab_hash %}
{% if page.name == extra_tab[1] %}
<li class="active"><a href="#">{{extra_tab[0]}}</a></li>
{% else %}
<li><a href="{{ extra_tab[1] | in2out }}">{{extra_tab[0]}}</a></li>
{% endif %}
{% endfor %}
{% endfor %}
I would like to write a custom tag that replaces the conditional if/else/endif, something like:
{% for extra_tab_hash in page.extra-tabs %}
{% for extra_tab in extra_tab_hash %}
{% mytab extra_tab[0] extra_tab[1] %}
{% endfor %}
{% endfor %}
I have two problems there:
- mytab is receiving only one input, conventionally called text, containing all the stuff inside the {% ... %}, whereas I need two separate values. I could have the Tag split(',') or something, but there's this other problem?
- it's not interpreted: it's literally "extra_tab[0] extra_tab[1]".
So: How can I induce Liquid to expand the references to "extra_tab[*]"?
Well, I guess I can make mytag be a block tag. This involves the clutter of the endmytag tag closer, but that's not sooooo bad:
I had a similar problem. I wanted to be able to do the following:
or for your case:
It is possible to do liquid expansion in your own tags input. For that you create a template out of the tags content and render it using your current context:
When you use this at the beginning of the render function of your custom tag you have a tag with liquid expansion: