python jinja - ghdrako/doc_snipets GitHub Wiki
Jinja
Jinja template
contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template.
Delimiter:
{% ... %}
for Statements{{ ... }}
for Expressions to print to the template output{# ... #}
for Comments not included in the template output# ... ##
for Line Statements
from jinja2 import Template
Filters
Jinja2 filters allow data manipulation and can be used in conjunction with variables:
{{ some_variable | default('Default Value') }}
The filters in Jinja2 transform variables and can be used for various purposes:
default
: Provides a default value if a variable is undefined.length
: Returns the length of a string or list.to_json
: Converts data into JSON format.regex_replace
: Performs a regular expression replace operation.
Conditionals
Conditional statements like if-else can be used inside templates:
{% if users %}
{% for user in users %}
{{ user.name }}
{% endfor %}
{% else %}
No users found.
{% endif %}
Loops
You can loop over sequences using the for loop:
{% for item in items %}
{{ item }}
{% endfor %}
Macros
Macros are reusable snippets of code:
{% macro say_hello(name) %}
Hello, {{ name }}!
{% endmacro %}
Template Inheritance
You can create a base template and then extend it in child templates. This allows for consistent layouts:
{% extends "base.html" %}
{% block content %}
This content replaces the base content block.
{% endblock %}
name = 'Peter'
age = 34
tm = Template("My name is {{ name }} and I am {{ age }}")
msg = tm.render(name=name, age=age)
print(msg)
Dictionary
person = { 'name': 'Person', 'age': 34 }
tm = Template("My name is {{ per.name }} and I am {{ per.age }}")
# tm = Template("My name is {{ per['name'] }} and I am {{ per['age'] }}")
msg = tm.render(per=person)
print(msg)
Escape special symbols
data = '''
{% raw %}
His name is {{ name }}
{% endraw %}
'''
tm = Template(data)
msg = tm.render(name='Peter')
print(msg)
Jinja for expressions
templates/showpersons.txt:
{% for person in persons -%}
{{ person.name }} {{ person.age }}
{% endfor %}
from jinja2 import Environment, FileSystemLoader
persons = [
{'name': 'Andrej', 'age': 34},
{'name': 'Mark', 'age': 17},
{'name': 'Thomas', 'age': 44},
{'name': 'Lucy', 'age': 14},
{'name': 'Robert', 'age': 23},
{'name': 'Dragomir', 'age': 54}
]
file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)
template = env.get_template('showpersons.txt')
output = template.render(persons=persons)
print(output)
rainbow.txt:
{% for color in colors %}
{{ color }}
{% endfor %}
Render with a list of colors.
template = env.get_template('rainbow.txt')
colors = ['red', 'green', 'blue']
output = template.render(colors=colors)
print(output)
```
This is a cheat sheet for the Jinja templating syntax.
Syntax
The syntax is somewhat similar to ERB except for {} instead of <> quotes
```
{{ ... }} # Escaping for expressions that create output
{% ... %} # Escaping for control statements
# ... ## # Line statements
```
Loops
```
{% for i in list %} ... {% endfor %}
```
Functions
Defining functions
```
{% macro myfunct(param1, param2) %}
...
{% endmacro %}
```
Using functions
```
{% myfunc(var, 5) %}
```