在Rails中使用液体3

时间:2011-03-28 00:50:51

标签: ruby-on-rails templates liquid

我正在制作一个用于学习目的的Rails博客引擎。我想用液体作为模板引擎。我有类似的东西

    ## 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 请让我知道我的问题是什么。 谢谢

1 个答案:

答案 0 :(得分:5)

据我所知,你应该有属性的液体方法(在你的情况下为'标题')。试试这样的事情

class Post < ActiveRecord::Base
  liquid_methods :title
end

看。

如果没有尝试使Liquid :: Drop

继承Post类

喜欢

class Posts < Liquid::Drop

end

** BTW,因为您收到错误声明缺少模板变量,请确保您的液体渲染部分如下

(直接从液体doc复制)

@template = Liquid::Template.parse("hi {{name}}")  # Parses and compiles the template
@template.render( 'name' => 'tobi' )               # Renders the output => "hi tobi"

希望这会有所帮助

欢呼声

sameera