Liquid迭代语句中的变量

时间:2016-10-22 03:19:55

标签: for-loop liquid

我知道如何使用对象,但我的具体情况有一些细微差别。 我试图在对象中迭代var i。请参阅下面的内容,我已尝试使用语法进行各种更改,但似乎无法使其正常工作。可能吗?如果知道某些支持文档,我也很乐意看到它,但似乎无法在任何地方找到它。

<div class="grid--square">
  {% for i in (1..12) %}

  <div role="button" class="grid__item one-quarter" style="background:{% if settings.grid_[i]_image %}url({{ 'grid_[i].png' | asset_url }}){% else %}{{ settings.grid_[i]_color }}{% endif %};">
    {% if settings.grid_[i]_link != blank %}
      <a href="{{ settings.grid_[i]_link }}"></a>
    {% endif %}
    {%if settings.grid_[i]_text != blank %}
      {% unless settings.grid_[i]_image %}
        <div class="text">
          <h2>{{ settings.grid_[i]_text }}</h2>
        </div>
      {% endunless %}
    {% endif %}
  </div>
  {% endfor %}
</div>

1 个答案:

答案 0 :(得分:0)

我通过捕获每个变量来实现解决方案,以便我可以更改整个对象。

<div class="grid--square">
      {% for i in (1..12) %}
        {% capture enable_image %}grid_{{ i }}_image{% endcapture %}
        {% capture image %}grid_{{ i }}.png{% endcapture %}
        {% capture color %}grid_{{ i }}_color{% endcapture %}
        {% capture text %}grid_{{ i }}_text{% endcapture %}
        {% capture link %}grid_{{ i }}_link{% endcapture %}
        <div role="button" class="grid__item one-quarter" style="background:{% if settings[enable_image] %}url({{ image | asset_url }}){% else %}{{ settings[color] }}{% endif %};">
          {% if settings[link] != blank %}
            <a href="{{ settings[link] }}"></a>
          {% endif %}
          {%if settings[text] != blank %}
            {% unless settings[image] %}
              <div class="text">
                <h2>{{ settings[text] }}</h2>
              </div>
            {% endunless %}
          {% endif %}
        </div>
      {% endfor %}
    </div>