Keith Johnson
Keith Johnson

Reputation: 730

Nested Liquid Templates in Sinatra

According to Sinatra docs you pass liquid objects into a liquid template like so

liquid :index, :locals => { :title => "My Sinatra App"}

for rendering in a template like

{{title}}

This seems to break with nested objects though, for instance

liquid :index, :locals => { :foo => { :bar => "baz" }}

Doesn't let me refer to the value of bar in the liquid template like

{{foo.bar}}

Is there some specific way to build nested liquid objects for passing into a view? Thanks!

Upvotes: 2

Views: 324

Answers (1)

dossy
dossy

Reputation: 1679

I was dealing with this same issue, and I discovered that if you use symbols to define subkeys in the locals hash, you don't get the behavior you might expect. In other words:

liquid :index, :locals => { :foo => { :bar => "baz" }}

will not make {{ foo.bar }} do what you expect. What you want is:

liquid :index, :locals => { :foo => { "bar" => "baz" }}

This will make {{ foo.bar }} substitute with the value baz as you expect.

Given this behavior, this may be a useful/relevant follow-on SO post:

How to change hash keys from `Symbol`s to `String`s?

Upvotes: 3

Related Questions