在 Liquid 标签调用中使用 Liquid 变量
我在 Liquid 中创建了一个自定义链接标签,并且我试图能够将 Liquid 变量传递到对该标签的调用中,如下所示
{{ assign id = 'something' }} // this value is actual dynamic while looping through data
{% link_to article: id, text: 'Click Me!' %} // my custom tag
但是,这会导致文章参数按照分配作为“id”而不是“某物”传递上面的声明。
有谁知道如何将变量传递到标签调用中?
I made a custom link tag in Liquid and I am trying to be able to pass liquid variables into the call for that tag like so
{{ assign id = 'something' }} // this value is actual dynamic while looping through data
{% link_to article: id, text: 'Click Me!' %} // my custom tag
However this results in the article parameter being passed in as 'id' instead of 'something' as per the assign statement above it.
Does anyone know how to pass variables into tag calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我最近使用 Jekyll 0.11.2 和 Liquid 2.3.0 通过将变量的名称作为标签参数传递来非常简单地解决了这个问题。
您还可以在循环中传递控件变量的名称,如上面的
article
。在
Liquid::Tag.initialize
中,@markup
是第二个参数,即标签名称后面的字符串。分配的变量在上下文
的顶层可用。这显然只允许传递一个参数。更复杂的解决方案将解析诸如
x: 2, y: 3
之类的参数。I've recently solved this very simply with Jekyll 0.11.2 and Liquid 2.3.0 by passing the name of the variable as the tag parameter.
You can also pass the name of the control var while in a loop, like
article
above.In
Liquid::Tag.initialize
,@markup
is the second parameter, the string following the tag name. The assigned variables are available in the top level of thecontext
.This obviously only allows one param to be passed. A more complex solution would parse params like
x: 2, y: 3
.这解决了我的问题
context[@markup.strip]
。我的问题是我希望能够将变量传递给我的自定义 Liquid 标签,如下所示:
{% get_menu main_menu navigation.html settings.theme.id %}
为了做到这一点,我首先拆分将变量字符串转换为每个空格字符上的不同变量。
*这只是一个比 Jonathan Julian 上面的答案更丰富的例子
This solved the case for me
context[@markup.strip]
.My problem was that i wanted to be able to pass a variable to my custom Liquid tag like this:
{% get_menu main_menu navigation.html settings.theme.id %}
In order to do this i first split the variable string into different varaibles on every space character.
*This is just a more rich example that the answer above by Jonathan Julian
看起来这不可能,我的解决方案是将变量名称传递给标签,然后从渲染标签的上下文中获取它。就像这样:
在我的标签代码(压缩)中:
Doesn't look like this is possible, my solution was to just pass the variable name in to the tag and grab it out of the context the tag is being rendered in. Like so:
in my tag code (condensed):
最好有一个可以用文字和变量(如
or
或 )
调用的标签,当然也可以,
为此,我提出了一个辅助函数
在渲染中,只需调用辅助函数即可获取文字或变量的值。
仅供参考,初始化程序如下所示:
It would be great to have a tag that can be called with literals and variables like
or
or
and also of course
In order to so I propose a helper function
And in the render just call the helper function to get the value of the literal or variable.
FYI, the initialiser would look like this:
这并没有严格回答这个问题,但它可能会帮助其他刚接触 Liquid 的人(比如我自己)并尝试这样的事情。不要实现自定义标签,而是考虑实现自定义过滤器代替。变量在传递到过滤器之前会被解析。
Ruby 代码:
Liquid 模板:
输出:
您还可以使用变量作为参数。因此,以下内容将具有相同的输出:
并且过滤器可以有零个或多个(逗号分隔)参数:
This does not strictly answer the question, but it may help others who are new to Liquid (like myself) and try something like this. Instead of implementing a custom tag, consider implementing a custom filter instead. Variables are resolved before they are passed into filters.
Ruby code:
Liquid template:
Output:
You can also use variables as parameters. So the following would have the same output:
And filters can have zero or more (comma-separated) parameters:
您可以像 Liquid 本身一样解析变量,例如查看
assign
中的表达式:链接。这是一个示例自定义标记,它将整个标记计算为表达式:
您可能需要使用正则表达式从复杂标记中提取表达式。
这是一个执行此操作的简单函数。请注意,由于它使用正则表达式,因此它无法处理需要上下文无关语法的更复杂的表达式,例如包含多个嵌套括号的表达式。
这是一个使用它的简单标签:
You can just parse the variable like Liquid itself does, see for instance how the expression in
assign
: link.This is a sample custom tag that evaluates the entire markup as an expression:
You may want to use regular expressions to carve out expressions from your complex markup.
Here's a simple function that does that. Note that, since it uses regular expressions, it can't handle more complex expressions that need a context-free grammar, such as those containing multiple nested parentheses.
Here's a simple tag that makes use of it: