如何在 liquid shopify 中进行插值
How to interpolate in liquid shopify
我正在编辑由 Shopify Liquid 中的另一位开发人员编写的标题标签,不幸的是我对语法感到很困惑。我正在尝试在其中一项编辑中进行 ruby 样式插值。例如,
{% assign title_content = teacher.name %}
{% include "layout/page_title", title: title_content %}
纯ruby,会是这样的
{% assign title_content = "the name of the teacher is #{teacher.name}" %}
这将给出输出 "the name of the teacher is bla bla"。
我想知道是否可以用 liquid shopify 做这样的事情。
您可以使用 capture
标签来构建您的变量,如下所示:
{% capture title_content %}
the name of the teacher is {{ teacher.name }}
{% endcapture %}
或者,您应该能够在分配期间使用 append
过滤器:
{% assign title_content = "the name of the teacher is " | append: teacher.name %}
无论如何,Liquid 应该是一种 "safe" 模板语言。因此,它的表现力不如例如允许您使用任意 Ruby 代码的 ERB。然而,这确保了普通用户可以输入和更新模板,而不会冒在服务器上执行任意代码的风险。
我正在编辑由 Shopify Liquid 中的另一位开发人员编写的标题标签,不幸的是我对语法感到很困惑。我正在尝试在其中一项编辑中进行 ruby 样式插值。例如,
{% assign title_content = teacher.name %}
{% include "layout/page_title", title: title_content %}
纯ruby,会是这样的
{% assign title_content = "the name of the teacher is #{teacher.name}" %}
这将给出输出 "the name of the teacher is bla bla"。 我想知道是否可以用 liquid shopify 做这样的事情。
您可以使用 capture
标签来构建您的变量,如下所示:
{% capture title_content %}
the name of the teacher is {{ teacher.name }}
{% endcapture %}
或者,您应该能够在分配期间使用 append
过滤器:
{% assign title_content = "the name of the teacher is " | append: teacher.name %}
无论如何,Liquid 应该是一种 "safe" 模板语言。因此,它的表现力不如例如允许您使用任意 Ruby 代码的 ERB。然而,这确保了普通用户可以输入和更新模板,而不会冒在服务器上执行任意代码的风险。