自定义液体块检索上下文变量
Custom liquid block retrieving context variables
如何从 jekyll markdown 中的自定义 Liquid 块中检索分配的变量?
我读过 using assignments in templates,但我显然遗漏了一些简单的东西。
编辑:这只发生在 Jekyll 变量上,而不是在 liquid 中设置的基本变量。
无效:
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
尝试将 {% assign variable = site.variables | where "someval", "true" %}
更改为 {% assign variable = site.variables | where: "someval", "true" %}
。
此外,根据 docs,variable
似乎不是一个选项。如果你想传递更多信息,你需要使用类似 site.data
的东西或在你的 jekyll 初始化器中定义的信息(通常称为 _config.yml
)。
有一些转移注意力的问题被证明是问题所在:
- 我的
variable
其实是一个Drop and is not a normal hash value. Specifically, it's a DocumentDrop which delegates to_s
to the Documentclass。
to_s
的实现打印出 Document
的 output
、content
或 "NO CONTENT"
。
- 在我的例子中,
output
和 content
是 space 或换行符,所以这就是输出的全部内容。这是因为这些文件只存在于它们的前面,所以没有实际内容。
- 首页是通过Drop接口访问的。所以我实际上得到了
variable
,只有一个空字符串表示。
- 事不宜迟,访问前题数据:
context[@variable]["my-front-matter-data"]
好吧,至少我觉得自己现在不那么像 ruby 新手了。
如何从 jekyll markdown 中的自定义 Liquid 块中检索分配的变量?
我读过 using assignments in templates,但我显然遗漏了一些简单的东西。
编辑:这只发生在 Jekyll 变量上,而不是在 liquid 中设置的基本变量。
无效:
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
尝试将 {% assign variable = site.variables | where "someval", "true" %}
更改为 {% assign variable = site.variables | where: "someval", "true" %}
。
此外,根据 docs,variable
似乎不是一个选项。如果你想传递更多信息,你需要使用类似 site.data
的东西或在你的 jekyll 初始化器中定义的信息(通常称为 _config.yml
)。
有一些转移注意力的问题被证明是问题所在:
- 我的
variable
其实是一个Drop and is not a normal hash value. Specifically, it's a DocumentDrop which delegatesto_s
to the Documentclass。 to_s
的实现打印出Document
的output
、content
或"NO CONTENT"
。- 在我的例子中,
output
和content
是 space 或换行符,所以这就是输出的全部内容。这是因为这些文件只存在于它们的前面,所以没有实际内容。 - 首页是通过Drop接口访问的。所以我实际上得到了
variable
,只有一个空字符串表示。 - 事不宜迟,访问前题数据:
context[@variable]["my-front-matter-data"]
好吧,至少我觉得自己现在不那么像 ruby 新手了。