wagtail/docs/reference/jinja2.rst

137 wiersze
4.2 KiB
ReStructuredText
Czysty Zwykły widok Historia

2015-10-01 05:52:04 +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 :ref:`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:
2015-10-01 05:52:04 +00:00
.. code-block:: python
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
# ... the rest of the existing Django template configuration ...
},
2015-10-01 05:52:04 +00:00
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': True,
'OPTIONS': {
'extensions': [
2022-03-17 14:38:02 +00:00
'wagtail.jinja2tags.core',
Rename wagtail.wagtailadmin to wagtail.admin Conflicts: docs/advanced_topics/customisation/admin_templates.rst docs/advanced_topics/customisation/page_editing_interface.rst docs/advanced_topics/i18n/duplicate_tree.rst docs/advanced_topics/jinja2.rst docs/advanced_topics/settings.rst docs/getting_started/integrating_into_django.rst docs/getting_started/tutorial.rst docs/reference/hooks.rst docs/reference/pages/panels.rst docs/topics/pages.rst docs/topics/streamfield.rst wagtail/admin/blocks.py wagtail/admin/checks.py wagtail/admin/edit_handlers.py wagtail/admin/forms.py wagtail/admin/rich_text.py wagtail/admin/search.py wagtail/admin/site_summary.py wagtail/admin/templatetags/wagtailadmin_tags.py wagtail/admin/tests/test_admin_search.py wagtail/admin/tests/test_buttons_hooks.py wagtail/admin/tests/test_compare.py wagtail/admin/tests/test_edit_handlers.py wagtail/admin/tests/test_page_chooser.py wagtail/admin/tests/test_pages_views.py wagtail/admin/tests/test_rich_text.py wagtail/admin/tests/test_widgets.py wagtail/admin/tests/tests.py wagtail/admin/urls/__init__.py wagtail/admin/views/account.py wagtail/admin/views/chooser.py wagtail/admin/views/collection_privacy.py wagtail/admin/views/collections.py wagtail/admin/views/home.py wagtail/admin/views/page_privacy.py wagtail/admin/viewsets/model.py wagtail/admin/wagtail_hooks.py wagtail/admin/widgets.py wagtail/contrib/settings/registry.py wagtail/contrib/wagtailsearchpromotions/wagtail_hooks.py wagtail/contrib/wagtailstyleguide/wagtail_hooks.py wagtail/project_template/project_name/settings/base.py wagtail/project_template/project_name/urls.py wagtail/tests/non_root_urls.py wagtail/tests/settings.py wagtail/tests/snippets/models.py wagtail/tests/testapp/models.py wagtail/tests/testapp/wagtail_hooks.py wagtail/tests/urls.py wagtail/wagtaildocs/models.py wagtail/wagtaildocs/views/chooser.py wagtail/wagtaildocs/wagtail_hooks.py wagtail/wagtailembeds/wagtail_hooks.py wagtail/wagtailforms/models.py wagtail/wagtailforms/tests/test_views.py wagtail/wagtailforms/views.py wagtail/wagtailforms/wagtail_hooks.py wagtail/wagtailimages/models.py wagtail/wagtailimages/views/chooser.py wagtail/wagtailimages/wagtail_hooks.py wagtail/wagtailredirects/forms.py wagtail/wagtailredirects/wagtail_hooks.py wagtail/wagtailsites/forms.py wagtail/wagtailsites/views.py wagtail/wagtailsites/wagtail_hooks.py wagtail/wagtailsnippets/tests.py wagtail/wagtailsnippets/wagtail_hooks.py wagtail/wagtailusers/forms.py wagtail/wagtailusers/views/groups.py wagtail/wagtailusers/wagtail_hooks.py
2017-11-17 10:44:34 +00:00
'wagtail.admin.jinja2tags.userbar',
'wagtail.images.jinja2tags.images',
2015-10-01 05:52:04 +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``.
By default, the Jinja environment does not have any Django functions or filters. The Django documentation has more information on :class:`configuring Jinja for Django <django.template.backends.jinja2.Jinja2>`.
2015-10-01 05:52:04 +00:00
``self`` in templates
=====================
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
Template tags, functions & filters
==================================
2015-10-01 05:52:04 +00:00
``pageurl()``
~~~~~~~~~~~~~
Generate a URL for a Page instance:
.. code-block:: html+jinja
<a href="{{ pageurl(page.more_information) }}">More information</a>
See :ref:`pageurl_tag` for more information
``slugurl()``
~~~~~~~~~~~~~
Generate a URL for a Page with a slug:
.. code-block:: html+jinja
2015-10-08 15:59:58 +00:00
<a href="{{ slugurl("about") }}">About us</a>
2015-10-01 05:52:04 +00:00
See :ref:`slugurl_tag` for more information
``image()``
~~~~~~~~~~~
Resize an image, and print an ``<img>`` tag:
.. code-block:: html+jinja
{# Print an image tag #}
{{ image(page.header_image, "fill-1024x200", class="header-image") }}
{# Resize an image #}
{% set background=image(page.background_image, "max-1024x1024") %}
<div class="wrapper" style="background-image: url({{ background.url }});">
See :ref:`image_tag` for more information
``|richtext``
~~~~~~~~~~~~~
Transform Wagtail's internal HTML representation, expanding internal references to pages and images.
.. code-block:: html+jinja
{{ page.body|richtext }}
See :ref:`rich-text-filter` for more information
2022-01-19 18:34:41 +00:00
``wagtail_site``
~~~~~~~~~~~~~~~~
Returns the Site object corresponding to the current request.
.. code-block:: html+jinja
{{ wagtail_site().site_name }}
See :ref:`wagtail_site_tag` for more information
2015-10-01 05:52:04 +00:00
``wagtailuserbar()``
~~~~~~~~~~~~~~~~~~~~
Output the Wagtail contextual flyout menu for editing pages from the front end
.. code-block:: html+jinja
{{ wagtailuserbar() }}
See :ref:`wagtailuserbar_tag` for more information
``{% include_block %}``
~~~~~~~~~~~~~~~~~~~~~~~
Output the HTML representation for the stream content as a whole, as well as for each individual block.
Allows to pass template context (by default) to the StreamField template.
.. code-block:: html+jinja
{% include_block page.body %}
{% include_block page.body with context %} {# The same as the previous #}
{% include_block page.body without context %}
See :ref:`StreamField template rendering<streamfield_template_rendering>` for more information.
.. 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.