我正在尝试创建一个循环来使用 swig 访问数组对象。
我想创建一个循环来检查对象的长度。我可以通过 {{styles[0].style}} 访问对象。其中 [] 是一个数组。所以我需要做的就是拥有类似的东西
for (var i; i < styles.length; i++) { styles[i].style };
如果styles对象中有十个数组,我需要展示{{styles[0].style}},{{styles[1].style}},... {{styles[9].style }}
这是我想要放入 {{}} 的代码:
<table border="1">
<tbody>
<tr><td><a href={{styles[0].a}}><div style="width: 175px;height: 250px" id="products">
<img id="img" src={{styles[0].img}}></div></a></td></tr><tr><td id="styleno">{{styles[0].style}}
</td></tr>
</tbody>
</table>
我认为需要这样的东西:
{% for x in y %}
{% if loop.first %}<ul>{% endif %}
<li>{{ loop.index }} - {{ loop.key }}: {{ x }}</li>
{% if loop.last %}</ul>{% endif %}
{% endfor %}
有人可以帮忙吗?谢谢!
这是我的 JSON:
{
"styles":[
{"style":"123", "a":"http://", "img":"http://", "price":3},
{"style":"234", "a":"http://", "img":"http://", "price":2}
]
}
最佳答案
使用 if 语句检查 styles
项的长度...
在 swig@1.0.0-pre1
{% if styles and styles.length === 10 %}
<table border="1">
<tbody>
{% for style in styles %}
<tr>
<td>
<a href="{{ style.a }}">
<div style="width:175px; height:250px;" id="products">
<img id="img" src="{{ style.img }}">
</div>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
关于javascript - 使用 Nodejs、Express 进行 Swig for 循环,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18238958/