【问题标题】:Is there a way for concatenate two strings and then process the result inside the Output tags not as a string?有没有办法连接两个字符串,然后在输出标签中处理结果而不是作为字符串?
【发布时间】:2015-05-09 22:33:43
【问题描述】:

这是我想要完成的:

  <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"
    }
  }
}

有没有办法以编程方式完成此操作,还是我真的必须使用 ?

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    看看我对this similar question的回答。

    试试这个:

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

    【讨论】:

    • 没用。奇怪的是,我得到了“:”作为输出而不是颜色值。我使用的确切代码是这样的:&lt;li{% if childlink.active %} class="site-nav--active" {% else %} style="background-color: {{ settings[color_from_settings] }}"{% endif %}&gt; 我也试过{% settings[color_from_settings] %} 但这甚至没有上传(使用shopify ruby​​ gem)。给我一个“422 Unprocessable Entity”错误。
    • 它对我有用- {% for i in (1..2) %} {% assign color_from_settings = 'collection_color_' | append:forloop.index %}
    • {% endfor %}
  • 啊...对不起,它确实有效。我忘记将“settings.collection_color_”更改为“collection_color_”。傻我...
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签