3
votes

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:

  1. 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?
  2. 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[*]"?

2

2 Answers

8
votes

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
0
votes

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:

{% for extra_tab_hash in page.extra-tabs %}
  {% for extra_tab in extra_tab_hash %}
    {% mytab %} {{extra_tab[0]}}, {{extra_tab[1]}} {% endmytab %}
  {% endfor %}
{% endfor %}