将变量传递给 Liquid 模板中的模型实例方法
这个周末我一直在使用 Liquid 模板引擎,我想知道以下是否可行。
假设我在 Blog
模型中有一个 latest_posts
方法,我可以向该方法传递一个整数来获取最新的 N 个帖子。是否可以在液体模板中使用该方法?
例如:
class Blog
has_many :posts
def latest_posts(n)
posts.latest(n) # using a named scope
end
def to_liquid(*args)
{
'all_posts' => posts.all, # allows me to use {% for posts in blog.all_posts %}
'last_post' => post.last, # allows me to use {% assign recent = blog.last_post %}
'latest_posts' => posts.latest_posts(args[0]) # how do I pass variables to this?
}
end
end
在上面的简化示例中,在我的液体模板中,我可以使用 blog.all_posts
和 blog.last_post
,但不知道我会如何做 >blog.latest_posts:10
。
有人能指出我正确的方向吗?
我想到的一个想法是创建一个 Liquid 过滤器并将 Blog 对象和一个整数传递给它。类似的东西:
{% for post in blog | latest_posts(10) %}
- 但还没有尝试过,感觉就像我在黑暗中刺伤一样。希望有经验的 Liquid 用户提供一些帮助。
I've been playing around with the Liquid templating engine this weekend, and I wonder if the following is possible.
Say I have a latest_posts
method in a Blog
model, which I can pass an integer to to get the latest N posts. Is it possible to use that method in a liquid template?
For example:
class Blog
has_many :posts
def latest_posts(n)
posts.latest(n) # using a named scope
end
def to_liquid(*args)
{
'all_posts' => posts.all, # allows me to use {% for posts in blog.all_posts %}
'last_post' => post.last, # allows me to use {% assign recent = blog.last_post %}
'latest_posts' => posts.latest_posts(args[0]) # how do I pass variables to this?
}
end
end
In the simplified example above, in my liquid templates I can use blog.all_posts
and blog.last_post
, but have no idea how I would do anything like blog.latest_posts: 10
.
Can anyone point my in the right direction?
One idea I thought of was to create a Liquid filter and pass both the Blog object and an integer to that. Something like:
{% for post in blog | latest_posts(10) %}
- but haven't tried that yet as feel like I'm stabbing around in the dark a bit. Would appreciate some help from more experienced Liquid users.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里回答我自己的问题,我找到了 液体组页面。
本质上,我需要为最新帖子创建一个 drop - 一个
LatestPostsDrop
- 并使用before_method
方法将变量传递给它。这是完整的解决方案:执行上述操作,允许您使用以下内容迭代任意数量的最新帖子:
这似乎有点 hacky,但它有效:)
Answering my own question here, I found a solution documented in the Liquid groups pages.
Essentially, I needed to create a drop for the latest posts - a
LatestPostsDrop
- and kind of hack passing a variable to it using thebefore_method
method. Here is the complete solution:Doing the above, allows you to iterate through any number of latest posts using something like:
It seems a bit hacky, but it works :)
我认为液体是一个很棒的模板系统。恭喜您调查/使用它。
默认情况下,没有任何模型方法可用于液体模板。这是一件好事。然后您指定哪些方法可用。 (白名单。)
我使用了在邮件列表上发送的模块扩展。完整的扩展如下。它通过向类和模块添加简单的 #liquid_methods 方法来为您处理 Liquid::Drop 创建。
然后,在您的模型中,只需执行以下操作:
我不确定如何/是否可以将参数传递到 drop 中。在 Liquid 邮件列表上询问。我想你可以。
添加:我现在重新阅读您的问题,发现您确实想要将该参数发送到该方法。您可以向 Liquid 过滤器发送多个自变量/参数。因此,您可以有一个过滤器:
在本示例中,还请记住您还需要在 Post 类中声明 Liquid 方法。
这是模块扩展。
I think liquid is a fantastic template system. Congrats on investigating/using it.
By default, none of the model's methods are available to the liquid template. This is a good thing. You then specify which methods shall be available. (A white list.)
I use an extension to Module which was sent on the mailing list. Complete extension is below. It handles the Liquid::Drop creation for you by adding a simple #liquid_methods method to classes and modules.
Then, in your models, just do:
I'm not sure offhand how/if you can pass params into a drop. Ask on the Liquid mailing list. I think you can.
Added: I now re-read your question and see that you really want to send in that param to the method. You can send in more than one argument/parameter to a Liquid filter. So you could have a filter:
In this example, also remember that you'll need to declare liquid methods in the Post class too.
Here is the module extension.