在 Liquid (Shopify) 中,如何获取特定数组对象的索引位置?

Sam*_*ala 5 arrays liquid shopify

在一个名为“sb”的片段中我有这个内容

{% assign seller_id = 'another_seller_shop_name,test_seller' | split: ',' %} 
{% assign seller_html = 'Another Seller Desc,Seller Description' | split: ',' %}
Run Code Online (Sandbox Code Playgroud)

在模板页面中 - 在本例中,集合列表我引用了此片段

{% include 'sb' %}{% assign seller_id_page = collection.title | replace: ' ','_' | downcase %}
Run Code Online (Sandbox Code Playgroud)

“seller_id_page”将等于“seller_id”中的值之一。我只想能够返回该值的位置,这样我就可以seller_html[x]正确分配索引值并渲染字段。

dri*_*rip 8

您将需要循环数组并获取相应等式的索引。

在代码中:

{% for item in seller_id %}
  {% if item == seller_id_page %}
    {% assign position = forloop.index0 %}
    {% break %} 
  {% endif %}
{% endfor %}

{{ seller_html[position] }}
Run Code Online (Sandbox Code Playgroud)

就是这样。