创建自定义 Jekyll 标签时,“tokens”参数用于什么?

When creating a custom Jekyll tag, what is the `tokens` arg used for?

Jekyll 文档说我们可以 create a custom tag:

module Jekyll
  class TestTag < Liquid::Tag
    def initialize(tag_name, text, tokens)
      super
      @text = text
      @tokens = tokens
    end
    def render(context)
      "text: #{@text} tokens: #{@tokens}"
    end
  end
end

Liquid::Template.register_tag('test', Jekyll::TestTag)

似乎 initialize 是一个内置函数,尽管文档没有明确说明。

当我在页面中包含此标记时:

{% test hallo world %}

我得到:

text: hallo world tokens: {:locale=>#<Liquid::I18n:0x007fd62dbd5e38 
@path=”/Library/Ruby/Gems/2.0.0/gems/liquid-3.0.6/lib/liquid/locales/en.yml”>,
:line_numbers=>true}

这些代币是从哪里来的?他们在做什么?我可以自己设置令牌吗?

当使用 super 定义方法时,keyword 告诉解析器沿着查找路径查找同名方法。

Liquid::Tag 有一个初始化方法,这就是这些标记最有可能来自的地方。

这些代币从哪里来?

您正在使用 super 关键字,这意味着它调用其父 class 的 initialize 方法,在本例中 Liquid::Tag,它是 [= class 的 25=]constructor 并创建 Tag 的新实例。

他们是做什么的?

tokens argument:

is a hash that stores Liquid options. By default it has two keys: :locale and :line_numbers, the first is a Liquid::I18n object, and the second, a boolean parameter that determines if error messages should display the line number the error occurred. This argument is used mostly to display localized error messages on Liquid built-in Tags and Filters.