Ruby Liquid 模板嵌套哈希

标签 ruby erb liquid

我有一个嵌套哈希:

{
  :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/

相关文章:

javascript - 什么会导致 Sprockets::CachedEnvironment 错误?

ruby - Ruby 的 Liquid 模板引擎中的模数(或缺少模数)

ruby - 在字符串数组中查找公共(public)字符串( ruby )

ruby - Tux 与 Sinatra 无法加载

ruby - Jekyll 与 .erb 的兼容性

css - 我可以在 css 中使用 liquid 标签让 jekyll 在每页的基础上使用不同的背景图像吗?

json - 为 .liquid 文件添加 JSON 语法突出显示到 IntelliJ IDEA

ruby - 为什么 Nokogiri 不加载整个页面?

mysql - 通过rails session 参数过滤MySQL数据

ruby-on-rails - 在 Rails View 中排序而不显示重复项