如何获得液体变量的值

时间:2015-02-18 04:26:10

标签: liquid

如果我有这个:

"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] [姓名,绿松石] [displayType,5] [图片,] [价格] [id,29907474] [name,Teal] [displayType,5] [image,] [price,]

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

1 个答案:

答案 0 :(得分:0)

循环开头的

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

试试这个:

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