check variable type inside Jinja2 in Flask

You are looking for the mapping test:

{% if {'a': 1, 'b': 2} is mapping %}
    "Oh Yes!"
{% else %}
    "Oh No!"
{% endif %}

Jinja is not Python though, so you don’t have access to all the builtins (type and print do not exist, for example, unless you add them to the context. In Flask, you do this with the context_processor decorator).

You don’t actually need print at all. By default everything is output (unless you are in a child template that extends a parent, in which case you can do interesting things like the NULL Master fallback because only blocks with names available in the master template are output).

Leave a Comment