奇怪的Shopify Liquid Forloop行为 - 页面在使用标签Forloop内的Forloop时弄糟了

问题描述:

我为我的客户创建了一个系统,能够在产品页面上显示产品详细信息/规格表,供应主题的分组过滤器标签,例如。具有标签“Brand_Philips”的产品将自动在表格中添加一行,第一列为“Brand”,第二列为“Philips”。奇怪的Shopify Liquid Forloop行为 - 页面在使用标签Forloop内的Forloop时弄糟了

我在主题settings_schema.json中添加了一个输入,所以我的客户端应该能够添加/删除和重新排列细节,现在我要做的就是循环通过新设置并检查是否存在匹配标记并将其添加到表中,但由于某种原因,当我循环标记循环内的所有细节时,一切正常,当我在细节内循环标记时,整个页面会变得混乱。

这里是我的代码:

在settings_schema.json:

{ 
    "name": "Sort Product Details", 
    "settings": [ 
    { 
     "type": "text", 
     "label": "Type the product detail tags in a comma-separated list.", 
     "id": "product_details", 
     "info": "List items must be identical to the tag prefixes (no underscore), and have no spaces between commas.ie. Brand,Avarage Lifetime,Watts,Volts" 
    } 
    ] 
} 

在产品页面上,我写这个代码:

{% assign marker = '_' %} 
{% assign productDetails = settings.product_details | split: ',' %} 
{% assign found = false %} 
{% for tag in product.tags %}        <!-- The tags loop --> 
    {% for detail in productDetails %}      <!-- The details loop --> 
    {% if tag contains marker and tag contains detail %} 
     {% if found == false %} 
     {% assign found = true %} 
     <hr> 
     <h3>Product Details:</h3> 
     <table class="table-striped"> 
     {% endif %} 
     {{ tag | replace: marker, ': </strong></td><td>' | prepend: '<tr><td><strong>' | append: '</td></tr>' }} 
     {% if found and forloop.last %}</table>{% endif %} 
    {% endif %} 
    {% endfor %} 
{% endfor %} 

注意标签循环中循环我细节,但当我循环详细信息循环内的标签时,我的页面全部搞砸了。

这里是我的页面的外观正常:

enter image description here

这是我的页面的外观,当我循环的细节循环内的标签:

enter image description here 我之所以希望它环路细节循环中的标签是我希望我的客户端能够重新排列细节 - 而且它不应该按照字母顺序显示 - 标签循环的工作方式。

提前致谢!

Martin。

问这个问题上Shopify forums后,我得到了一个答案,想在这里分享。

它之所以会搞砸的原因是仅在循环结束时添加了闭合</table>标记,代码为{% if found and forloop.last %}</table>{% endif %},并且仅渲染包含在细节中的标记时,它永远不会到达最后一个标记。

因此,这里是我的固定码:

{% assign marker = '_' %} 
{% assign productDetails = settings.product_details | split: ',' %} 
{% assign found = false %} 
{% for detail in productDetails %} 
    {% for tag in product.tags %} 
    {% if tag contains marker and tag contains detail %} 
     {% if found == false %} 
     {% assign found = true %} 
     <hr> 
     <h3>Product Details:</h3> 
     <table class="table-striped"> 
     {% endif %} 
     {{ tag | replace: marker, ': </strong></td><td>' | prepend: '<tr><td><strong>' | append: '</td></tr>' }} 
    {% endif %} 
    {% endfor %} 
    {% if found and forloop.last %}</table>{% endif %} 
{% endfor %} 

注意标签循环是在细节上环和闭合</table>标签在细节循环的末尾。