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

Jar*_*ski 6 liquid shopify

这就是我想要完成的事情:

  <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>
Run Code Online (Sandbox Code Playgroud)

当它被渲染时,我得到:

background-color: settings.collection_color_1;
Run Code Online (Sandbox Code Playgroud)

而不是从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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法以编程方式完成此操作,还是我真的必须使用<%case forloop.index%>?

Ste*_*arp 12

看看我对这个类似问题的回答.

试试这个:

{% assign color_from_settings = 'collection_color_' | append:forloop.index %}
...
{{ settings[color_from_settings] }}
Run Code Online (Sandbox Code Playgroud)

  • 它适用于我 - {%for i in(1..2)%} {%assign color_from_settings ='collection_color_'| 追加:forloop.index%} <li class ="site-nav - active"style ="background-color:{{settings [color_from_settings]}}"> 1 </ li> {%endfor%} (2认同)