我有一个嵌套哈希:
{
:KeyA => {
:KeyB => "hello",
:KeyC => {
:KeyD => "foo",
:KeyE => "bar"
}
}
}
如果我将其传递给 ERB,我可以:
<%= config[:KeyA][:KeyC][:KeyD] %>
并得到:
foo
但是这不适用于 Liquid :(
如果我这样做:
Liquid::Template.parse(template).render(Hash["config" => myhash ])
我能做到:
{{ config }}
我从上面打印出伪 json,但是
{{ config[:KeyA] }}
没有结果:(
Liquid 支持吗?如果不是,那么 ERB 有哪些替代方案支持这一点,最好允许我自定义替换标签(我不能使用 ERB,因为我在包含 <%
标记的 ASP 文件上运行它)
最佳答案
使用点,
例如:
Liquid::Template.parse("{{ a[0].b[2].c }}").render('a' => [{'b'=>[1,2,{'c'=>33}]}])
输出:
=> "33"
关于Ruby Liquid 模板嵌套哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3710894/