Rak*_*kib 22 variables types jinja2 flask
我创建的模板文件包含:
{% if type({'a':1,'b':2}) is dict %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
Run Code Online (Sandbox Code Playgroud)
然后Jinja2回应说:
TemplateAssertionError: no test named 'dict'
Run Code Online (Sandbox Code Playgroud)
我是Jinja2和Flask的新手
Sea*_*ira 39
您正在寻找mapping测试:
{% if {'a': 1, 'b': 2} is mapping %}
"Oh Yes!"
{% else %}
"Oh No!"
{% endif %}
Run Code Online (Sandbox Code Playgroud)
神社是不是虽然蟒蛇,所以你没有访问所有的内建(type与print不存在,例如,除非你将它们添加到背景下,在瓶,你这样做与context_processor装饰).
你实际上根本不需要print.默认情况下,输出所有内容(除非您在extends父模板的子模板中,在这种情况下,您可以执行有趣的操作,例如NULL Master回退,因为只输出主模板中可用名称的块).
怎么样:
{% if {'a':1,'b':2} is mapping %}
print "Oh Yes!!"
{% else %}
print "Oh No!!!"
{% endif %}
Run Code Online (Sandbox Code Playgroud)
请参阅内置测试列表以供参考.
如果要获取自定义类型,可以像以下示例一样访问字段名称:
{% if 'RelationField' in field.__class__.__name__ %}
<div class="col-md-1">
Manage object
</div>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37774 次 |
| 最近记录: |