2
0

jinja2.rst 4.1 KB

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