Debian Codegraph @

docs/integration.rst

raw
1 Integration
2 ===========
3
4
5 Flask
6 -----
7
8 The `Flask`_ web application framework, also maintained by Pallets, uses
9 Jinja templates by default. Flask sets up a Jinja environment and
10 template loader for you, and provides functions to easily render
11 templates from view functions.
12
13 .. _Flask: https://flask.palletsprojects.com
14
15
16 Django
17 ------
18
19 Django supports using Jinja as its template engine, see
20 https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.
21
22
23 .. _babel-integration:
24
25 Babel
26 -----
27
28 Jinja provides support for extracting gettext messages from templates
29 via a `Babel`_ extractor entry point called
30 ``jinja2.ext.babel_extract``. The support is implemented as part of the
31 :ref:`i18n-extension` extension.
32
33 Gettext messages are extracted from both ``trans`` tags and code
34 expressions.
35
36 To extract gettext messages from templates, the project needs a Jinja
37 section in its Babel extraction method `mapping file`_:
38
39 .. sourcecode:: ini
40
41 [jinja2: **/templates/**.html]
42 encoding = utf-8
43
44 The syntax related options of the :class:`Environment` are also
45 available as configuration values in the mapping file. For example, to
46 tell the extractor that templates use ``%`` as
47 ``line_statement_prefix`` you can use this code:
48
49 .. sourcecode:: ini
50
51 [jinja2: **/templates/**.html]
52 encoding = utf-8
53 line_statement_prefix = %
54
55 :ref:`jinja-extensions` may also be defined by passing a comma separated
56 list of import paths as the ``extensions`` value. The i18n extension is
57 added automatically.
58
59 Template syntax errors are ignored by default. The assumption is that
60 tests will catch syntax errors in templates. If you don't want to ignore
61 errors, add ``silent = false`` to the settings.
62
63 .. _Babel: https://babel.readthedocs.io/
64 .. _mapping file: https://babel.readthedocs.io/en/latest/messages.html#extraction-method-mapping-and-configuration
65
66
67 Pylons
68 ------
69
70 It's easy to integrate Jinja into a `Pylons`_ application.
71
72 The template engine is configured in ``config/environment.py``. The
73 configuration for Jinja looks something like this:
74
75 .. code-block:: python
76
77 from jinja2 import Environment, PackageLoader
78 config['pylons.app_globals'].jinja_env = Environment(
79 loader=PackageLoader('yourapplication', 'templates')
80 )
81
82 After that you can render Jinja templates by using the ``render_jinja``
83 function from the ``pylons.templating`` module.
84
85 Additionally it's a good idea to set the Pylons ``c`` object to strict
86 mode. By default attribute access on missing attributes on the ``c``
87 object returns an empty string and not an undefined object. To change
88 this add this to ``config/environment.py``:
89
90 .. code-block:: python
91
92 config['pylons.strict_c'] = True
93
94 .. _Pylons: https://pylonsproject.org/