开发者

Using liquid in Rails 3

开发者 https://www.devze.com 2023-02-20 08:45 出处:网络
Im making a Rails blog engine for learning purpose. I want to use liquid as template engine. I开发者_JS百科 have something like this

Im making a Rails blog engine for learning purpose. I want to use liquid as template engine. I开发者_JS百科 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 %}

That gave me the following error:

undefined local variable or method `template' for
#<PostsController:0x103d16290>

I already had LiquidView loaded in initializers/liquid.rb Please let me know what is my problem. Thank you


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

and see.

If not try to make Post class inherited by Liquid::Drop

like

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"

hope this helps

cheers

sameera

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号