我试图根据产品类型在产品页面上包含一些片段和模板。然而,液体似乎不会有条件地生成片段。在液体中包含条件和模板条件
的我想要实现的一个例子:
{% if product.type == 'shoes' %}
{% include 'shoes-template' %}
{% else %}
{% include 'other-template' %}
{% endif %}
我试图根据产品类型在产品页面上包含一些片段和模板。然而,液体似乎不会有条件地生成片段。在液体中包含条件和模板条件
的我想要实现的一个例子:
{% if product.type == 'shoes' %}
{% include 'shoes-template' %}
{% else %}
{% include 'other-template' %}
{% endif %}
如果你有许多产品类型,而不是使用多个if
和else if
,你可以使用数组和contains
。 您也可以通过执行capture
并查找字符串“液体错误”来验证模板是否存在。
{% 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 %}
有什么。有条件的'包括'工程。我在很多地方使用它。你能分享任何其他相关的代码吗? 'shoes-template'和'other-template'也是? – HymnZ