如何获取液体变量的值
How to get a value for liquid variable
如果我有这个:
"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, ]
我哪里错了?抱歉,液体初学者。
循环开头的 'name' 不是 属性,而是为循环创建局部变量。看看MDN's for-in loop documentation for Javascript。它的工作原理类似。
试试这个:
{% for color in attributes.color %}
{{ color.name }}
{% endfor %}
如果我有这个:
"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, ]
我哪里错了?抱歉,液体初学者。
'name' 不是 属性,而是为循环创建局部变量。看看MDN's for-in loop documentation for Javascript。它的工作原理类似。
试试这个:
{% for color in attributes.color %}
{{ color.name }}
{% endfor %}