【问题标题】:How to get a value for liquid variable如何获取液体变量的值
【发布时间】:2015-02-18 04:26:09
【问题描述】:

如果我有这个:

"attributes": {
"color": [
  {
    "id": 29907472,
    "name": "Green",
    "displayType": 5,
    "image": "/_assets/img/products/tshirt-green.png",
    "price": null
  },
  {
    "id": 29907473,
    "name": "Turquoise",
    "displayType": 5,
    "image": "",
    "price": null
  },
  {
    "id": 29907474,
    "name": "Teal",
    "displayType": 5,
    "image": "",
    "price": null
  }
]

},

并且只想输出液体中颜色的名称,我需要怎么做?我试过了

{% for name in attributes.color %}
      {{ name }} 
{% endfor %}

但我只得到这个作为输出:[id, 29907472][name, Green][displayType, 5][image, /_assets/img/products/tshirt-green.png][price, ] [id, 29907473][name, Turquoise][displayType, 5][image, ][price, ] [id, 29907474][name, Teal][displayType, 5][image, ][price, ]

我哪里错了?对不起,液体初学者。

【问题讨论】:

    标签: liquid


    【解决方案1】:
    循环开头的

    'name' 不是属性,它为循环创建了一个局部变量。看看MDN's for-in loop documentation for Javascript。它的工作原理类似。

    试试这个:

    {% for color in attributes.color %}
        {{ color.name }} 
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2018-12-25
      • 1970-01-01
      相关资源
      最近更新 更多