Skip to content
Myr

integration.rst

This file

Path

docs/integration.rst

94 lines · 2.7 KiB

Last changed by

Merge branch '3.1.x'David Lordbefore adoption5d9ece6

History of this file

Take it

1Integration2===========345Flask6-----78The `Flask`_ web application framework, also maintained by Pallets, uses9Jinja templates by default. Flask sets up a Jinja environment and10template loader for you, and provides functions to easily render11templates from view functions.1213.. _Flask: https://flask.palletsprojects.com141516Django17------1819Django supports using Jinja as its template engine, see20https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.212223.. _babel-integration:2425Babel26-----2728Jinja provides support for extracting gettext messages from templates29via a `Babel`_ extractor entry point called30``jinja2.ext.babel_extract``. The support is implemented as part of the31:ref:`i18n-extension` extension.3233Gettext messages are extracted from both ``trans`` tags and code34expressions.3536To extract gettext messages from templates, the project needs a Jinja37section in its Babel extraction method `mapping file`_:3839.. sourcecode:: ini4041    [jinja2: **/templates/**.html]42    encoding = utf-84344The syntax related options of the :class:`Environment` are also45available as configuration values in the mapping file. For example, to46tell the extractor that templates use ``%`` as47``line_statement_prefix`` you can use this code:4849.. sourcecode:: ini5051    [jinja2: **/templates/**.html]52    encoding = utf-853    line_statement_prefix = %5455:ref:`jinja-extensions` may also be defined by passing a comma separated56list of import paths as the ``extensions`` value. The i18n extension is57added automatically.5859Template syntax errors are ignored by default. The assumption is that60tests will catch syntax errors in templates. If you don't want to ignore61errors, add ``silent = false`` to the settings.6263.. _Babel: https://babel.readthedocs.io/64.. _mapping file: https://babel.readthedocs.io/en/latest/messages.html#extraction-method-mapping-and-configuration656667Pylons68------6970It's easy to integrate Jinja into a `Pylons`_ application.7172The template engine is configured in ``config/environment.py``. The73configuration for Jinja looks something like this:7475.. code-block:: python7677    from jinja2 import Environment, PackageLoader78    config['pylons.app_globals'].jinja_env = Environment(79        loader=PackageLoader('yourapplication', 'templates')80    )8182After that you can render Jinja templates by using the ``render_jinja``83function from the ``pylons.templating`` module.8485Additionally it's a good idea to set the Pylons ``c`` object to strict86mode. By default attribute access on missing attributes on the ``c``87object returns an empty string and not an undefined object. To change88this add this to ``config/environment.py``:8990.. code-block:: python9192    config['pylons.strict_c'] = True9394.. _Pylons: https://pylonsproject.org/