jinja2.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. .. _jinja2:
  2. =======================
  3. Jinja2 template support
  4. =======================
  5. 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.
  6. Configuring Django
  7. ==================
  8. 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:
  9. .. code-block:: python
  10. TEMPLATES = [
  11. {
  12. "BACKEND": "django.template.backends.django.DjangoTemplates",
  13. # ... the rest of the existing Django template configuration ...
  14. },
  15. {
  16. 'BACKEND': 'django.template.backends.jinja2.Jinja2',
  17. 'APP_DIRS': True,
  18. 'OPTIONS': {
  19. 'extensions': [
  20. 'wagtail.core.jinja2tags.core',
  21. 'wagtail.admin.jinja2tags.userbar',
  22. 'wagtail.images.jinja2tags.images',
  23. ],
  24. },
  25. }
  26. ]
  27. 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``.
  28. 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>`.
  29. ``self`` in templates
  30. =====================
  31. 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.
  32. Template tags, functions & filters
  33. ==================================
  34. ``pageurl()``
  35. ~~~~~~~~~~~~~
  36. Generate a URL for a Page instance:
  37. .. code-block:: html+jinja
  38. <a href="{{ pageurl(page.more_information) }}">More information</a>
  39. See :ref:`pageurl_tag` for more information
  40. ``slugurl()``
  41. ~~~~~~~~~~~~~
  42. Generate a URL for a Page with a slug:
  43. .. code-block:: html+jinja
  44. <a href="{{ slugurl("about") }}">About us</a>
  45. See :ref:`slugurl_tag` for more information
  46. ``image()``
  47. ~~~~~~~~~~~
  48. Resize an image, and print an ``<img>`` tag:
  49. .. code-block:: html+jinja
  50. {# Print an image tag #}
  51. {{ image(page.header_image, "fill-1024x200", class="header-image") }}
  52. {# Resize an image #}
  53. {% set background=image(page.background_image, "max-1024x1024") %}
  54. <div class="wrapper" style="background-image: url({{ background.url }});">
  55. See :ref:`image_tag` for more information
  56. ``|richtext``
  57. ~~~~~~~~~~~~~
  58. Transform Wagtail's internal HTML representation, expanding internal references to pages and images.
  59. .. code-block:: html+jinja
  60. {{ page.body|richtext }}
  61. See :ref:`rich-text-filter` for more information
  62. ``wagtailuserbar()``
  63. ~~~~~~~~~~~~~~~~~~~~
  64. Output the Wagtail contextual flyout menu for editing pages from the front end
  65. .. code-block:: html+jinja
  66. {{ wagtailuserbar() }}
  67. See :ref:`wagtailuserbar_tag` for more information
  68. ``{% include_block %}``
  69. ~~~~~~~~~~~~~~~~~~~~~~~
  70. Output the HTML representation for the stream content as a whole, as well as for each individual block.
  71. Allows to pass template context (by default) to the StreamField template.
  72. .. code-block:: html+jinja
  73. {% include_block page.body %}
  74. {% include_block page.body with context %} {# The same as the previous #}
  75. {% include_block page.body without context %}
  76. See :ref:`StreamField template rendering<streamfield_template_rendering>` for more information.
  77. .. note::
  78. The ``{% include_block %}`` tag is designed to closely follow the syntax and behaviour
  79. of Jinja's ``{% include %}``, so it does not implement the Django version's feature of
  80. only passing specified variables into the context.