# Python : Pallets : Jinja \[ [pypi](https://pypi.org/project/Jinja/) | [src](https://github.com/pallets/jinja/) | [docs](https://jinja.palletsprojects.com/en/3.1.x/) | [api](https://jinja.palletsprojects.com/en/3.1.x/api/) ] ## Cheatsheet ```jinja2 {{ expression }} {% statement %} {# comment #} # line statement ## line comment ``` #### Control Structures ```jinja2 {% if %} {% elif %} {% else %} {% end %} {% ... if ... [else ...] %} {% for x in myiterable [if ...] %} -or- {% for k, v in mydict.iteritems() [if ...] %} {% else %} # optional ...rendered if empty sequence... {% endfor %} ``` #### Operators ```jinja2 in # x in y, x not in y is # x is y, x is not y | # apply filter ~ # join into string; ex: {{ "Hello " ~ name ~ "!" }} ``` #### Variables ```jinja2 true, false, none # titlecase is allowed, but lowercase is preferred self self.<block> # inside loops loop.index # current iteration (1 indexed) loop.index0 # current iteration (0 indexed) loop.first # True if first iteration loop.last # True if last iteration loop.length loop.cycle # cycle between a list of sequences; ex: loop.cycle( 'odd', 'even' ) loop.previtem loop.nextitem loop.changed(*val) # True if previously called with a different value (to detect when in the sequence it changes) # inside macros varargs # leftover positional arguments kwargs # leftover keyword arguments caller # (if called from a call tag) # assignments {% set foo = ... %} # if top-level (outside blocks), will be exported like macros {% set key, value = ... %} {% set foo [| <filter>] %} ...content... {% endset %} ``` #### Macros ```jinja2 {% macro input(name, value='', type='text', size=20) -%} <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"> {%- endmacro %} <p>{{ input('username') }}</p> ``` #### Importation Accessing macros in other templates. ```jinja2 {% import 'my_macros.html' as m %} {% m.foo(...) %} {% from 'my_macros.html' import foo as bar %} {% bar(...) %} ``` #### Inclusion ```jinja2 {% include 'header.html' %} # render contents into current namespace ``` #### Inheritance ```jinja2 # base.html {% block foo %} ...initial contents... {% endblock %} # or {% endblock foo %} for readability # child template {% extends "base.html" %} {% block foo %} ...overridden contents... {% # child template {% extends "base.html" %} {% block foo %} {{ super() }} ...appended contents... {% ``` #### Filters ```jinja2 {{ <expression> | <filter1> | ... }} {% filter <filter> %} ...content... {% endfilter %} ``` #### Built-In Filters Args usable as positional or named. ```jinja2 <number> | abs <object> | attr( <name> ) <string> | capitalize <value> | default( <value> ) # alias: d ... | dictsort # {% for item in mydict|dictsort %} <string> | escape # alias: e <string> | indent( width=4, first=False ) # indent each line after the first by <int> spaces <value> | pprint | safe # mark output as HTML-safe to prevent automatic escaping # (Jinja2 functions - i.e., macros, super, self.BLOCKNAME - always return template data that is marked as safe) <value> | sort <value> | sort( attribute='...') <string> | title # titlecase <value> | urlencode ``` #### Built-In Tests Parens optional if only one arg. ```jinja2 {{ if x is ... }} defined divisibleby( <int> ) eq( <b> ) even none number odd ``` #### Built-In Functions Whatever.