{ :KeyD => "foo", :KeyE => "bar" } } } ...">

Ruby Liquid templating nested hashes

I have a nested hash:

{
  :KeyA => {
            :KeyB => "hello",
            :KeyC => {
                      :KeyD => "foo",
                      :KeyE => "bar"
                    }
          }
}

If I pass this ERB, I can do:

<%= config[:KeyA][:KeyC][:KeyD] %>

and get:

foo

However, this does not work with Liquid :(

If I do this:

Liquid::Template.parse(template).render(Hash["config" => myhash ])

I can do:

{{ config }}

and I get pseudo-json on top, but

{{ config[:KeyA] }}

gives nothing: (

Does Liquid support this? If not, what alternatives exist for ERBs that support this, and it is preferable to allow me to configure replacement tags (I cannot use ERB because I run it in an ASP file that contains markup <%)

+3
source share
4 answers

used erubis at the end

0
source

Use dots

eg:.

Liquid::Template.parse("{{ a[0].b[2].c  }}").render('a' => [{'b'=>[1,2,{'c'=>33}]}])

Conclusion:

 => "33"
+1

I had the same problem (importing the yaml object into a hash) and it was impossible to work. Finally, I decided to use erubs. There is life beyond the Liquid, as well as another enngines pattern. You can read a great post at http://www.hokstad.com/mini-reviews-of-19-ruby-template-engines.html

0
source

use deep_stringify_keys!

The liquid seems to have problems with characters as keys for nested objects.

0
source

Source: https://habr.com/ru/post/1764715/


All Articles