【问题标题】:Including Snippets and Templates with conditions in Liquid在 Liquid 中包含具有条件的片段和模板
【发布时间】:2016-09-25 08:16:26
【问题描述】:

我正在尝试根据产品类型在产品页面上包含一些 sn-ps 和模板。但是,liquid 似乎不会有条件地生成 sn-ps。

我正在努力实现的一个例子:

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

【问题讨论】:

  • 有东西。有条件的include 有效。我在很多地方都使用它。你能分享任何其他相关的代码吗? shoes-templateother-template 也是?

标签: shopify liquid liquid-layout


【解决方案1】:

如果您有多种产品类型,您可以使用一个数组和一个contains,而不是使用多个ifelse 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 %}

【讨论】:

  • 这是一个回合。我不确定它是否有效。 ["shoes","shirts"] 也不表示液态代码中的数组。
  • 我修复了数组。如果您有许多产品类型,这比长 if, else if, else if, ... 更有效,只要您与模板文件名一致。
猜你喜欢
  • 1970-01-01
  • 2023-01-21
  • 2014-03-25
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
相关资源
最近更新 更多