Jinja2
Posted on 2013-10-29 21:56:46 +0900 in Technique
Variables
{{ foo.bar }}
{{ foo[‘bar’] }}
Implementation
Sake foo.bar in Jinja2 does the following thing on the Python layer:
- check if there is an attribute called bar on foo.
- if there is not, check if there is an item ‘bar’ in foo.
- if there is not, return an undefined object.
foo[‘bar’] works mostly the same with a difference in the order:
- check if there is an item ‘bar’ in foo.
- if there is not, check if there is an attribute called bar on foo.
- if there is not, return an undefined object.
###Filers###
Variables can be modified by filters.
{{ name|striptags|title }} will remove all HTML Tags from the name and title-cases it.
###Tests### Tests can be used to test a variable against a common expression.
{% if loop.index is divisibleby 3 %}
###Comments###
{# note: disabled template because we no longer use this
{% for user in users %}
…
{% endfor %}
#}
Hide Comments
comments powered by Disqus