docs.jinja - jgrey4296/jgrey4296.github.io GitHub Wiki
{# Comments #}
{% Statements %}
{{ Expressions }}
{{ foo.bar }}
{{ foo['bar'] }}
# Escaping:
{{ '{{' }}
# Clean all spaces and newlines preceding first char:
{% raw -%}
# Html Escaping:
{{ user.name | e }}
# Protect from auto escape:
{{ user.name | safe }}
Single trailing newlines are stripped Multiple newlines are kept
# assign the content to 'navigation'
{% set navigation %}
stuff
otherstuff
{% endset %}
{{ navigation }}
{% for x in y %}
{{ loop.first }} # bool : true if first iteration
{{ loop.index }} # int
{{ loop.length }} # int
{{ loop.cycle('odd', 'even') }} #
{{ loop.last }} # bool : true if last iteration
{% else %}
# Default
{% endfor %}
{% if True %}
# something
{% elif False %}
# another
{% else %}
# last option
{% endif %}
{% macro example(name, type='text') -%}
{{varargs}} {{kwargs}} {{caller}}
{{name}} {{arguments}} {{catch_kwargs}} {{catch_varargs}}
<input type="{{type}}" name="{{name}}" />
{%- endmacro %}
# Call with:
{{ example('test') }}
# or:
{% call example('test') %}
Internal Call block is accessed in the macro as the 'caller' var
{% endcall %}
# Passed current context:
{% include 'header.html' %}
# Does not pass current context
{% import 'forms.html' as forms %}
{% filter upper %}
This will become uppercase.
{% endfilter %}
# Literals
"Some Text"
42
['list', 'of', 'objects']
('tuples', 'of', 42)
{'dict': 42}
# Lowercase:
true
false
none
# Math:
2 + 3
5 - 2
5 * 2
2 ** 3
# Float div:
10 / 3
# Int div:
10 // 2
% Remainder:
11 % 4
# Comparisons
==
!=
>
=
<
<=
and
or
not
(expr)
is
in
# Convert to str and concat:
{{ "Hello " ~ name ~ "! : " ~ 22 }}
# Python methods:
{{ page.title.capitalize() }}
Applied using “|”:
- upper
- abs
- attr
- batch
- capitalize
- center
- default
- dictsort
- escape
- filesizeformat
- first
- float
- forceescape
- format
- groupby
- indent
- int
- items
- join
- last
- length
- list
- lower
- map
- pprint
- random
- reject
- rejectattr
- replace
- reverse
- round
- safe
- select
- selectattr
- slice
- sort
- string
- striptags
- sum
- title
- tojson
- trim
- truncate
- unique
- upper
- urlencode
- urlize
- worcount
- wordwrap
- xmlattr
- boolean
- callable
- defined
- divisibleby
- eq
- escaped
- even
- false
- filter
- float
- ge
- gt
- in
- integer
- iterable
- le
- lower
- lt
- mapping
- ne
- none
- number
- odd
- sameas
- sequence
- string
- true
- undefined
- upper
- range([start, ] stop[, step])
- lipsum(n=5, html=True, min=20, max=100)
- dict(**items)
- cycler(*items)
- joiner(sep=’, ‘)
- namespace(…)
{% extends 'base.html' %}
# Mark a block as required:
{% block important required %}{% endblock %}
# Override declared blocks:
{% block title %}
override content
{% endblock %}
# Call the default definition
{% block head %}
{{ super() }}
override content
# End the block explicitly
{% endblock head %}
# Add extensions after creation time:
{{ jinja_env.add_extension('jinja2.ext.debug') }}
# Dump current context, filters, tests
{% debug %}
{% with %}
{% set foo = 42 %}
{{ foo }}
{% endwith %}
Foo isn't available here