具有两个参数的自定义液体标签 [英] Custom Liquid tag with two parameters

查看:70
本文介绍了具有两个参数的自定义液体标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写/调用具有两个参数的自定义液体标签?

How can I write/invoke a custom Liquid Tag with two parameters?

上下文:Jekyll 2.1.1

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 ...

我还有一个限制:呈现a-page.md时,显示的标签应该看起来有所不同(< li class ="active"> ...).尽管此 可以通过上面链接的技术解决,但这很丑陋:

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

我想编写一个自定义标签来替换条件if/else/endif,例如:

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

我那里有两个问题:

  1. mytab仅接收一个输入,通常称为 text ,其中包含{%...%}内的所有内容,而我需要两个单独的值.我可以使用Tag split(',')之类的东西,但是还有其他问题吗?
  2. 它没有被解释:实际上是" extra_tab [0] extra_tab [1] ".
  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]".

所以:我怎样才能使Liquid扩展对"extra_tab [*]"的引用?

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

或针对您的情况:

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

这篇关于具有两个参数的自定义液体标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆