Hak*_*aba 2 ruby interpolation ruby-on-rails liquid shopify
我正在编辑由 Shopify Liquid 中的另一位开发人员编写的标题标签,不幸的是我对语法感到很困惑。我正在尝试在其中一次编辑中进行 ruby 风格的插值。例如,
{% assign title_content = teacher.name %}
{% include "layout/page_title", title: title_content %}
Run Code Online (Sandbox Code Playgroud)
在纯红宝石中,它会是这样的
{% assign title_content = "the name of the teacher is #{teacher.name}" %}
Run Code Online (Sandbox Code Playgroud)
这将给出输出“老师的名字是bla bla”。我想知道是否可以使用 Liquid shopify 做这样的事情。
您可以使用该capture
标签来构建您的变量,如下所示:
{% capture title_content %}
the name of the teacher is {{ teacher.name }}
{% endcapture %}
Run Code Online (Sandbox Code Playgroud)
或者,您应该能够append
在分配期间使用过滤器:
{% assign title_content = "the name of the teacher is " | append: teacher.name %}
Run Code Online (Sandbox Code Playgroud)
无论如何,Liquid 应该是一种“安全”的模板语言。因此,它将不如 ERB 那样具有表现力,它允许您使用任意 Ruby 代码。但是,这可确保普通用户可以输入和更新模板,而不会冒在服务器上执行任意代码的风险。