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:
So: How can I induce Liquid to expand the references to "extra_tab[*]"?
I had a similar problem. I wanted to be able to do the following:
{% mytag {{ page.var }} {{ captured_var }} %}
or for your case:
{% mytab {{ extra_tab[0] }} {{ extra_tab[1] }} %}
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:
rendered_input = Liquid::Template.parse(@input).render(context)
When you use this at the beginning of the render function of your custom tag you have a tag with liquid expansion:
module Jekyll
class TestTag < Liquid::Tag
def initialize(tag_name, text, token)
super
@input = text
end
def render(context)
rendered_input = Liquid::Template.parse(@input).render(context)
# do fancy stuff with rendered_input
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With