The template file i created contains this:
{% if type({'a':1,'b':2}) is dict %} print "Oh Yes!!" {% else %} print "Oh No!!!" {% endif %}
Then Jinja2 responds by saying:
TemplateAssertionError: no test named 'dict'
I am completely new to Jinja2 and Flask
If variable is always evaluated to True when not None, {% if variable != None %} is equivalent to {% if variable %} .
Flask comes packaged with Jinja2, and hence we just need to install Flask. For this series, I recommend using the development version of Flask, which includes much more stable command line support among many other features and improvements to Flask in general.
Jinja can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn't need to have a specific extension: . html , . xml , or any other extension is just fine.
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).
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With