有没有办法连接两个字符串然后处理输出标记内的结果而不是字符串?

时间:2015-05-09 22:33:44

标签: shopify liquid

这是我想要完成的事情:

  <ul class="site-nav__dropdown">
    {% for childlink in linklists[child_list_handle].links %}
this - {% assign color_from_settings = 'settings.collection_color_' | append: forloop.index %}
used here -  <li{% if childlink.active %} class="site-nav--active" {% else %} style="background-color: {{ color_from_settings }}" {% endif %}>
        <a href="{{ childlink.url }}" class="site-nav__link">{{ childlink.title | escape }}</a>
      </li>
    {% endfor %}
  </ul>

当它被渲染时,我得到:

background-color: settings.collection_color_1;

而不是从settings_data.json获取的实际颜色,如下所示:

{
  "current": "Default",
  "presets": {
    "Default": {
      ...
      "collection_color_1": "#9997c0",
      "collection_color_2": "#8997b1",
      "collection_color_3": "#7997a2",
      "collection_color_4": "#699793",
      "collection_color_5": "#599784",
      "collection_color_6": "#499775",
      "collection_color_7": "#399766",
      "collection_color_8": "#299757",
      "collection_color_9": "#199748",
      "collection_color_10": "#099739"
    }
  }
}

有没有办法以编程方式完成此操作,或者我是否真的必须使用&lt;%case forloop.index%&gt; ?

1 个答案:

答案 0 :(得分:12)

看看我对this similar question的回答。

试试这个:

{% assign color_from_settings = 'collection_color_' | append:forloop.index %}
...
{{ settings[color_from_settings] }}