check variable type inside Jinja2 in Flask check variable type inside Jinja2 in Flask flask flask

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).


How about:

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

see List of Builtin Tests for reference.


In case you want to get a custom type you can access field name like in this example:

  {% if 'RelationField' in field.__class__.__name__ %}      <div class="col-md-1">      Manage object      </div>  {% endif %}