2016-06-01 14 views
0

In Shopify Ich versuche, durch einige Metafields, die Feature-Titel enthalten. Dann muss ich einige andere Metafields durchlaufen und die Feature-Beschreibung basierend auf dem aktuellen Loop-Index abrufen.Shopify/Liquid spezifische Schleife Index innerhalb Schleife

Dieser Code funktioniert gut für mich, aber es ist sehr unelegant und ich bin sicher, es gibt einen viel besseren Weg, um das gleiche Ergebnis zu erzielen!

{% for field in product.metafields.feature_title %} 
    <h4>{{ field | last }}</h4> 
    {% assign i = forloop.index %} 
    {% if forloop.index == 1 %} 
     <p>{{ product.metafields.feature_description.001 }}</p> 
    {% endif %} 
    {% if forloop.index == 2 %} 
     <p>{{ product.metafields.feature_description.002 }}</p> 
    {% endif %} 
    {% if forloop.index == 3 %} 
     <p>{{ product.metafields.feature_description.003 }}</p> 
    {% endif %} 
    {% if forloop.index == 4 %} 
     <p>{{ product.metafields.feature_description.004 }}</p> 
    {% endif %} 
    {% if forloop.index == 5 %} 
    <p>{{ product.metafields.feature_description.005 }}</p> 
    {% endif %} 
{% endfor %} 

Zusätzlich gibt es einen Fehler, der auf 5 begrenzt ist, oder wer auch immer viele if-Anweisungen erstellt.

Cheers,

DB

Antwort

1

nicht getestet, aber so etwas wie dies funktionieren sollte:

{% for field in product.metafields.feature_title %} 
    <h4>{{ field | last }}</h4> 
    {% capture idx %}00{{forloop.index}}{% endcapture %} 
    {% assign key = idx | slice: -3, 3 %} 
    <p>{{ product.metafields.feature_description[key]}}</p> 
{% endfor %} 
Verwandte Themen