【问题标题】:List all nodes in a Liquid Template?列出 Liquid 模板中的所有节点?
【发布时间】:2012-08-13 06:05:36
【问题描述】:

考虑以下已解析的模板及其 root.nodelist 的循环:

text = '{% if true %}{{ "poland" | capitalize }}{% else %}{{ "portugal" | capitalize}}{% endif %}'
template = Liquid::Template.parse(text)
template.root.nodelist.each { |node| p node }

只打印:

Portugal 

(而不是波兰)

如何从模板中列出所有节点(包括 Liquid 变量、标签等​​)?尤其是 if/else 逻辑中的那些?

【问题讨论】:

    标签: ruby liquid


    【解决方案1】:

    我并没有真正找到在某个条件下访问 Blocks 的直接方法,但我使用了一种变通方法,通过以下代码向 Class 添加了一个方法

    def b
      @blocks
    end
    

    然后使用这段代码

    template.root.nodelist[0].b.each { |a| p a.attachment[0].name }
    

    我可以找到您要查找的列表

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2019-09-03
      相关资源
      最近更新 更多