Swig for Node with Nodejs,Express,

时间:2013-08-14 18:00:56

标签: javascript node.js express swig-template

我正在尝试使用swig创建一个访问数组对象的循环。

我想创建一个检查对象长度的循环。我可以通过{{styles [0] .style}}访问对象。其中[]是一个数组。所以我需要做的就是拥有像

这样的东西
for (var i; i < styles.length; i++) { styles[i].style };

如果样式对象中有十个数组,我需要展示{{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}
         ]
}

1 个答案:

答案 0 :(得分: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 %}