javascript - 使用 Nodejs、Express 进行 Swig for 循环,

标签 javascript node.js express swig-template

我正在尝试创建一个循环来使用 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/

相关文章:

javascript - enzyme |有条件地检查 Prop

node.js - 无法在 Swift 5 中调用 https 请求(自签名证书)

javascript - WebStorm Node 调试显式关闭错误

JavaScript:如何将数据添加到 JSON 文件内的数组

javascript - 在 Firebase 中使用 onUpdate 函数时,如何检索已更新的记录?

javascript - 如果 textarea 中有新行,则添加 1 行并增加 textarea 的大小

javascript - Node.js Express 应用程序处理启动错误

node.js - 在 PassportJS/ExpressJS 中获取 IP

javascript - 来自 AngularJS POST 请求的 Node.js 数据未定义

javascript - Bootstrap 弹出窗口无法在我自己的网站上运行