【问题标题】:Swig for loop with Nodejs, Express,使用 Nodejs、Express、Swig for 循环,
【发布时间】:2013-08-17 12:17:08
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js express swig-template


    【解决方案1】:

    使用 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 %}
    

    【讨论】:

    • 我只能通过样式访问我的 JSON[0].style 这种格式..还有其他想法吗?
    • 你需要实际发布你的 JSON,因为我不明白你在说什么。
    • 我上传了我的 JSON 谢谢!
    • @wisleans 我给你的例子会奏效。你真的试过了吗?
    • 啊!我找到了.. 我想知道为什么只有 {{ style.img}} 可以工作.. 你在 上有错字.. 谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2019-02-15
    相关资源
    最近更新 更多