Shopify Liquid 有条件地包含部分

Shopify Liquid conditionally include sections

因此,当前的 Shopify 版块实施还有很多不足之处。大部分功能都归属于主页。

我试图在一定程度上绕过它,但基本上将所有部分功能(通常会分成多个部分)放入 one 部分文件中,并且然后为商店中的每个产品复制它,重新使用每个产品的句柄作为部分名称。

例如:example-product-handle --> sections/example-product-handle.liquid

然后我的想法是在主 product.liquid 文件中创建一个简单的路由系统,如果存在与句柄匹配的部分,该系统将有条件地包含一个部分。这 SO answer 让我的创意源源不断。

理想的结果应该是……

{% assign current_page = product.handle %}
{% capture snippet_exists %}{% section current_page %}{% endcapture %}
{% unless snippet_exists contains "Liquid error" %}
    {% section current_page %}
{% endunless %}

这对代码片段非常好。将该代码中的 section 替换为 include,路由系统运行完美。

然而有部分?

Liquid syntax error: Error in tag 'section' - Valid syntax: section '[type]'

有没有办法解决这个问题?部分名称是否必须明确说明?

这是不可能的。这是故意不可能的。尝试使用该部分来动态包含片段。

{% for block in section.blocks %}
  {% case block.type %}
  {% when 'layout1' %}
    {% include 'layout1' %}
{% endfor %}