Skip to content

Instantly share code, notes, and snippets.

@traverseda
Created January 28, 2016 19:04
Show Gist options
  • Save traverseda/4f43148331a469a9169c to your computer and use it in GitHub Desktop.
Save traverseda/4f43148331a469a9169c to your computer and use it in GitHub Desktop.
Render a json-tree recusrivly in jinja2
{% macro keyValue(key,value) -%}
{# generic case, if key doesn't match a particular renderer #}
<div class="{{key}}">
{{ router(value) }}
</div>
{%- endmacro %}
{% macro router(value)%}
{% if value is mapping %}
{% for key2,value2 in value.items() %}
{{ keyValue(key2,value2) }}
{% endfor %}
{% elif value is string %}
{{ value|markd }}
{% elif value is iterable %}
{{list(value)}}
{% endif %}
{% endmacro %}
{% macro list(data) %}
{% for item in data %}
{{ router(item) }}
{% endfor %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment