【问题标题】:In Liquid (Shopify) how am I able to get the index position of a specific array object?在 Liquid (Shopify) 中,我如何获取特定数组对象的索引位置?
【发布时间】:2020-04-10 13:20:16
【问题描述】:

在一个名为 'sb' 的 sn-p 中,我有这个内容

{% assign seller_id = 'another_seller_shop_name,test_seller' | split: ',' %} 
{% assign seller_html = 'Another Seller Desc,Seller Description' | split: ',' %}

在模板页面中——在本例中,collection-list 我已经引用了这个 sn-p

{% include 'sb' %}{% assign seller_id_page = collection.title | replace: ' ','_' | downcase %}

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

【问题讨论】:

    标签: arrays shopify liquid


    【解决方案1】:

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

    在代码中:

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

    仅此而已。

    【讨论】:

    • 谢谢伙计,这真是令人难以置信的优雅和直截了当。赞赏。
    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多