Custom Liquid tag with two parameters

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 21:31:24

问题


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


回答1:


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



回答2:


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 %}


来源:https://stackoverflow.com/questions/29480950/custom-liquid-tag-with-two-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!