blob: 871b326f519a60cbe852db5683a519f4c71193ff [file] [log] [blame]
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02001API
2===
3
David Lord4a59ac92020-01-26 21:12:52 -08004.. module:: jinja2
jaba811d862019-06-06 03:04:36 +00005 :noindex:
David Lord57626a02019-10-23 12:29:46 -07006 :synopsis: public Jinja API
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02007
David Lord57626a02019-10-23 12:29:46 -07008This document describes the API to Jinja and not the template language. It
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02009will be most useful as reference to those implementing the template interface
David Lord57626a02019-10-23 12:29:46 -070010to the application and not those who are creating Jinja templates.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020011
12Basics
13------
14
David Lord57626a02019-10-23 12:29:46 -070015Jinja uses a central object called the template :class:`Environment`.
Carl A Dunhamd5463582014-01-18 15:26:10 -060016Instances of this class are used to store the configuration and global objects,
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020017and are used to load templates from the file system or other locations.
Armin Ronacher0aa0f582009-03-18 01:01:36 +010018Even if you are creating templates from strings by using the constructor of
Armin Ronacher61a5a242008-05-26 12:07:44 +020019:class:`Template` class, an environment is created automatically for you,
20albeit a shared one.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020021
22Most applications will create one :class:`Environment` object on application
Jon Dufresne148b6fb2018-08-29 20:58:03 -070023initialization and use that to load templates. In some cases however, it's
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020024useful to have multiple environments side by side, if different configurations
25are in use.
26
David Lord57626a02019-10-23 12:29:46 -070027The simplest way to configure Jinja to load templates for your application
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020028looks roughly like this::
29
David Lord4a59ac92020-01-26 21:12:52 -080030 from jinja2 import Environment, PackageLoader, select_autoescape
Armin Ronacherb81a8a32017-01-07 16:13:39 +010031 env = Environment(
32 loader=PackageLoader('yourapplication', 'templates'),
33 autoescape=select_autoescape(['html', 'xml'])
34 )
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020035
36This will create a template environment with the default settings and a
37loader that looks up the templates in the `templates` folder inside the
38`yourapplication` python package. Different loaders are available
39and you can also write your own if you want to load templates from a
Armin Ronacherb81a8a32017-01-07 16:13:39 +010040database or other resources. This also enables autoescaping for HTML and
41XML files.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020042
43To load a template from this environment you just have to call the
44:meth:`get_template` method which then returns the loaded :class:`Template`::
45
46 template = env.get_template('mytemplate.html')
47
48To render it with some variables, just call the :meth:`render` method::
49
Deepak Amin4965fac2019-05-31 14:17:35 -040050 print(template.render(the='variables', go='here'))
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020051
Éric Araujof6b654d2015-05-29 16:57:10 -040052Using a template loader rather than passing strings to :class:`Template`
Armin Ronacher61a5a242008-05-26 12:07:44 +020053or :meth:`Environment.from_string` has multiple advantages. Besides being
54a lot easier to use it also enables template inheritance.
55
Armin Ronacherb81a8a32017-01-07 16:13:39 +010056.. admonition:: Notes on Autoescaping
57
David Lord57626a02019-10-23 12:29:46 -070058 In future versions of Jinja we might enable autoescaping by default
Armin Ronacherb81a8a32017-01-07 16:13:39 +010059 for security reasons. As such you are encouraged to explicitly
60 configure autoescaping now instead of relying on the default.
61
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020062
Armin Ronacherf3c35c42008-05-23 23:18:14 +020063Unicode
64-------
65
David Lord57626a02019-10-23 12:29:46 -070066Jinja is using Unicode internally which means that you have to pass Unicode
Armin Ronacherf3c35c42008-05-23 23:18:14 +020067objects to the render function or bytestrings that only consist of ASCII
68characters. Additionally newlines are normalized to one end of line
69sequence which is per default UNIX style (``\n``).
70
Armin Ronacher61a5a242008-05-26 12:07:44 +020071Python 2.x supports two ways of representing string objects. One is the
72`str` type and the other is the `unicode` type, both of which extend a type
73called `basestring`. Unfortunately the default is `str` which should not
74be used to store text based information unless only ASCII characters are
Armin Ronacher0aa0f582009-03-18 01:01:36 +010075used. With Python 2.6 it is possible to make `unicode` the default on a per
Armin Ronacher61a5a242008-05-26 12:07:44 +020076module level and with Python 3 it will be the default.
77
Armin Ronacher656d5e72010-02-09 01:31:47 +010078To explicitly use a Unicode string you have to prefix the string literal
Armin Ronacher61a5a242008-05-26 12:07:44 +020079with a `u`: ``u'Hänsel und Gretel sagen Hallo'``. That way Python will
Armin Ronacher656d5e72010-02-09 01:31:47 +010080store the string as Unicode by decoding the string with the character
Armin Ronacher61a5a242008-05-26 12:07:44 +020081encoding from the current Python module. If no encoding is specified this
82defaults to 'ASCII' which means that you can't use any non ASCII identifier.
83
84To set a better module encoding add the following comment to the first or
Armin Ronacher656d5e72010-02-09 01:31:47 +010085second line of the Python module using the Unicode literal::
Armin Ronacher61a5a242008-05-26 12:07:44 +020086
87 # -*- coding: utf-8 -*-
88
89We recommend utf-8 as Encoding for Python modules and templates as it's
90possible to represent every Unicode character in utf-8 and because it's
David Lord57626a02019-10-23 12:29:46 -070091backwards compatible to ASCII. For Jinja the default encoding of templates
Armin Ronacher61a5a242008-05-26 12:07:44 +020092is assumed to be utf-8.
93
David Lord57626a02019-10-23 12:29:46 -070094It is not possible to use Jinja to process non-Unicode data. The reason
95for this is that Jinja uses Unicode already on the language level. For
96example Jinja treats the non-breaking space as valid whitespace inside
Armin Ronacher61a5a242008-05-26 12:07:44 +020097expressions which requires knowledge of the encoding or operating on an
Armin Ronacher656d5e72010-02-09 01:31:47 +010098Unicode string.
Armin Ronacher61a5a242008-05-26 12:07:44 +020099
Armin Ronacher656d5e72010-02-09 01:31:47 +0100100For more details about Unicode in Python have a look at the excellent
Armin Ronacher61a5a242008-05-26 12:07:44 +0200101`Unicode documentation`_.
102
David Lord57626a02019-10-23 12:29:46 -0700103Another important thing is how Jinja is handling string literals in
Armin Ronacher656d5e72010-02-09 01:31:47 +0100104templates. A naive implementation would be using Unicode strings for
Armin Ronacher58f351d2008-05-28 21:30:14 +0200105all string literals but it turned out in the past that this is problematic
106as some libraries are typechecking against `str` explicitly. For example
Armin Ronacher656d5e72010-02-09 01:31:47 +0100107`datetime.strftime` does not accept Unicode arguments. To not break it
David Lord57626a02019-10-23 12:29:46 -0700108completely Jinja is returning `str` for strings that fit into ASCII and
Armin Ronacher58f351d2008-05-28 21:30:14 +0200109for everything else `unicode`:
110
111>>> m = Template(u"{% set a, b = 'foo', 'föö' %}").module
112>>> m.a
113'foo'
114>>> m.b
115u'f\xf6\xf6'
116
Armin Ronacher61a5a242008-05-26 12:07:44 +0200117
David Lord06696562019-07-26 12:12:41 -0700118.. _Unicode documentation: https://docs.python.org/3/howto/unicode.html
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200119
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200120High Level API
121--------------
122
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200123The high-level API is the API you will use in the application to load and
David Lord57626a02019-10-23 12:29:46 -0700124render Jinja templates. The :ref:`low-level-api` on the other side is only
125useful if you want to dig deeper into Jinja or :ref:`develop extensions
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200126<jinja-extensions>`.
127
Armin Ronacher5411ce72008-05-25 11:36:22 +0200128.. autoclass:: Environment([options])
Armin Ronacher31bbd9e2010-01-14 00:41:30 +0100129 :members: from_string, get_template, select_template,
Armin Ronacher46844982011-01-29 20:19:58 +0100130 get_or_select_template, join_path, extend, compile_expression,
Armin Ronacher94638502011-09-26 00:41:25 +0200131 compile_templates, list_templates, add_extension
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200132
133 .. attribute:: shared
134
135 If a template was created by using the :class:`Template` constructor
136 an environment is created automatically. These environments are
137 created as shared environments which means that multiple templates
138 may have the same anonymous environment. For all shared environments
139 this attribute is `True`, else `False`.
140
141 .. attribute:: sandboxed
142
143 If the environment is sandboxed this attribute is `True`. For the
144 sandbox mode have a look at the documentation for the
David Lord4a59ac92020-01-26 21:12:52 -0800145 :class:`~jinja2.sandbox.SandboxedEnvironment`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200146
147 .. attribute:: filters
148
149 A dict of filters for this environment. As long as no template was
Armin Ronacher7259c762008-04-30 13:03:59 +0200150 loaded it's safe to add new filters or remove old. For custom filters
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200151 see :ref:`writing-filters`. For valid filter names have a look at
152 :ref:`identifier-naming`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200153
154 .. attribute:: tests
155
Lukas Meuserad48a2e2008-05-01 18:19:57 +0200156 A dict of test functions for this environment. As long as no
157 template was loaded it's safe to modify this dict. For custom tests
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200158 see :ref:`writing-tests`. For valid test names have a look at
159 :ref:`identifier-naming`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200160
161 .. attribute:: globals
162
163 A dict of global variables. These variables are always available
Armin Ronacher981cbf62008-05-13 09:12:27 +0200164 in a template. As long as no template was loaded it's safe
Armin Ronacher7259c762008-04-30 13:03:59 +0200165 to modify this dict. For more details see :ref:`global-namespace`.
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200166 For valid object names have a look at :ref:`identifier-naming`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200167
Armin Ronachere2535202016-12-31 00:43:50 +0100168 .. attribute:: policies
169
170 A dictionary with :ref:`policies`. These can be reconfigured to
171 change the runtime behavior or certain template features. Usually
172 these are security related.
173
ThiefMaster14936312015-04-06 13:54:14 +0200174 .. attribute:: code_generator_class
175
176 The class used for code generation. This should not be changed
177 in most cases, unless you need to modify the Python code a
178 template compiles to.
179
ThiefMasterf22fdd52015-04-06 14:08:46 +0200180 .. attribute:: context_class
181
182 The context used for templates. This should not be changed
183 in most cases, unless you need to modify internals of how
184 template variables are handled. For details, see
David Lord4a59ac92020-01-26 21:12:52 -0800185 :class:`~jinja2.runtime.Context`.
ThiefMasterf22fdd52015-04-06 14:08:46 +0200186
Armin Ronachered98cac2008-05-07 08:42:11 +0200187 .. automethod:: overlay([options])
188
Armin Ronacher58f351d2008-05-28 21:30:14 +0200189 .. method:: undefined([hint, obj, name, exc])
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200190
Armin Ronacher5411ce72008-05-25 11:36:22 +0200191 Creates a new :class:`Undefined` object for `name`. This is useful
192 for filters or functions that may return undefined objects for
193 some operations. All parameters except of `hint` should be provided
194 as keyword parameters for better readability. The `hint` is used as
195 error message for the exception if provided, otherwise the error
Armin Ronacher0aa0f582009-03-18 01:01:36 +0100196 message will be generated from `obj` and `name` automatically. The exception
Armin Ronacher5411ce72008-05-25 11:36:22 +0200197 provided as `exc` is raised if something with the generated undefined
198 object is done that the undefined object does not allow. The default
199 exception is :exc:`UndefinedError`. If a `hint` is provided the
Alex Chan972c0302015-04-05 22:42:34 +0100200 `name` may be omitted.
Armin Ronacher5411ce72008-05-25 11:36:22 +0200201
202 The most common way to create an undefined object is by providing
203 a name only::
204
205 return environment.undefined(name='some_name')
206
207 This means that the name `some_name` is not defined. If the name
208 was from an attribute of an object it makes sense to tell the
209 undefined object the holder object to improve the error message::
210
211 if not hasattr(obj, 'attr'):
212 return environment.undefined(obj=obj, name='attr')
213
214 For a more complex example you can provide a hint. For example
215 the :func:`first` filter creates an undefined object that way::
216
Jon Dufresne148b6fb2018-08-29 20:58:03 -0700217 return environment.undefined('no first item, sequence was empty')
Armin Ronacher5411ce72008-05-25 11:36:22 +0200218
219 If it the `name` or `obj` is known (for example because an attribute
Alex Chan972c0302015-04-05 22:42:34 +0100220 was accessed) it should be passed to the undefined object, even if
Armin Ronacher5411ce72008-05-25 11:36:22 +0200221 a custom `hint` is provided. This gives undefined objects the
222 possibility to enhance the error message.
223
224.. autoclass:: Template
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200225 :members: module, make_module
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200226
Armin Ronacher7259c762008-04-30 13:03:59 +0200227 .. attribute:: globals
228
Armin Ronachered98cac2008-05-07 08:42:11 +0200229 The dict with the globals of that template. It's unsafe to modify
230 this dict as it may be shared with other templates or the environment
231 that loaded the template.
Armin Ronacher7259c762008-04-30 13:03:59 +0200232
233 .. attribute:: name
234
Armin Ronachered98cac2008-05-07 08:42:11 +0200235 The loading name of the template. If the template was loaded from a
236 string this is `None`.
237
Armin Ronacher5411ce72008-05-25 11:36:22 +0200238 .. attribute:: filename
239
240 The filename of the template on the file system if it was loaded from
241 there. Otherwise this is `None`.
242
Armin Ronachered98cac2008-05-07 08:42:11 +0200243 .. automethod:: render([context])
244
245 .. automethod:: generate([context])
246
247 .. automethod:: stream([context])
Armin Ronacher7259c762008-04-30 13:03:59 +0200248
Armin Ronacherd8326d92016-12-28 22:51:46 +0100249 .. automethod:: render_async([context])
250
251 .. automethod:: generate_async([context])
252
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200253
David Lord4a59ac92020-01-26 21:12:52 -0800254.. autoclass:: jinja2.environment.TemplateStream()
Armin Ronacher74b51062008-06-17 11:28:59 +0200255 :members: disable_buffering, enable_buffering, dump
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200256
257
Armin Ronacher1da23d12010-04-05 18:11:18 +0200258Autoescaping
259------------
260
Armin Ronachera27a5032017-01-07 15:55:20 +0100261.. versionchanged:: 2.4
Armin Ronacher1da23d12010-04-05 18:11:18 +0200262
David Lord57626a02019-10-23 12:29:46 -0700263Jinja now comes with autoescaping support. As of Jinja 2.9 the
Armin Ronachera27a5032017-01-07 15:55:20 +0100264autoescape extension is removed and built-in. However autoescaping is
Armin Ronacherb81a8a32017-01-07 16:13:39 +0100265not yet enabled by default though this will most likely change in the
266future. It's recommended to configure a sensible default for
267autoescaping. This makes it possible to enable and disable autoescaping
268on a per-template basis (HTML versus text for instance).
269
David Lord4a59ac92020-01-26 21:12:52 -0800270.. autofunction:: jinja2.select_autoescape
Armin Ronacher1da23d12010-04-05 18:11:18 +0200271
272Here a recommended setup that enables autoescaping for templates ending
273in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default
David Lord4a59ac92020-01-26 21:12:52 -0800274for all other extensions. You can use the :func:`~jinja2.select_autoescape`
Armin Ronacherb81a8a32017-01-07 16:13:39 +0100275function for this::
Armin Ronacher1da23d12010-04-05 18:11:18 +0200276
David Lord4a59ac92020-01-26 21:12:52 -0800277 from jinja2 import Environment, select_autoescape
Armin Ronacherb81a8a32017-01-07 16:13:39 +0100278 env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']),
Armin Ronachera27a5032017-01-07 15:55:20 +0100279 loader=PackageLoader('mypackage'))
Armin Ronacher1da23d12010-04-05 18:11:18 +0200280
Armin Ronacherb81a8a32017-01-07 16:13:39 +0100281The :func:`~jinja.select_autoescape` function returns a function that
Unknown778ccb22017-11-08 20:02:28 -0500282works roughly like this::
Armin Ronacherb81a8a32017-01-07 16:13:39 +0100283
284 def autoescape(template_name):
285 if template_name is None:
286 return False
287 if template_name.endswith(('.html', '.htm', '.xml'))
288
Armin Ronacher1da23d12010-04-05 18:11:18 +0200289When implementing a guessing autoescape function, make sure you also
290accept `None` as valid template name. This will be passed when generating
Armin Ronacherb81a8a32017-01-07 16:13:39 +0100291templates from strings. You should always configure autoescaping as
292defaults in the future might change.
Armin Ronacher1da23d12010-04-05 18:11:18 +0200293
294Inside the templates the behaviour can be temporarily changed by using
295the `autoescape` block (see :ref:`autoescape-overrides`).
296
297
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200298.. _identifier-naming:
299
300Notes on Identifiers
Armin Ronacher5411ce72008-05-25 11:36:22 +0200301--------------------
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200302
David Lord57626a02019-10-23 12:29:46 -0700303Jinja uses Python naming rules. Valid identifiers can be any combination
304of Unicode characters accepted by Python.
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200305
306Filters and tests are looked up in separate namespaces and have slightly
307modified identifier syntax. Filters and tests may contain dots to group
308filters and tests by topic. For example it's perfectly valid to add a
309function into the filter dict and call it `to.unicode`. The regular
310expression for filter and test identifiers is
311``[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*```.
312
313
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200314Undefined Types
315---------------
316
317These classes can be used as undefined types. The :class:`Environment`
318constructor takes an `undefined` parameter that can be one of those classes
319or a custom subclass of :class:`Undefined`. Whenever the template engine is
320unable to look up a name or access an attribute one of those objects is
321created and returned. Some operations on undefined values are then allowed,
322others fail.
323
Étienne Pelletier19133d42019-05-08 10:47:33 -0400324The closest to regular Python behavior is the :class:`StrictUndefined` which
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200325disallows all operations beside testing if it's an undefined object.
326
David Lord4a59ac92020-01-26 21:12:52 -0800327.. autoclass:: jinja2.Undefined()
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200328
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200329 .. attribute:: _undefined_hint
330
331 Either `None` or an unicode string with the error message for
332 the undefined object.
333
334 .. attribute:: _undefined_obj
335
336 Either `None` or the owner object that caused the undefined object
337 to be created (for example because an attribute does not exist).
338
339 .. attribute:: _undefined_name
340
341 The name for the undefined variable / attribute or just `None`
342 if no such information exists.
343
344 .. attribute:: _undefined_exception
345
346 The exception that the undefined object wants to raise. This
347 is usually one of :exc:`UndefinedError` or :exc:`SecurityError`.
348
349 .. method:: _fail_with_undefined_error(\*args, \**kwargs)
350
351 When called with any arguments this method raises
352 :attr:`_undefined_exception` with an error message generated
353 from the undefined hints stored on the undefined object.
354
David Lord4a59ac92020-01-26 21:12:52 -0800355.. autoclass:: jinja2.ChainableUndefined()
Étienne Pelletier19133d42019-05-08 10:47:33 -0400356
David Lord4a59ac92020-01-26 21:12:52 -0800357.. autoclass:: jinja2.DebugUndefined()
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200358
David Lord4a59ac92020-01-26 21:12:52 -0800359.. autoclass:: jinja2.StrictUndefined()
Armin Ronacher5411ce72008-05-25 11:36:22 +0200360
Armin Ronacher6e9dfbf2014-06-06 22:14:45 +0600361There is also a factory function that can decorate undefined objects to
362implement logging on failures:
363
David Lord4a59ac92020-01-26 21:12:52 -0800364.. autofunction:: jinja2.make_logging_undefined
Armin Ronacher6e9dfbf2014-06-06 22:14:45 +0600365
Armin Ronacher5411ce72008-05-25 11:36:22 +0200366Undefined objects are created by calling :attr:`undefined`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200367
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200368.. admonition:: Implementation
369
370 :class:`Undefined` objects are implemented by overriding the special
371 `__underscore__` methods. For example the default :class:`Undefined`
372 class implements `__unicode__` in a way that it returns an empty
373 string, however `__int__` and others still fail with an exception. To
374 allow conversion to int by returning ``0`` you can implement your own::
375
376 class NullUndefined(Undefined):
377 def __int__(self):
378 return 0
379 def __float__(self):
380 return 0.0
381
382 To disallow a method, just override it and raise
Armin Ronacher58f351d2008-05-28 21:30:14 +0200383 :attr:`~Undefined._undefined_exception`. Because this is a very common
Ruben Garciaa9d557f2019-02-08 11:18:06 +0100384 idiom in undefined objects there is the helper method
Armin Ronacher58f351d2008-05-28 21:30:14 +0200385 :meth:`~Undefined._fail_with_undefined_error` that does the error raising
386 automatically. Here a class that works like the regular :class:`Undefined`
387 but chokes on iteration::
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200388
389 class NonIterableUndefined(Undefined):
390 __iter__ = Undefined._fail_with_undefined_error
391
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200392
Armin Ronacher7259c762008-04-30 13:03:59 +0200393The Context
394-----------
395
David Lord4a59ac92020-01-26 21:12:52 -0800396.. autoclass:: jinja2.runtime.Context()
Armin Ronacherf35e2812008-05-06 16:04:10 +0200397 :members: resolve, get_exported, get_all
Armin Ronacher7259c762008-04-30 13:03:59 +0200398
399 .. attribute:: parent
400
401 A dict of read only, global variables the template looks up. These
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200402 can either come from another :class:`Context`, from the
Armin Ronacher5411ce72008-05-25 11:36:22 +0200403 :attr:`Environment.globals` or :attr:`Template.globals` or points
404 to a dict created by combining the globals with the variables
405 passed to the render function. It must not be altered.
Armin Ronacher7259c762008-04-30 13:03:59 +0200406
407 .. attribute:: vars
408
409 The template local variables. This list contains environment and
410 context functions from the :attr:`parent` scope as well as local
411 modifications and exported variables from the template. The template
412 will modify this dict during template evaluation but filters and
413 context functions are not allowed to modify it.
414
415 .. attribute:: environment
416
417 The environment that loaded the template.
418
419 .. attribute:: exported_vars
420
421 This set contains all the names the template exports. The values for
422 the names are in the :attr:`vars` dict. In order to get a copy of the
423 exported variables as dict, :meth:`get_exported` can be used.
424
425 .. attribute:: name
426
427 The load name of the template owning this context.
428
429 .. attribute:: blocks
430
431 A dict with the current mapping of blocks in the template. The keys
432 in this dict are the names of the blocks, and the values a list of
433 blocks registered. The last item in each list is the current active
434 block (latest in the inheritance chain).
435
Armin Ronacherfe150f32010-03-15 02:42:41 +0100436 .. attribute:: eval_ctx
437
438 The current :ref:`eval-context`.
439
David Lord4a59ac92020-01-26 21:12:52 -0800440 .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs)
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200441
442
443.. admonition:: Implementation
444
445 Context is immutable for the same reason Python's frame locals are
David Lord57626a02019-10-23 12:29:46 -0700446 immutable inside functions. Both Jinja and Python are not using the
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200447 context / frame locals as data storage for variables but only as primary
448 data source.
449
David Lord57626a02019-10-23 12:29:46 -0700450 When a template accesses a variable the template does not define, Jinja
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200451 looks up the variable in the context, after that the variable is treated
452 as if it was defined in the template.
453
Armin Ronacher7259c762008-04-30 13:03:59 +0200454
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200455.. _loaders:
456
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200457Loaders
458-------
459
460Loaders are responsible for loading templates from a resource such as the
Armin Ronacher7259c762008-04-30 13:03:59 +0200461file system. The environment will keep the compiled modules in memory like
462Python's `sys.modules`. Unlike `sys.modules` however this cache is limited in
463size by default and templates are automatically reloaded.
Armin Ronachercda43df2008-05-03 17:10:05 +0200464All loaders are subclasses of :class:`BaseLoader`. If you want to create your
Armin Ronachercda43df2008-05-03 17:10:05 +0200465own loader, subclass :class:`BaseLoader` and override `get_source`.
466
David Lord4a59ac92020-01-26 21:12:52 -0800467.. autoclass:: jinja2.BaseLoader
Armin Ronachercda43df2008-05-03 17:10:05 +0200468 :members: get_source, load
469
David Lord57626a02019-10-23 12:29:46 -0700470Here a list of the builtin loaders Jinja provides:
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200471
David Lord4a59ac92020-01-26 21:12:52 -0800472.. autoclass:: jinja2.FileSystemLoader
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200473
David Lord4a59ac92020-01-26 21:12:52 -0800474.. autoclass:: jinja2.PackageLoader
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200475
David Lord4a59ac92020-01-26 21:12:52 -0800476.. autoclass:: jinja2.DictLoader
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200477
David Lord4a59ac92020-01-26 21:12:52 -0800478.. autoclass:: jinja2.FunctionLoader
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200479
David Lord4a59ac92020-01-26 21:12:52 -0800480.. autoclass:: jinja2.PrefixLoader
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200481
David Lord4a59ac92020-01-26 21:12:52 -0800482.. autoclass:: jinja2.ChoiceLoader
Armin Ronachera816bf42008-09-17 21:28:01 +0200483
David Lord4a59ac92020-01-26 21:12:52 -0800484.. autoclass:: jinja2.ModuleLoader
Armin Ronacher46844982011-01-29 20:19:58 +0100485
Armin Ronachera816bf42008-09-17 21:28:01 +0200486
487.. _bytecode-cache:
488
489Bytecode Cache
490--------------
491
492Jinja 2.1 and higher support external bytecode caching. Bytecode caches make
493it possible to store the generated bytecode on the file system or a different
494location to avoid parsing the templates on first use.
495
496This is especially useful if you have a web application that is initialized on
497the first request and Jinja compiles many templates at once which slows down
498the application.
499
Jakub Wilk3fc008b2013-05-25 23:37:34 +0200500To use a bytecode cache, instantiate it and pass it to the :class:`Environment`.
Armin Ronachera816bf42008-09-17 21:28:01 +0200501
David Lord4a59ac92020-01-26 21:12:52 -0800502.. autoclass:: jinja2.BytecodeCache
Armin Ronachera816bf42008-09-17 21:28:01 +0200503 :members: load_bytecode, dump_bytecode, clear
504
David Lord4a59ac92020-01-26 21:12:52 -0800505.. autoclass:: jinja2.bccache.Bucket
Armin Ronachera816bf42008-09-17 21:28:01 +0200506 :members: write_bytecode, load_bytecode, bytecode_from_string,
507 bytecode_to_string, reset
508
509 .. attribute:: environment
510
511 The :class:`Environment` that created the bucket.
512
513 .. attribute:: key
514
515 The unique cache key for this bucket
516
517 .. attribute:: code
518
519 The bytecode if it's loaded, otherwise `None`.
520
521
522Builtin bytecode caches:
523
David Lord4a59ac92020-01-26 21:12:52 -0800524.. autoclass:: jinja2.FileSystemBytecodeCache
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200525
David Lord4a59ac92020-01-26 21:12:52 -0800526.. autoclass:: jinja2.MemcachedBytecodeCache
Armin Ronacheraa1d17d2008-09-18 18:09:06 +0200527
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200528
Armin Ronacherd8326d92016-12-28 22:51:46 +0100529Async Support
530-------------
531
David Lord57626a02019-10-23 12:29:46 -0700532Starting with version 2.9, Jinja also supports the Python `async` and
Armin Ronacherd8326d92016-12-28 22:51:46 +0100533`await` constructs. As far as template designers go this feature is
534entirely opaque to them however as a developer you should be aware of how
535it's implemented as it influences what type of APIs you can safely expose
536to the template environment.
537
538First you need to be aware that by default async support is disabled as
539enabling it will generate different template code behind the scenes which
540passes everything through the asyncio event loop. This is important to
541understand because it has some impact to what you are doing:
542
543* template rendering will require an event loop to be set for the
544 current thread (``asyncio.get_event_loop`` needs to return one)
545* all template generation code internally runs async generators which
546 means that you will pay a performance penalty even if the non sync
547 methods are used!
548* The sync methods are based on async methods if the async mode is
549 enabled which means that `render` for instance will internally invoke
550 `render_async` and run it as part of the current event loop until the
551 execution finished.
552
553Awaitable objects can be returned from functions in templates and any
554function call in a template will automatically await the result. This
nwalsh199565337f82018-04-17 14:55:13 -0700555means that you can provide a method that asynchronously loads data
Armin Ronacherd8326d92016-12-28 22:51:46 +0100556from a database if you so desire and from the template designer's point of
557view this is just another function they can call. This means that the
558``await`` you would normally issue in Python is implied. However this
559only applies to function calls. If an attribute for instance would be an
Frank Sachsenheimd0f88112018-05-11 21:25:22 +0200560awaitable object then this would not result in the expected behavior.
Armin Ronacherd8326d92016-12-28 22:51:46 +0100561
562Likewise iterations with a `for` loop support async iterators.
563
Armin Ronachere2535202016-12-31 00:43:50 +0100564.. _policies:
565
566Policies
567--------
568
569Starting with Jinja 2.9 policies can be configured on the environment
570which can slightly influence how filters and other template constructs
571behave. They can be configured with the
David Lord4a59ac92020-01-26 21:12:52 -0800572:attr:`~jinja2.Environment.policies` attribute.
Armin Ronachere2535202016-12-31 00:43:50 +0100573
574Example::
575
576 env.policies['urlize.rel'] = 'nofollow noopener'
577
Armin Ronacher028f0582017-01-07 14:57:44 +0100578``compiler.ascii_str``:
David Lord57626a02019-10-23 12:29:46 -0700579 This boolean controls on Python 2 if Jinja should store ASCII only
Armin Ronacher028f0582017-01-07 14:57:44 +0100580 literals as bytestring instead of unicode strings. This used to be
581 always enabled for Jinja versions below 2.9 and now can be changed.
582 Traditionally it was done this way since some APIs in Python 2 failed
583 badly for unicode strings (for instance the datetime strftime API).
584 Now however sometimes the inverse is true (for instance str.format).
585 If this is set to False then all strings are stored as unicode
586 internally.
587
Armin Ronacherfb47dfa2017-01-10 09:21:14 +0100588``truncate.leeway``:
589 Configures the leeway default for the `truncate` filter. Leeway as
590 introduced in 2.9 but to restore compatibility with older templates
591 it can be configured to `0` to get the old behavior back. The default
592 is `5`.
593
Armin Ronachere2535202016-12-31 00:43:50 +0100594``urlize.rel``:
595 A string that defines the items for the `rel` attribute of generated
596 links with the `urlize` filter. These items are always added. The
597 default is `noopener`.
598
599``urlize.target``:
600 The default target that is issued for links from the `urlize` filter
601 if no other target is defined by the call explicitly.
602
Armin Ronachere71a1302017-01-06 21:33:51 +0100603``json.dumps_function``:
604 If this is set to a value other than `None` then the `tojson` filter
605 will dump with this function instead of the default one. Note that
606 this function should accept arbitrary extra arguments which might be
607 passed in the future from the filter. Currently the only argument
608 that might be passed is `indent`. The default dump function is
609 ``json.dumps``.
610
611``json.dumps_kwargs``:
612 Keyword arguments to be passed to the dump function. The default is
613 ``{'sort_keys': True}``.
614
Adrian Moenniche605ff12017-02-17 23:49:39 +0100615.. _ext-i18n-trimmed:
616
617``ext.i18n.trimmed``:
618 If this is set to `True`, ``{% trans %}`` blocks of the
619 :ref:`i18n-extension` will always unify linebreaks and surrounding
620 whitespace as if the `trimmed` modifier was used.
621
Armin Ronacherd8326d92016-12-28 22:51:46 +0100622
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200623Utilities
624---------
625
626These helper functions and classes are useful if you add custom filters or
David Lord57626a02019-10-23 12:29:46 -0700627functions to a Jinja environment.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200628
David Lord4a59ac92020-01-26 21:12:52 -0800629.. autofunction:: jinja2.environmentfilter
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200630
David Lord4a59ac92020-01-26 21:12:52 -0800631.. autofunction:: jinja2.contextfilter
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200632
David Lord4a59ac92020-01-26 21:12:52 -0800633.. autofunction:: jinja2.evalcontextfilter
Armin Ronacherfe150f32010-03-15 02:42:41 +0100634
David Lord4a59ac92020-01-26 21:12:52 -0800635.. autofunction:: jinja2.environmentfunction
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200636
David Lord4a59ac92020-01-26 21:12:52 -0800637.. autofunction:: jinja2.contextfunction
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200638
David Lord4a59ac92020-01-26 21:12:52 -0800639.. autofunction:: jinja2.evalcontextfunction
Armin Ronacherfe150f32010-03-15 02:42:41 +0100640
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200641.. function:: escape(s)
642
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200643 Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s`
644 to HTML-safe sequences. Use this if you need to display text that might
645 contain such characters in HTML. This function will not escaped objects
646 that do have an HTML representation such as already escaped data.
647
648 The return value is a :class:`Markup` string.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200649
David Lord4a59ac92020-01-26 21:12:52 -0800650.. autofunction:: jinja2.clear_caches
Armin Ronacher187bde12008-05-01 18:19:16 +0200651
David Lord4a59ac92020-01-26 21:12:52 -0800652.. autofunction:: jinja2.is_undefined
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200653
David Lord4a59ac92020-01-26 21:12:52 -0800654.. autoclass:: jinja2.Markup([string])
Armin Ronacher58f351d2008-05-28 21:30:14 +0200655 :members: escape, unescape, striptags
656
657.. admonition:: Note
658
David Lord57626a02019-10-23 12:29:46 -0700659 The Jinja :class:`Markup` class is compatible with at least Pylons and
Armin Ronacher58f351d2008-05-28 21:30:14 +0200660 Genshi. It's expected that more template engines and framework will pick
661 up the `__html__` concept soon.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200662
663
664Exceptions
665----------
666
David Lord4a59ac92020-01-26 21:12:52 -0800667.. autoexception:: jinja2.TemplateError
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200668
David Lord4a59ac92020-01-26 21:12:52 -0800669.. autoexception:: jinja2.UndefinedError
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200670
David Lord4a59ac92020-01-26 21:12:52 -0800671.. autoexception:: jinja2.TemplateNotFound
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200672
David Lord4a59ac92020-01-26 21:12:52 -0800673.. autoexception:: jinja2.TemplatesNotFound
Armin Ronacher31bbd9e2010-01-14 00:41:30 +0100674
David Lord4a59ac92020-01-26 21:12:52 -0800675.. autoexception:: jinja2.TemplateSyntaxError
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200676
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200677 .. attribute:: message
678
679 The error message as utf-8 bytestring.
680
681 .. attribute:: lineno
682
683 The line number where the error occurred
684
685 .. attribute:: name
686
687 The load name for the template as unicode string.
688
689 .. attribute:: filename
690
691 The filename that loaded the template as bytestring in the encoding
692 of the file system (most likely utf-8 or mbcs on Windows systems).
693
694 The reason why the filename and error message are bytestrings and not
695 unicode strings is that Python 2.x is not using unicode for exceptions
696 and tracebacks as well as the compiler. This will change with Python 3.
697
David Lord4a59ac92020-01-26 21:12:52 -0800698.. autoexception:: jinja2.TemplateRuntimeError
Adrian Moennichcc1d2872017-02-26 18:00:06 +0100699
David Lord4a59ac92020-01-26 21:12:52 -0800700.. autoexception:: jinja2.TemplateAssertionError
Armin Ronacher7259c762008-04-30 13:03:59 +0200701
702
703.. _writing-filters:
704
705Custom Filters
706--------------
707
708Custom filters are just regular Python functions that take the left side of
Guillaume Paumier345e0ba2016-04-10 08:58:06 -0700709the filter as first argument and the arguments passed to the filter as
Armin Ronacher7259c762008-04-30 13:03:59 +0200710extra arguments or keyword arguments.
711
712For example in the filter ``{{ 42|myfilter(23) }}`` the function would be
713called with ``myfilter(42, 23)``. Here for example a simple filter that can
714be applied to datetime objects to format them::
715
716 def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
717 return value.strftime(format)
718
719You can register it on the template environment by updating the
720:attr:`~Environment.filters` dict on the environment::
721
722 environment.filters['datetimeformat'] = datetimeformat
723
724Inside the template it can then be used as follows:
725
726.. sourcecode:: jinja
727
728 written on: {{ article.pub_date|datetimeformat }}
729 publication date: {{ article.pub_date|datetimeformat('%d-%m-%Y') }}
730
731Filters can also be passed the current template context or environment. This
Armin Ronacher0aa0f582009-03-18 01:01:36 +0100732is useful if a filter wants to return an undefined value or check the current
Armin Ronacher2e3c9c72010-04-10 13:03:46 +0200733:attr:`~Environment.autoescape` setting. For this purpose three decorators
Armin Ronacherfe150f32010-03-15 02:42:41 +0100734exist: :func:`environmentfilter`, :func:`contextfilter` and
735:func:`evalcontextfilter`.
Armin Ronacher7259c762008-04-30 13:03:59 +0200736
737Here a small example filter that breaks a text into HTML line breaks and
738paragraphs and marks the return value as safe HTML string if autoescaping is
739enabled::
740
741 import re
David Lord4a59ac92020-01-26 21:12:52 -0800742 from jinja2 import evalcontextfilter, Markup, escape
Armin Ronacher7259c762008-04-30 13:03:59 +0200743
Mark Amery9e410c72018-11-25 17:49:22 +0000744 _paragraph_re = re.compile(r'(?:\r\n|\r(?!\n)|\n){2,}')
Armin Ronacher7259c762008-04-30 13:03:59 +0200745
Armin Ronacherfe150f32010-03-15 02:42:41 +0100746 @evalcontextfilter
747 def nl2br(eval_ctx, value):
Jörn Hees17024512014-06-15 18:31:16 +0200748 result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br>\n'))
Armin Ronacher7259c762008-04-30 13:03:59 +0200749 for p in _paragraph_re.split(escape(value)))
Armin Ronacherfe150f32010-03-15 02:42:41 +0100750 if eval_ctx.autoescape:
Armin Ronacher7259c762008-04-30 13:03:59 +0200751 result = Markup(result)
752 return result
753
754Context filters work the same just that the first argument is the current
Daniel van Flymen96f52e62017-03-01 14:10:34 -0500755active :class:`Context` rather than the environment.
Armin Ronacher7259c762008-04-30 13:03:59 +0200756
757
Armin Ronacherfe150f32010-03-15 02:42:41 +0100758.. _eval-context:
759
760Evaluation Context
761------------------
762
763The evaluation context (short eval context or eval ctx) is a new object
Jakub Wilk3fc008b2013-05-25 23:37:34 +0200764introduced in Jinja 2.4 that makes it possible to activate and deactivate
Armin Ronacherfe150f32010-03-15 02:42:41 +0100765compiled features at runtime.
766
767Currently it is only used to enable and disable the automatic escaping but
768can be used for extensions as well.
769
770In previous Jinja versions filters and functions were marked as
771environment callables in order to check for the autoescape status from the
772environment. In new versions it's encouraged to check the setting from the
773evaluation context instead.
774
775Previous versions::
776
777 @environmentfilter
778 def filter(env, value):
779 result = do_something(value)
780 if env.autoescape:
781 result = Markup(result)
782 return result
783
784In new versions you can either use a :func:`contextfilter` and access the
785evaluation context from the actual context, or use a
786:func:`evalcontextfilter` which directly passes the evaluation context to
787the function::
788
789 @contextfilter
790 def filter(context, value):
791 result = do_something(value)
792 if context.eval_ctx.autoescape:
793 result = Markup(result)
794 return result
795
796 @evalcontextfilter
797 def filter(eval_ctx, value):
798 result = do_something(value)
799 if eval_ctx.autoescape:
800 result = Markup(result)
801 return result
802
803The evaluation context must not be modified at runtime. Modifications
804must only happen with a :class:`nodes.EvalContextModifier` and
805:class:`nodes.ScopedEvalContextModifier` from an extension, not on the
806eval context object itself.
807
David Lord4a59ac92020-01-26 21:12:52 -0800808.. autoclass:: jinja2.nodes.EvalContext
Armin Ronacher30fda272010-03-15 03:06:04 +0100809
810 .. attribute:: autoescape
811
812 `True` or `False` depending on if autoescaping is active or not.
813
814 .. attribute:: volatile
815
816 `True` if the compiler cannot evaluate some expressions at compile
817 time. At runtime this should always be `False`.
818
819
Armin Ronacher7259c762008-04-30 13:03:59 +0200820.. _writing-tests:
821
822Custom Tests
823------------
824
Armin Ronachera5d8f552008-09-11 20:46:34 +0200825Tests work like filters just that there is no way for a test to get access
Armin Ronacher7259c762008-04-30 13:03:59 +0200826to the environment or context and that they can't be chained. The return
Armin Ronachera5d8f552008-09-11 20:46:34 +0200827value of a test should be `True` or `False`. The purpose of a test is to
Armin Ronacher7259c762008-04-30 13:03:59 +0200828give the template designers the possibility to perform type and conformability
829checks.
830
Armin Ronachera5d8f552008-09-11 20:46:34 +0200831Here a simple test that checks if a variable is a prime number::
Armin Ronacher7259c762008-04-30 13:03:59 +0200832
833 import math
834
835 def is_prime(n):
836 if n == 2:
837 return True
Deepak Amin4965fac2019-05-31 14:17:35 -0400838 for i in range(2, int(math.ceil(math.sqrt(n))) + 1):
Armin Ronacher7259c762008-04-30 13:03:59 +0200839 if n % i == 0:
840 return False
841 return True
Jon Dufresne148b6fb2018-08-29 20:58:03 -0700842
Armin Ronacher7259c762008-04-30 13:03:59 +0200843
844You can register it on the template environment by updating the
845:attr:`~Environment.tests` dict on the environment::
846
847 environment.tests['prime'] = is_prime
848
849A template designer can then use the test like this:
850
851.. sourcecode:: jinja
852
853 {% if 42 is prime %}
854 42 is a prime number
855 {% else %}
856 42 is not a prime number
857 {% endif %}
858
859
860.. _global-namespace:
861
862The Global Namespace
863--------------------
864
Armin Ronacher981cbf62008-05-13 09:12:27 +0200865Variables stored in the :attr:`Environment.globals` dict are special as they
866are available for imported templates too, even if they are imported without
867context. This is the place where you can put variables and functions
868that should be available all the time. Additionally :attr:`Template.globals`
869exist that are variables available to a specific template that are available
870to all :meth:`~Template.render` calls.
Armin Ronacher5411ce72008-05-25 11:36:22 +0200871
872
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200873.. _low-level-api:
874
Armin Ronacher5411ce72008-05-25 11:36:22 +0200875Low Level API
876-------------
877
878The low level API exposes functionality that can be useful to understand some
879implementation details, debugging purposes or advanced :ref:`extension
Armin Ronacher61a5a242008-05-26 12:07:44 +0200880<jinja-extensions>` techniques. Unless you know exactly what you are doing we
881don't recommend using any of those.
Armin Ronacher5411ce72008-05-25 11:36:22 +0200882
883.. automethod:: Environment.lex
884
885.. automethod:: Environment.parse
886
Armin Ronacher9ad96e72008-06-13 22:44:01 +0200887.. automethod:: Environment.preprocess
888
Armin Ronacher5411ce72008-05-25 11:36:22 +0200889.. automethod:: Template.new_context
890
891.. method:: Template.root_render_func(context)
892
893 This is the low level render function. It's passed a :class:`Context`
894 that has to be created by :meth:`new_context` of the same template or
895 a compatible template. This render function is generated by the
896 compiler from the template code and returns a generator that yields
897 unicode strings.
898
899 If an exception in the template code happens the template engine will
900 not rewrite the exception but pass through the original one. As a
901 matter of fact this function should only be called from within a
902 :meth:`render` / :meth:`generate` / :meth:`stream` call.
903
904.. attribute:: Template.blocks
905
906 A dict of block render functions. Each of these functions works exactly
907 like the :meth:`root_render_func` with the same limitations.
908
909.. attribute:: Template.is_up_to_date
910
911 This attribute is `False` if there is a newer version of the template
912 available, otherwise `True`.
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200913
914.. admonition:: Note
915
David Lord57626a02019-10-23 12:29:46 -0700916 The low-level API is fragile. Future Jinja versions will try not to
917 change it in a backwards incompatible way but modifications in the Jinja
918 core may shine through. For example if Jinja introduces a new AST node
Armin Ronacher58f351d2008-05-28 21:30:14 +0200919 in later versions that may be returned by :meth:`~Environment.parse`.
Armin Ronacher63cf9b82009-07-26 10:33:36 +0200920
921The Meta API
922------------
923
924.. versionadded:: 2.2
925
926The meta API returns some information about abstract syntax trees that
927could help applications to implement more advanced template concepts. All
928the functions of the meta API operate on an abstract syntax tree as
929returned by the :meth:`Environment.parse` method.
930
David Lord4a59ac92020-01-26 21:12:52 -0800931.. autofunction:: jinja2.meta.find_undeclared_variables
Armin Ronacher63cf9b82009-07-26 10:33:36 +0200932
David Lord4a59ac92020-01-26 21:12:52 -0800933.. autofunction:: jinja2.meta.find_referenced_templates