Skip to content

Instantly share code, notes, and snippets.

@paulrobertlloyd
Last active August 3, 2019 07:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulrobertlloyd/ed1a7473ed5b12793034 to your computer and use it in GitHub Desktop.
Save paulrobertlloyd/ed1a7473ed5b12793034 to your computer and use it in GitHub Desktop.
Looping over values with Nunjucks. Some values are strings, some values are arrays. Now what?
{
"items": [
{
"name": "Last modified time",
"value": "2004-12-23T23:33Z"
},
{
"name": "Recommended update interval",
"value": "60s"
},
{
"name": [
"Authors",
"Editors"
],
"value": [
"Robert Rothman",
"Daniel Jackson"
]
}
]
}
<!-- Expected output -->
<dl>
<dt>Last modified time</dt>
<dd>2004-12-23T23:33Z</dd>
<dt>Recommended update interval</dt>
<dd>60s</dd>
<dt>Authors</dt>
<dt>Editors</dt>
<dd>Robert Rothman</dd>
<dd>Daniel Jackson</dd>
</dl>
<dl>
{% for item in items %}
{# if item.name is an array #}
{% for name in item.name %}
<dt>{{ name }}</dt>
{% endfor %}
{# else #}
<dt>{{ item.name }}</dt>
{# endif #}
{# if item.value is an array #}
{% for value in item.value %}
<dd>{{ value }}</dd>
{% endfor %}
{# else #}
<dd>{{ item.value }}</dd>
{# endif #}
{% endfor %}
</dl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment