Search code examples
expressswig-template

Foreach equiv with the swig tpl engine


<p>
    <strong>id</strong>: {{ user.facebook.id }}<br>
    <strong>token</strong>: {{ user.facebook.token }}<br>
    <strong>email</strong>: {{ user.facebook.email }}<br>
    <strong>name</strong>: {{ user.facebook.name }}
</p>

I am trying to loop through an object printing out the key and the value as i go but it is not working.. insetad the key us the result of the user.facebook[key]. I am using the swig tpl engine with express in node.

<p>
{% for key in user.facebook %}
    <strong>{{key}}</strong>{{user.facebook[key]}}<br>
{% endfor %}
</p>

Does anyone know how i can achieve this?


Solution

  • I did not realise how closely related swig was to twig.. after much search i thought i would just thw twig functionality a try and hey presto it worked :)

    http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-keys-and-values

    {% for key, value in some.object %}
        <strong>{[key]}</strong>{[value]}<br>
    {% endfor %}