2017-08-09 34 views
1

我爲我的客戶創建了一個系統,能夠在產品頁面上顯示產品詳細信息/規格表,供應主題的分組過濾器標籤,例如。具有標籤「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。

回答

2

問這個問題上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>標籤在細節循環的末尾。