Last active
August 3, 2019 07:44
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"items": [ | |
{ | |
"name": "Last modified time", | |
"value": "2004-12-23T23:33Z" | |
}, | |
{ | |
"name": "Recommended update interval", | |
"value": "60s" | |
}, | |
{ | |
"name": [ | |
"Authors", | |
"Editors" | |
], | |
"value": [ | |
"Robert Rothman", | |
"Daniel Jackson" | |
] | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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