Rob
Rob

Reputation: 1855

Stuck trying to get the liquid gem to work in rails

I'm building an app to learn rails better and I'm trying to implement the liquid gem

Just trying to keep it simple so I can get it working and understand it by just having @guide.name passed through the liquid template but when i put {{ @guide.name }} in the template it doesn't render at all.

category_item Controller

def show
     @guide = Guide.friendly.find(params[:guide_id])
     @category = Category.friendly.find(params[:category_id])
     @category_item = Category.friendly.find(params[:category_id]).category_items.friendly.find params[:id]
end

show.html.erb

<% template = Liquid::Template.parse(@category.template) %>

<%= template.render %>

and in @category.template is

the guide is {{ @guide.name  }}

If I just put hello in the @category.template it renders fine

I think I'm supposed add something in the category_item helper but i'm not sure what to put in there. Or maybe there are other places I'm supposed to add information as well.

Upvotes: 0

Views: 715

Answers (2)

Rajesh Sharma
Rajesh Sharma

Reputation: 419

try this

<% template = Liquid::Template.parse('the guide is {{ guide_name  }}') %>
<%= template.render('guide_name' => @guide.name) %>

basically passing a hash of your variables as a mapping to the ones that you are using in your template.

hope that helps

Upvotes: 1

Asad Ali
Asad Ali

Reputation: 660

Follow liquid gem documentation to better understand basics. In your case Liquid::Template.parse() should be like this:

 <% template = Liquid::Template.parse('the guide is {{ @guide.name  }}') %>
 <%= template.render('@guide' => @guide) %>

rule: 'string {{variable name only}} string '

According to liquid gem documentation example:

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

Upvotes: 2

Related Questions