jinja2.rst 3.8 KB

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