【发布时间】:2018-07-13 18:33:40
【问题描述】:
如何在 jekyll markdown 的自定义 Liquid 块中检索分配的变量?
我读过using assignments in templates,但我显然缺少一些简单的东西。
编辑:这仅发生在 Jekyll 变量中,而不是在液体中设置的基本变量。
不起作用:
markdown中的液体块:
{% assign variable = site.variables | where "someval", "true" %}
{% customblock variable %}
{% endcustomblock %}
Jekyll 插件:
module Jekyll
module Tags
class CustomBlockTag < Liquid::Block
def initialize(tag_name, variable_name, options)
super
@variable = variable_name
end
def render(context)
puts context.scopes
puts context.key?(@variable)
puts context.find_variable(@variable)
puts context[@variable]
end
end
end
end
Liquid::Template.register_tag('customblock', Jekyll::Tags::CustomBlockTag)
输出:
{..., "variable"=> {<another map>} }
true
<blank>
<blank>
我不经常使用 ruby,但据我在source 中看到的,它只是包装了字典查找。我在这里错过了什么?
以下工作正常
markdown中的液体块:
{% assign variable = "1" %}
{% customblock variable %}
{% endcustomblock %}
Jekyll 插件:
module Jekyll
module Tags
class CustomBlockTag < Liquid::Block
def initialize(tag_name, variable_name, options)
super
@variable = variable_name
end
def render(context)
puts context.scopes
puts context.key?(@variable)
puts context.find_variable(@variable)
puts context[@variable]
end
end
end
end
Liquid::Template.register_tag('customblock', Jekyll::Tags::CustomBlockTag)
输出:
{..., "variable"=>"1"}
true
1
1
【问题讨论】: