2016-09-25 51 views
1

我试图根据产品类型在产品页面上包含一些片段和模板。然而,液体似乎不会有条件地生成片段。在液体中包含条件和模板条件

的我想要实现的一个例子:

{% if product.type == 'shoes' %} 
    {% include 'shoes-template' %} 
{% else %} 
    {% include 'other-template' %} 
{% endif %} 
+0

有什么。有条件的'包括'工程。我在很多地方使用它。你能分享任何其他相关的代码吗? 'shoes-template'和'other-template'也是? – HymnZ

回答

2

如果你有许多产品类型,而不是使用多个ifelse 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 %} 
+0

这是一个关于方式的回合。我不确定它是否有效。同样''“鞋子”,“衬衫”]不代表液体代码中的数组。 – HymnZ

+1

我修复了阵列。如果您有多种产品类型,只要您与模板文件名保持一致,这比长时间使用'if,else if,else if,...'效率更高。 – jrbedard