因此,目前 Shopify 的部分实现还有很多不足之处。大多数功能都集中在主页上。
我试图在一定程度上绕过这个问题,但基本上将所有部分功能(通常会分为多个部分)放入一个部分文件中,然后为每个部分复制它商店中的产品,重用每个产品的句柄作为部分名称。
例如: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 %}
关于include - Shopify Liquid 有条件地包含部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165991/