【问题标题】:Passing liquid file to Liquid::Template.parse将液体文件传递给 Liquid::Template.parse
【发布时间】:2023-03-12 19:14:01
【问题描述】:

现在我可以做到这一点:

@template = Liquid::Template.parse("hi {{name}}") # Parses and compiles the template
p @template.render('name' => 'tobi')

但是,我如何使用我已调用 template.liquid 的文件调用 Liquid::Template,该文件包含以下内容:

hi {{name}}

【问题讨论】:

    标签: ruby liquid


    【解决方案1】:

    File.read() 将整个文件作为字符串返回,因此您可以编写:

    template = Liquid::Template.parse(
                 File.read("template.liquid")
               )
    

    【讨论】:

    • 我意识到,当我这样做时,我看到的不是hi toby,而是hi tobi\n 为什么会这样?
    • @HommerSmith,某些操作系统会自动在文件末尾添加“\n”。您可以使用 chomp() 删除尾随的“\n”:File.read("template.liquid").chomp。如果没有尾随“\n”,则 chomp 不会删除任何内容。
    • 我很惊讶 Liquid 没有提供直接从 File 解析的方法。
    • @HommerSmith,我在发布答案之前检查了 Liquid 文档,但文档并未表明您可以这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多