blob: 1d25a2b9852e8ef9ba3e10f2134c286e753f0716 [file] [log] [blame]
Armin Ronachered98cac2008-05-07 08:42:11 +02001.. _jinja-extensions:
2
3Extensions
4==========
5
Armin Ronachered98cac2008-05-07 08:42:11 +02006Jinja2 supports extensions that can add extra filters, tests, globals or even
7extend the parser. The main motivation of extensions is it to move often used
8code into a reusable class like adding support for internationalization.
9
10
11Adding Extensions
12-----------------
13
14Extensions are added to the Jinja2 environment at creation time. Once the
15environment is created additional extensions cannot be added. To add an
16extension pass a list of extension classes or import paths to the
17`environment` parameter of the :class:`Environment` constructor. The following
18example creates a Jinja2 environment with the i18n extension loaded::
19
Armin Ronacher09c002e2008-05-10 22:21:30 +020020 jinja_env = Environment(extensions=['jinja2.ext.i18n'])
Armin Ronachered98cac2008-05-07 08:42:11 +020021
22
Armin Ronachered98cac2008-05-07 08:42:11 +020023.. _i18n-extension:
24
Armin Ronacher023b5e92008-05-08 11:03:10 +020025i18n Extension
26--------------
Armin Ronachered98cac2008-05-07 08:42:11 +020027
Armin Ronacher5d2733f2008-05-15 23:26:52 +020028**Import name:** `jinja2.ext.i18n`
29
Armin Ronacher023b5e92008-05-08 11:03:10 +020030Jinja2 currently comes with one extension, the i18n extension. It can be
31used in combination with `gettext`_ or `babel`_. If the i18n extension is
32enabled Jinja2 provides a `trans` statement that marks the wrapped string as
33translatable and calls `gettext`.
Armin Ronachered98cac2008-05-07 08:42:11 +020034
Armin Ronacher762079c2008-05-08 23:57:56 +020035After enabling dummy `_` function that forwards calls to `gettext` is added
36to the environment globals. An internationalized application then has to
37provide at least an `gettext` and optoinally a `ngettext` function into the
38namespace. Either globally or for each rendering.
39
40After enabling of the extension the environment provides the following
41additional methods:
42
43.. method:: jinja2.Environment.install_gettext_translations(translations)
44
45 Installs a translation globally for that environment. The tranlations
46 object provided must implement at least `ugettext` and `ungettext`.
47 The `gettext.NullTranslations` and `gettext.GNUTranslations` classes
48 as well as `Babel`_\s `Translations` class are supported.
49
50.. method:: jinja2.Environment.install_null_translations()
51
52 Install dummy gettext functions. This is useful if you want to prepare
53 the application for internationalization but don't want to implement the
54 full internationalization system yet.
55
56.. method:: jinja2.Environment.uninstall_gettext_translations()
57
58 Uninstall the translations again.
59
60.. method:: jinja2.Environment.extract_translations(source)
61
62 Extract localizable strings from the given template node or source.
63
64 For every string found this function yields a ``(lineno, function,
65 message)`` tuple, where:
66
67 * `lineno` is the number of the line on which the string was found,
68 * `function` is the name of the `gettext` function used (if the
69 string was extracted from embedded Python code), and
70 * `message` is the string itself (a `unicode` object, or a tuple
71 of `unicode` objects for functions with multiple string arguments).
72
73 If `Babel`_ is installed :ref:`the babel integration <babel-integration>`
74 can be used to extract strings for babel.
Armin Ronachered98cac2008-05-07 08:42:11 +020075
76For a web application that is available in multiple languages but gives all
77the users the same language (for example a multilingual forum software
78installed for a French community) may load the translations once and add the
79translation methods to the environment at environment generation time::
80
81 translations = get_gettext_translations()
Armin Ronacher2b228742008-05-18 20:29:32 +020082 env = Environment(extensions=['jinja2.ext.i18n'])
Armin Ronacher762079c2008-05-08 23:57:56 +020083 env.install_gettext_translations(translations)
Armin Ronachered98cac2008-05-07 08:42:11 +020084
85The `get_gettext_translations` function would return the translator for the
Armin Ronacher762079c2008-05-08 23:57:56 +020086current configuration. (For example by using `gettext.find`)
Armin Ronachered98cac2008-05-07 08:42:11 +020087
88The usage of the `i18n` extension for template designers is covered as part
89:ref:`of the template documentation <i18n-in-templates>`.
90
Armin Ronachered98cac2008-05-07 08:42:11 +020091.. _gettext: http://docs.python.org/dev/library/gettext
Armin Ronacher762079c2008-05-08 23:57:56 +020092.. _Babel: http://babel.edgewall.org/
Armin Ronachered98cac2008-05-07 08:42:11 +020093
Armin Ronacher023b5e92008-05-08 11:03:10 +020094
Armin Ronacher6df604e2008-05-23 22:18:38 +020095Expression Statement
96--------------------
Armin Ronacher5d2733f2008-05-15 23:26:52 +020097
98**Import name:** `jinja2.ext.do`
99
Armin Ronacher6df604e2008-05-23 22:18:38 +0200100The "do" aka expression-statement extension adds a simple `do` tag to the
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200101template engine that works like a variable expression but ignores the
102return value.
103
Armin Ronacher3da90312008-05-23 16:37:28 +0200104.. _loopcontrols-extension:
105
Armin Ronacher6df604e2008-05-23 22:18:38 +0200106Loop Controls
107-------------
Armin Ronacher3da90312008-05-23 16:37:28 +0200108
109**Import name:** `jinja2.ext.loopcontrols`
110
111This extension adds support for `break` and `continue` in loops. After
112enabling Jinja2 provides those two keywords which work exactly like in
113Python.
114
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200115
Armin Ronachered98cac2008-05-07 08:42:11 +0200116.. _writing-extensions:
117
118Writing Extensions
119------------------
120
Armin Ronacher762079c2008-05-08 23:57:56 +0200121.. module:: jinja2.ext
122
Armin Ronacher023b5e92008-05-08 11:03:10 +0200123By writing extensions you can add custom tags to Jinja2. This is a non trival
124task and usually not needed as the default tags and expressions cover all
125common use cases. The i18n extension is a good example of why extensions are
126useful, another one would be fragment caching.
127
Armin Ronacherb9e78752008-05-10 23:36:28 +0200128When writing extensions you have to keep in mind that you are working with the
129Jinja2 template compiler which does not validate the node tree you are possing
130to it. If the AST is malformed you will get all kinds of compiler or runtime
131errors that are horrible to debug. Always make sure you are using the nodes
132you create correctly. The API documentation below shows which nodes exist and
133how to use them.
134
Armin Ronacher023b5e92008-05-08 11:03:10 +0200135Example Extension
136~~~~~~~~~~~~~~~~~
137
Armin Ronacher762079c2008-05-08 23:57:56 +0200138The following example implements a `cache` tag for Jinja2 by using the
139`Werkzeug`_ caching contrib module:
Armin Ronacher023b5e92008-05-08 11:03:10 +0200140
141.. literalinclude:: cache_extension.py
142 :language: python
143
Armin Ronacher762079c2008-05-08 23:57:56 +0200144And here is how you use it in an environment::
Armin Ronacher023b5e92008-05-08 11:03:10 +0200145
146 from jinja2 import Environment
147 from werkzeug.contrib.cache import SimpleCache
148
Armin Ronacher762079c2008-05-08 23:57:56 +0200149 env = Environment(extensions=[FragmentCacheExtension])
150 env.fragment_cache = SimpleCache()
Armin Ronacher023b5e92008-05-08 11:03:10 +0200151
Armin Ronacher6df604e2008-05-23 22:18:38 +0200152Inside the template it's then possible to mark blocks as cacheable. The
153following example caches a sidebar for 300 seconds:
154
155.. sourcecode:: html+jinja
156
157 {% cache 'sidebar', 300 %}
158 <div class="sidebar">
159 ...
160 </div>
161 {% endcache %}
162
Armin Ronacher023b5e92008-05-08 11:03:10 +0200163.. _Werkzeug: http://werkzeug.pocoo.org/
164
165Extension API
166~~~~~~~~~~~~~
167
168Extensions always have to extend the :class:`jinja2.ext.Extension` class:
169
170.. autoclass:: Extension
Armin Ronacher9ad96e72008-06-13 22:44:01 +0200171 :members: preprocess, filter_stream, parse, attr, call_method
Armin Ronacher023b5e92008-05-08 11:03:10 +0200172
173 .. attribute:: identifier
174
175 The identifier of the extension. This is always the true import name
176 of the extension class and must not be changed.
177
178 .. attribute:: tags
179
180 If the extension implements custom tags this is a set of tag names
181 the extension is listening for.
182
183Parser API
184~~~~~~~~~~
185
186The parser passed to :meth:`Extension.parse` provides ways to parse
187expressions of different types. The following methods may be used by
188extensions:
189
190.. autoclass:: jinja2.parser.Parser
Armin Ronacher09c002e2008-05-10 22:21:30 +0200191 :members: parse_expression, parse_tuple, parse_assign_target,
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200192 parse_statements, free_identifier, fail
Armin Ronacher023b5e92008-05-08 11:03:10 +0200193
194 .. attribute:: filename
195
196 The filename of the template the parser processes. This is **not**
Armin Ronacher67fdddf2008-05-16 09:27:51 +0200197 the load name of the template. For the load name see :attr:`name`.
Armin Ronacher023b5e92008-05-08 11:03:10 +0200198 For templates that were not loaded form the file system this is
199 `None`.
200
Armin Ronacher67fdddf2008-05-16 09:27:51 +0200201 .. attribute:: name
202
203 The load name of the template.
204
Armin Ronacher023b5e92008-05-08 11:03:10 +0200205 .. attribute:: stream
206
207 The current :class:`~jinja2.lexer.TokenStream`
208
209.. autoclass:: jinja2.lexer.TokenStream
Armin Ronacherfdf95302008-05-11 22:20:51 +0200210 :members: push, look, eos, skip, next, next_if, skip_if, expect
Armin Ronacher023b5e92008-05-08 11:03:10 +0200211
212 .. attribute:: current
213
214 The current :class:`~jinja2.lexer.Token`.
215
216.. autoclass:: jinja2.lexer.Token
217 :members: test, test_any
218
219 .. attribute:: lineno
220
221 The line number of the token
222
223 .. attribute:: type
224
225 The type of the token. This string is interned so you may compare
226 it with arbitrary strings using the `is` operator.
227
228 .. attribute:: value
229
230 The value of the token.
231
Armin Ronacherd02fc7d2008-06-14 14:19:47 +0200232There is also a utility function in the lexer module that can count newline
233characters in strings:
234
235.. autofunction:: jinja2.lexer.count_newlines
236
Armin Ronacher023b5e92008-05-08 11:03:10 +0200237AST
238~~~
239
240The AST (Abstract Syntax Tree) is used to represent a template after parsing.
241It's build of nodes that the compiler then converts into executable Python
242code objects. Extensions that provide custom statements can return nodes to
243execute custom Python code.
244
245The list below describes all nodes that are currently available. The AST may
246change between Jinja2 versions but will stay backwards compatible.
247
248For more information have a look at the repr of :meth:`jinja2.Environment.parse`.
249
250.. module:: jinja2.nodes
251
252.. jinjanodes::
253
254.. autoexception:: Impossible