在 Liquid 中包含带有条件的片段和模板
Including Snippets and Templates with conditions in Liquid
我正在尝试根据产品类型在产品页面上包含一些片段和模板。但是,liquid 似乎不会有条件地生成片段。
我正在努力实现的一个例子:
{% if product.type == 'shoes' %}
{% include 'shoes-template' %}
{% else %}
{% include 'other-template' %}
{% endif %}
如果您有很多产品类型,可以使用一个数组和一个 contains
,而不是使用多个 if
和 else if
。
您还可以通过执行 capture
并查找字符串 "Liquid error".
来验证模板是否存在
{% assign types = "shoes, shirts, pants" | split:", " %}
{% if types contains product.type %}
{% assign snip = product.type | append:"-template" %}
{% else %}
{% assign snip = "other-template" %}
{% endif %}
{% capture snip_content %}{% include snip %}{% endcapture %}
{% unless snip_content contains "Liquid error" %}
{% include snip %}
{% endunless %}
我正在尝试根据产品类型在产品页面上包含一些片段和模板。但是,liquid 似乎不会有条件地生成片段。
我正在努力实现的一个例子:
{% if product.type == 'shoes' %}
{% include 'shoes-template' %}
{% else %}
{% include 'other-template' %}
{% endif %}
如果您有很多产品类型,可以使用一个数组和一个 contains
,而不是使用多个 if
和 else if
。
您还可以通过执行 capture
并查找字符串 "Liquid error".
{% assign types = "shoes, shirts, pants" | split:", " %}
{% if types contains product.type %}
{% assign snip = product.type | append:"-template" %}
{% else %}
{% assign snip = "other-template" %}
{% endif %}
{% capture snip_content %}{% include snip %}{% endcapture %}
{% unless snip_content contains "Liquid error" %}
{% include snip %}
{% endunless %}