Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active August 6, 2023 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f-steff/e1c54b99e2bc28723e9ea2d5ccbd2674 to your computer and use it in GitHub Desktop.
Save f-steff/e1c54b99e2bc28723e9ea2d5ccbd2674 to your computer and use it in GitHub Desktop.
var_dump for Python Jinja2 html template engine.

This code is a macro written in Jinja2, a powerful template engine for Python. This macro, named var_dump, works similarly to PHP's var_dump() function. It helps to print or display structured information about variables, especially useful for debugging.

The var_dump macro takes four parameters:

  • var: The variable you want to display.

  • var_name: The name of the variable (this is optional, and its default value is an empty string).

  • space: The indentation level (this is optional, and its default value is 0).

  • indent: The indentation character(s) (this is optional, and its default value is three spaces).

The macro checks the type of var and displays its information accordingly. It can handle strings, numbers (integers or floats), mappings (similar to PHP's associative arrays or Python's dictionaries), iterable objects (like lists or sets), booleans, and None values. If the variable type is not any of the above, it will simply print the variable as is.

To use the macro, you simply call it with the variable you want to inspect. For example:

{{ var_dump(siteinfo, '[siteinfo]') }}
{{ var_dump(labels, '[labels]') }}

This will print out structured, easy-to-read information about siteinfo and labels.

The output will show the type and value of the variable, and if the variable is a mapping or iterable, it will recursively print out information about its elements. For mappings, it also includes an index to indicate the order of the elements.

This var_dump macro can be very helpful when debugging Jinja2 templates, especially when dealing with complex data structures.

It's recomended to simple insert the macro and the calls to the macro as shown in the var_dump.html file. This way the output will not mess up your page, and it's easily available when reading the page source.

<html>
<body>
Here you put all your normal html template code.
{# A var_dump() macro, implementaion is somewhat similar to the one in php #}
{# The var_dump macro is available here: https://gist.github.com/f-steff/e1c54b99e2bc28723e9ea2d5ccbd2674 #}
{% macro var_dump(var, var_name='', space=0, indent=' ') -%}
{{ indent * space }}{{ var_name }} => {{"\n"}}
{%- if var is string -%}
{{ indent * (space + 1) }}string({{ var|length }}): "{{ var }}"{{"\n"}}
{%- elif var is number -%}
{{ indent * (space + 1) }}int({{ var }}){{"\n"}}
{%- elif var is mapping -%}
{%- set count = var|length %}
{%- if count > 0 -%}
{{ indent * (space + 1) }} array({{ count }}) { {{"\n"}}
{%- for key, value in var.items() -%}
{{ var_dump(value, '' ~ loop.index ~ ': [' ~ key ~ ']', space + 2, indent) }}
{%- endfor -%}
{{ indent * (space + 1) }} }{{"\n"}}
{%- else -%}
array(0) {}
{%- endif %}
{%- elif var is iterable -%}
{%- set count = var|length %}
{%- if count > 0 -%}
{{ indent * (space + 1) }} array({{ count }}) { {{"\n"}}
{%- for value in var -%}
{{ var_dump(value, '[' ~ loop.index ~ ']', space + 2, indent) }}
{%- endfor -%}
{{ indent * (space + 1) }} }{{"\n"}}
{%- else -%}
array(0) {}
{%- endif %}
{%- elif var is boolean -%}
{{ indent * (space + 1) }} bool({{ var }}){{"\n"}}
{%- elif var is none -%}
{{ indent * (space + 1) }} NULL{{"\n"}}
{%- else -%}
{{ indent * (space + 1) }} {{ var }}{{"\n"}}
{%- endif -%}
{% endmacro %}
{{ var_dump(siteinfo, '[siteinfo]') }}
{{ var_dump(labels, '[labels]') }}
-->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment