在Rails 3中使用液体 [英] Using liquid in Rails 3
问题描述
我出于学习目的制作了Rails博客引擎.我想使用液体作为模板引擎.我有这样的东西
Im making a Rails blog engine for learning purpose. I want to use liquid as template engine. I have something like this
## posts_controller.rb
...
def index
@posts = Post.all
end
...
## posts/index.html.liquid
{% for post in posts do %}
{{ post.title }}
{% endfor %}
那给了我以下错误:
undefined local variable or method `template' for
#<PostsController:0x103d16290>
我已经在Initializers/liquid.rb中加载了LiquidView 请让我知道我的问题是什么. 谢谢
I already had LiquidView loaded in initializers/liquid.rb Please let me know what is my problem. Thank you
推荐答案
据我所知,您应该具有用于属性的液体方法(在您的情况下为"title").尝试这样的事情
As I know you should have liquid methods for attributes (in your case for 'title'). try something like this
class Post < ActiveRecord::Base
liquid_methods :title
end
然后看看.
如果不尝试使Liquid :: Drop继承Post类
If not try to make Post class inherited by Liquid::Drop
喜欢
class Posts < Liquid::Drop
end
**顺便说一句,因为您在声明缺少模板变量时遇到错误,请确保您的液体渲染部分如下
** BTW since you get an error claiming missing template variable make sure your liquid rendering part is as follows
(直接从液态文档复制)
(directly copied from liquid doc)
@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
@template.render( 'name' => 'tobi' ) # Renders the output => "hi tobi"
希望这会有所帮助
欢呼
萨摩耶拉
这篇关于在Rails 3中使用液体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!