Skip to content

Instantly share code, notes, and snippets.

@kevinmpowell
Last active November 6, 2017 21:30
Show Gist options
  • Save kevinmpowell/54dcdd616c40130a5f1e3d4fb7d1ec18 to your computer and use it in GitHub Desktop.
Save kevinmpowell/54dcdd616c40130a5f1e3d4fb7d1ec18 to your computer and use it in GitHub Desktop.
Differentiate a string from an array when looping over mixed values in nunjucks
{% set mixed_object = {
key_1: 'Delta',
key_2: 'Epsilon',
key_3: ['Zeta', 'Eta', 'Theta', 'Iota']
} %}
{% for key, value in mixed_objct %}
{% if value[0].length > 1 %}
{# This is an array, do array stuff with it #}
{{ value | join('<br>') | safe }}
{% else %}
{# This is just a string, array filters will break it, do string stuff with it #}
{{ value }}
{% endif %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment