检查Flask中Jinja2内部的变量类型 [英] check variable type inside Jinja2 in Flask
问题描述
我创建的模板文件包含以下内容:
The template file i created contains this:
{% if type({'a':1,'b':2}) is dict %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
然后Jinja2回答说:
Then Jinja2 responds by saying:
TemplateAssertionError: no test named 'dict'
我对Jinja2和Flask完全陌生
I am completely new to Jinja2 and Flask
推荐答案
您正在寻找 mapping
测试:
You are looking for the mapping
test:
{% if {'a': 1, 'b': 2} is mapping %}
"Oh Yes!"
{% else %}
"Oh No!"
{% endif %}
Jinja虽然不是Python,所以您无权访问所有内置函数(例如,type
和print
不存在,除非将它们添加到 context_processor
装饰器).
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).
您实际上根本不需要print
.默认情况下,所有内容都会输出(除非您位于父模板的子模板中,在这种情况下,您可以
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).
这篇关于检查Flask中Jinja2内部的变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!