2022-06-26 14:41:13 +00:00
(jinja2)=
# Jinja2 template support
Wagtail supports Jinja2 templating for all front end features. More information on each of the template tags below can be found in the [](writing_templates) documentation.
## Configuring Django
Django needs to be configured to support Jinja2 templates. As the Wagtail admin is written using standard Django templates, Django has to be configured to use **both** templating engines. Add the Jinja2 template backend configuration to the `TEMPLATES` setting for your app as shown here:
```python
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
# ... the rest of the existing Django template configuration ...
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': True,
'OPTIONS': {
'extensions': [
'wagtail.jinja2tags.core',
'wagtail.admin.jinja2tags.userbar',
'wagtail.images.jinja2tags.images',
],
2021-09-20 16:03:28 +00:00
},
2022-06-26 14:41:13 +00:00
}
]
```
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
Jinja templates must be placed in a `jinja2/` directory in your app. For example, the standard template location for an `EventPage` model in an `events` app would be `events/jinja2/events/event_page.html` .
2015-10-01 22:06:28 +00:00
2022-06-26 14:41:13 +00:00
By default, the Jinja environment does not have any Django functions or filters. The Django documentation has more information on {class}`django.template.backends.jinja2.Jinja2` (configuring Jinja for Django).
2015-10-01 22:06:28 +00:00
2022-06-26 14:41:13 +00:00
## `self` in templates
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
In Django templates, `self` can be used to refer to the current page, stream block, or field panel. In Jinja, `self` is reserved for internal use. When writing Jinja templates, use `page` to refer to pages, `value` for stream blocks, and `field_panel` for field panels.
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
## Template tags, functions & filters
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `pageurl()`
2015-10-01 05:52:04 +00:00
Generate a URL for a Page instance:
2022-06-26 14:41:13 +00:00
```html+jinja
< a href = "{{ pageurl(page.more_information) }}" > More information< / a >
```
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
See [](pageurl_tag) for more information
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `slugurl()`
2015-10-01 05:52:04 +00:00
Generate a URL for a Page with a slug:
2022-06-26 14:41:13 +00:00
```html+jinja
< a href = "{{ slugurl(" about " ) } } " > About us< / a >
```
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
See [](slugurl_tag) for more information
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `image()`
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
Resize an image, and print an `<img>` tag:
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
```html+jinja
{# Print an image tag #}
{{ image(page.header_image, "fill-1024x200", class="header-image") }}
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
{# Resize an image #}
{% set background=image(page.background_image, "max-1024x1024") %}
< div class = "wrapper" style = "background-image: url({{ background.url }});" >
```
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
See [](image_tag) for more information
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `|richtext`
2015-10-01 05:52:04 +00:00
Transform Wagtail's internal HTML representation, expanding internal references to pages and images.
2022-06-26 14:41:13 +00:00
```html+jinja
{{ page.body|richtext }}
```
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
See [](rich_text_filter) for more information
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `wagtail_site`
2022-01-19 18:34:41 +00:00
Returns the Site object corresponding to the current request.
2022-06-26 14:41:13 +00:00
```html+jinja
{{ wagtail_site().site_name }}
```
2022-01-19 18:34:41 +00:00
2022-06-26 14:41:13 +00:00
See [](wagtail_site_tag) for more information
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `wagtailuserbar()`
2015-10-01 05:52:04 +00:00
Output the Wagtail contextual flyout menu for editing pages from the front end
2022-06-26 14:41:13 +00:00
```html+jinja
{{ wagtailuserbar() }}
```
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
See [](wagtailuserbar_tag) for more information
2015-10-01 05:52:04 +00:00
2022-06-26 14:41:13 +00:00
### `{% include_block %}`
2016-07-12 14:48:20 +00:00
Output the HTML representation for the stream content as a whole, as well as for each individual block.
2016-07-27 16:04:19 +00:00
Allows to pass template context (by default) to the StreamField template.
2016-07-12 14:48:20 +00:00
2022-06-26 14:41:13 +00:00
```html+jinja
{% include_block page.body %}
{% include_block page.body with context %} {# The same as the previous #}
{% include_block page.body without context %}
```
2016-07-12 14:48:20 +00:00
2022-06-26 14:41:13 +00:00
See [StreamField template rendering ](streamfield_template_rendering ) for more information.
2016-07-12 14:48:20 +00:00
2022-06-26 14:41:13 +00:00
```{note}
The ``{% include_block %}`` tag is designed to closely follow the syntax and behaviour
of Jinja's ``{% include %}``, so it does not implement the Django version's feature of
only passing specified variables into the context.
```