| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 1 | API |
| 2 | === |
| 3 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 4 | .. module:: jinja2 |
| jab | a811d86 | 2019-06-06 03:04:36 +0000 | [diff] [blame] | 5 | :noindex: |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 6 | :synopsis: public Jinja API |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 7 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 8 | This document describes the API to Jinja and not the template language. It |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 9 | will be most useful as reference to those implementing the template interface |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 10 | to the application and not those who are creating Jinja templates. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 11 | |
| 12 | Basics |
| 13 | ------ |
| 14 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 15 | Jinja uses a central object called the template :class:`Environment`. |
| Carl A Dunham | d546358 | 2014-01-18 15:26:10 -0600 | [diff] [blame] | 16 | Instances of this class are used to store the configuration and global objects, |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 17 | and are used to load templates from the file system or other locations. |
| Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 18 | Even if you are creating templates from strings by using the constructor of |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 19 | :class:`Template` class, an environment is created automatically for you, |
| 20 | albeit a shared one. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 21 | |
| 22 | Most applications will create one :class:`Environment` object on application |
| Jon Dufresne | 148b6fb | 2018-08-29 20:58:03 -0700 | [diff] [blame] | 23 | initialization and use that to load templates. In some cases however, it's |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 24 | useful to have multiple environments side by side, if different configurations |
| 25 | are in use. |
| 26 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 27 | The simplest way to configure Jinja to load templates for your application |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 28 | looks roughly like this:: |
| 29 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 30 | from jinja2 import Environment, PackageLoader, select_autoescape |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 31 | env = Environment( |
| 32 | loader=PackageLoader('yourapplication', 'templates'), |
| 33 | autoescape=select_autoescape(['html', 'xml']) |
| 34 | ) |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 35 | |
| 36 | This will create a template environment with the default settings and a |
| 37 | loader that looks up the templates in the `templates` folder inside the |
| 38 | `yourapplication` python package. Different loaders are available |
| 39 | and you can also write your own if you want to load templates from a |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 40 | database or other resources. This also enables autoescaping for HTML and |
| 41 | XML files. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 42 | |
| 43 | To 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 | |
| 48 | To render it with some variables, just call the :meth:`render` method:: |
| 49 | |
| Deepak Amin | 4965fac | 2019-05-31 14:17:35 -0400 | [diff] [blame] | 50 | print(template.render(the='variables', go='here')) |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 51 | |
| Éric Araujo | f6b654d | 2015-05-29 16:57:10 -0400 | [diff] [blame] | 52 | Using a template loader rather than passing strings to :class:`Template` |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 53 | or :meth:`Environment.from_string` has multiple advantages. Besides being |
| 54 | a lot easier to use it also enables template inheritance. |
| 55 | |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 56 | .. admonition:: Notes on Autoescaping |
| 57 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 58 | In future versions of Jinja we might enable autoescaping by default |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 59 | for security reasons. As such you are encouraged to explicitly |
| 60 | configure autoescaping now instead of relying on the default. |
| 61 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 62 | |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 63 | Unicode |
| 64 | ------- |
| 65 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 66 | Jinja is using Unicode internally which means that you have to pass Unicode |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 67 | objects to the render function or bytestrings that only consist of ASCII |
| 68 | characters. Additionally newlines are normalized to one end of line |
| 69 | sequence which is per default UNIX style (``\n``). |
| 70 | |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 71 | Python 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 |
| 73 | called `basestring`. Unfortunately the default is `str` which should not |
| 74 | be used to store text based information unless only ASCII characters are |
| Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 75 | used. With Python 2.6 it is possible to make `unicode` the default on a per |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 76 | module level and with Python 3 it will be the default. |
| 77 | |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 78 | To explicitly use a Unicode string you have to prefix the string literal |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 79 | with a `u`: ``u'Hänsel und Gretel sagen Hallo'``. That way Python will |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 80 | store the string as Unicode by decoding the string with the character |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 81 | encoding from the current Python module. If no encoding is specified this |
| 82 | defaults to 'ASCII' which means that you can't use any non ASCII identifier. |
| 83 | |
| 84 | To set a better module encoding add the following comment to the first or |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 85 | second line of the Python module using the Unicode literal:: |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 86 | |
| 87 | # -*- coding: utf-8 -*- |
| 88 | |
| 89 | We recommend utf-8 as Encoding for Python modules and templates as it's |
| 90 | possible to represent every Unicode character in utf-8 and because it's |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 91 | backwards compatible to ASCII. For Jinja the default encoding of templates |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 92 | is assumed to be utf-8. |
| 93 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 94 | It is not possible to use Jinja to process non-Unicode data. The reason |
| 95 | for this is that Jinja uses Unicode already on the language level. For |
| 96 | example Jinja treats the non-breaking space as valid whitespace inside |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 97 | expressions which requires knowledge of the encoding or operating on an |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 98 | Unicode string. |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 99 | |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 100 | For more details about Unicode in Python have a look at the excellent |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 101 | `Unicode documentation`_. |
| 102 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 103 | Another important thing is how Jinja is handling string literals in |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 104 | templates. A naive implementation would be using Unicode strings for |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 105 | all string literals but it turned out in the past that this is problematic |
| 106 | as some libraries are typechecking against `str` explicitly. For example |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 107 | `datetime.strftime` does not accept Unicode arguments. To not break it |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 108 | completely Jinja is returning `str` for strings that fit into ASCII and |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 109 | for everything else `unicode`: |
| 110 | |
| 111 | >>> m = Template(u"{% set a, b = 'foo', 'föö' %}").module |
| 112 | >>> m.a |
| 113 | 'foo' |
| 114 | >>> m.b |
| 115 | u'f\xf6\xf6' |
| 116 | |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 117 | |
| David Lord | 0669656 | 2019-07-26 12:12:41 -0700 | [diff] [blame] | 118 | .. _Unicode documentation: https://docs.python.org/3/howto/unicode.html |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 119 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 120 | High Level API |
| 121 | -------------- |
| 122 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 123 | The high-level API is the API you will use in the application to load and |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 124 | render Jinja templates. The :ref:`low-level-api` on the other side is only |
| 125 | useful if you want to dig deeper into Jinja or :ref:`develop extensions |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 126 | <jinja-extensions>`. |
| 127 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 128 | .. autoclass:: Environment([options]) |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 129 | :members: from_string, get_template, select_template, |
| Armin Ronacher | 4684498 | 2011-01-29 20:19:58 +0100 | [diff] [blame] | 130 | get_or_select_template, join_path, extend, compile_expression, |
| Armin Ronacher | 9463850 | 2011-09-26 00:41:25 +0200 | [diff] [blame] | 131 | compile_templates, list_templates, add_extension |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 132 | |
| 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 Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 145 | :class:`~jinja2.sandbox.SandboxedEnvironment`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 146 | |
| 147 | .. attribute:: filters |
| 148 | |
| 149 | A dict of filters for this environment. As long as no template was |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 150 | loaded it's safe to add new filters or remove old. For custom filters |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 151 | see :ref:`writing-filters`. For valid filter names have a look at |
| 152 | :ref:`identifier-naming`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 153 | |
| 154 | .. attribute:: tests |
| 155 | |
| Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 156 | 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 Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 158 | see :ref:`writing-tests`. For valid test names have a look at |
| 159 | :ref:`identifier-naming`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 160 | |
| 161 | .. attribute:: globals |
| 162 | |
| 163 | A dict of global variables. These variables are always available |
| Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 164 | in a template. As long as no template was loaded it's safe |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 165 | to modify this dict. For more details see :ref:`global-namespace`. |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 166 | For valid object names have a look at :ref:`identifier-naming`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 167 | |
| Armin Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 168 | .. 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 | |
| ThiefMaster | 1493631 | 2015-04-06 13:54:14 +0200 | [diff] [blame] | 174 | .. 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 | |
| ThiefMaster | f22fdd5 | 2015-04-06 14:08:46 +0200 | [diff] [blame] | 180 | .. 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 Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 185 | :class:`~jinja2.runtime.Context`. |
| ThiefMaster | f22fdd5 | 2015-04-06 14:08:46 +0200 | [diff] [blame] | 186 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 187 | .. automethod:: overlay([options]) |
| 188 | |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 189 | .. method:: undefined([hint, obj, name, exc]) |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 190 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 191 | 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 Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 196 | message will be generated from `obj` and `name` automatically. The exception |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 197 | 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 Chan | 972c030 | 2015-04-05 22:42:34 +0100 | [diff] [blame] | 200 | `name` may be omitted. |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 201 | |
| 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 Dufresne | 148b6fb | 2018-08-29 20:58:03 -0700 | [diff] [blame] | 217 | return environment.undefined('no first item, sequence was empty') |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 218 | |
| 219 | If it the `name` or `obj` is known (for example because an attribute |
| Alex Chan | 972c030 | 2015-04-05 22:42:34 +0100 | [diff] [blame] | 220 | was accessed) it should be passed to the undefined object, even if |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 221 | a custom `hint` is provided. This gives undefined objects the |
| 222 | possibility to enhance the error message. |
| 223 | |
| 224 | .. autoclass:: Template |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 225 | :members: module, make_module |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 226 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 227 | .. attribute:: globals |
| 228 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 229 | 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 Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 232 | |
| 233 | .. attribute:: name |
| 234 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 235 | The loading name of the template. If the template was loaded from a |
| 236 | string this is `None`. |
| 237 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 238 | .. 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 Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 243 | .. automethod:: render([context]) |
| 244 | |
| 245 | .. automethod:: generate([context]) |
| 246 | |
| 247 | .. automethod:: stream([context]) |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 248 | |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 249 | .. automethod:: render_async([context]) |
| 250 | |
| 251 | .. automethod:: generate_async([context]) |
| 252 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 253 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 254 | .. autoclass:: jinja2.environment.TemplateStream() |
| Armin Ronacher | 74b5106 | 2008-06-17 11:28:59 +0200 | [diff] [blame] | 255 | :members: disable_buffering, enable_buffering, dump |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 256 | |
| 257 | |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 258 | Autoescaping |
| 259 | ------------ |
| 260 | |
| Armin Ronacher | a27a503 | 2017-01-07 15:55:20 +0100 | [diff] [blame] | 261 | .. versionchanged:: 2.4 |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 262 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 263 | Jinja now comes with autoescaping support. As of Jinja 2.9 the |
| Armin Ronacher | a27a503 | 2017-01-07 15:55:20 +0100 | [diff] [blame] | 264 | autoescape extension is removed and built-in. However autoescaping is |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 265 | not yet enabled by default though this will most likely change in the |
| 266 | future. It's recommended to configure a sensible default for |
| 267 | autoescaping. This makes it possible to enable and disable autoescaping |
| 268 | on a per-template basis (HTML versus text for instance). |
| 269 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 270 | .. autofunction:: jinja2.select_autoescape |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 271 | |
| 272 | Here a recommended setup that enables autoescaping for templates ending |
| 273 | in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 274 | for all other extensions. You can use the :func:`~jinja2.select_autoescape` |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 275 | function for this:: |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 276 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 277 | from jinja2 import Environment, select_autoescape |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 278 | env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']), |
| Armin Ronacher | a27a503 | 2017-01-07 15:55:20 +0100 | [diff] [blame] | 279 | loader=PackageLoader('mypackage')) |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 280 | |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 281 | The :func:`~jinja.select_autoescape` function returns a function that |
| Unknown | 778ccb2 | 2017-11-08 20:02:28 -0500 | [diff] [blame] | 282 | works roughly like this:: |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 283 | |
| 284 | def autoescape(template_name): |
| 285 | if template_name is None: |
| 286 | return False |
| 287 | if template_name.endswith(('.html', '.htm', '.xml')) |
| 288 | |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 289 | When implementing a guessing autoescape function, make sure you also |
| 290 | accept `None` as valid template name. This will be passed when generating |
| Armin Ronacher | b81a8a3 | 2017-01-07 16:13:39 +0100 | [diff] [blame] | 291 | templates from strings. You should always configure autoescaping as |
| 292 | defaults in the future might change. |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 293 | |
| 294 | Inside the templates the behaviour can be temporarily changed by using |
| 295 | the `autoescape` block (see :ref:`autoescape-overrides`). |
| 296 | |
| 297 | |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 298 | .. _identifier-naming: |
| 299 | |
| 300 | Notes on Identifiers |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 301 | -------------------- |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 302 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 303 | Jinja uses Python naming rules. Valid identifiers can be any combination |
| 304 | of Unicode characters accepted by Python. |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 305 | |
| 306 | Filters and tests are looked up in separate namespaces and have slightly |
| 307 | modified identifier syntax. Filters and tests may contain dots to group |
| 308 | filters and tests by topic. For example it's perfectly valid to add a |
| 309 | function into the filter dict and call it `to.unicode`. The regular |
| 310 | expression for filter and test identifiers is |
| 311 | ``[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*```. |
| 312 | |
| 313 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 314 | Undefined Types |
| 315 | --------------- |
| 316 | |
| 317 | These classes can be used as undefined types. The :class:`Environment` |
| 318 | constructor takes an `undefined` parameter that can be one of those classes |
| 319 | or a custom subclass of :class:`Undefined`. Whenever the template engine is |
| 320 | unable to look up a name or access an attribute one of those objects is |
| 321 | created and returned. Some operations on undefined values are then allowed, |
| 322 | others fail. |
| 323 | |
| Étienne Pelletier | 19133d4 | 2019-05-08 10:47:33 -0400 | [diff] [blame] | 324 | The closest to regular Python behavior is the :class:`StrictUndefined` which |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 325 | disallows all operations beside testing if it's an undefined object. |
| 326 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 327 | .. autoclass:: jinja2.Undefined() |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 328 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 329 | .. 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 Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 355 | .. autoclass:: jinja2.ChainableUndefined() |
| Étienne Pelletier | 19133d4 | 2019-05-08 10:47:33 -0400 | [diff] [blame] | 356 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 357 | .. autoclass:: jinja2.DebugUndefined() |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 358 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 359 | .. autoclass:: jinja2.StrictUndefined() |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 360 | |
| Armin Ronacher | 6e9dfbf | 2014-06-06 22:14:45 +0600 | [diff] [blame] | 361 | There is also a factory function that can decorate undefined objects to |
| 362 | implement logging on failures: |
| 363 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 364 | .. autofunction:: jinja2.make_logging_undefined |
| Armin Ronacher | 6e9dfbf | 2014-06-06 22:14:45 +0600 | [diff] [blame] | 365 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 366 | Undefined objects are created by calling :attr:`undefined`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 367 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 368 | .. 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 Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 383 | :attr:`~Undefined._undefined_exception`. Because this is a very common |
| Ruben Garcia | a9d557f | 2019-02-08 11:18:06 +0100 | [diff] [blame] | 384 | idiom in undefined objects there is the helper method |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 385 | :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 Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 388 | |
| 389 | class NonIterableUndefined(Undefined): |
| 390 | __iter__ = Undefined._fail_with_undefined_error |
| 391 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 392 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 393 | The Context |
| 394 | ----------- |
| 395 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 396 | .. autoclass:: jinja2.runtime.Context() |
| Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 397 | :members: resolve, get_exported, get_all |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 398 | |
| 399 | .. attribute:: parent |
| 400 | |
| 401 | A dict of read only, global variables the template looks up. These |
| Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 402 | can either come from another :class:`Context`, from the |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 403 | :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 Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 406 | |
| 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 Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 436 | .. attribute:: eval_ctx |
| 437 | |
| 438 | The current :ref:`eval-context`. |
| 439 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 440 | .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs) |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 441 | |
| 442 | |
| 443 | .. admonition:: Implementation |
| 444 | |
| 445 | Context is immutable for the same reason Python's frame locals are |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 446 | immutable inside functions. Both Jinja and Python are not using the |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 447 | context / frame locals as data storage for variables but only as primary |
| 448 | data source. |
| 449 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 450 | When a template accesses a variable the template does not define, Jinja |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 451 | looks up the variable in the context, after that the variable is treated |
| 452 | as if it was defined in the template. |
| 453 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 454 | |
| Armin Ronacher | 5cdc1ac | 2008-05-07 12:17:18 +0200 | [diff] [blame] | 455 | .. _loaders: |
| 456 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 457 | Loaders |
| 458 | ------- |
| 459 | |
| 460 | Loaders are responsible for loading templates from a resource such as the |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 461 | file system. The environment will keep the compiled modules in memory like |
| 462 | Python's `sys.modules`. Unlike `sys.modules` however this cache is limited in |
| 463 | size by default and templates are automatically reloaded. |
| Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 464 | All loaders are subclasses of :class:`BaseLoader`. If you want to create your |
| Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 465 | own loader, subclass :class:`BaseLoader` and override `get_source`. |
| 466 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 467 | .. autoclass:: jinja2.BaseLoader |
| Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 468 | :members: get_source, load |
| 469 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 470 | Here a list of the builtin loaders Jinja provides: |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 471 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 472 | .. autoclass:: jinja2.FileSystemLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 473 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 474 | .. autoclass:: jinja2.PackageLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 475 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 476 | .. autoclass:: jinja2.DictLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 477 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 478 | .. autoclass:: jinja2.FunctionLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 479 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 480 | .. autoclass:: jinja2.PrefixLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 481 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 482 | .. autoclass:: jinja2.ChoiceLoader |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 483 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 484 | .. autoclass:: jinja2.ModuleLoader |
| Armin Ronacher | 4684498 | 2011-01-29 20:19:58 +0100 | [diff] [blame] | 485 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 486 | |
| 487 | .. _bytecode-cache: |
| 488 | |
| 489 | Bytecode Cache |
| 490 | -------------- |
| 491 | |
| 492 | Jinja 2.1 and higher support external bytecode caching. Bytecode caches make |
| 493 | it possible to store the generated bytecode on the file system or a different |
| 494 | location to avoid parsing the templates on first use. |
| 495 | |
| 496 | This is especially useful if you have a web application that is initialized on |
| 497 | the first request and Jinja compiles many templates at once which slows down |
| 498 | the application. |
| 499 | |
| Jakub Wilk | 3fc008b | 2013-05-25 23:37:34 +0200 | [diff] [blame] | 500 | To use a bytecode cache, instantiate it and pass it to the :class:`Environment`. |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 501 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 502 | .. autoclass:: jinja2.BytecodeCache |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 503 | :members: load_bytecode, dump_bytecode, clear |
| 504 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 505 | .. autoclass:: jinja2.bccache.Bucket |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 506 | :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 | |
| 522 | Builtin bytecode caches: |
| 523 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 524 | .. autoclass:: jinja2.FileSystemBytecodeCache |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 525 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 526 | .. autoclass:: jinja2.MemcachedBytecodeCache |
| Armin Ronacher | aa1d17d | 2008-09-18 18:09:06 +0200 | [diff] [blame] | 527 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 528 | |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 529 | Async Support |
| 530 | ------------- |
| 531 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 532 | Starting with version 2.9, Jinja also supports the Python `async` and |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 533 | `await` constructs. As far as template designers go this feature is |
| 534 | entirely opaque to them however as a developer you should be aware of how |
| 535 | it's implemented as it influences what type of APIs you can safely expose |
| 536 | to the template environment. |
| 537 | |
| 538 | First you need to be aware that by default async support is disabled as |
| 539 | enabling it will generate different template code behind the scenes which |
| 540 | passes everything through the asyncio event loop. This is important to |
| 541 | understand 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 |
| David Brochart | 7c1b535 | 2020-02-07 13:30:35 +0100 | [diff] [blame^] | 546 | means that you will pay a performance penalty even if the non async |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 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 | |
| 553 | Awaitable objects can be returned from functions in templates and any |
| 554 | function call in a template will automatically await the result. This |
| nwalsh1995 | 65337f8 | 2018-04-17 14:55:13 -0700 | [diff] [blame] | 555 | means that you can provide a method that asynchronously loads data |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 556 | from a database if you so desire and from the template designer's point of |
| 557 | view this is just another function they can call. This means that the |
| 558 | ``await`` you would normally issue in Python is implied. However this |
| 559 | only applies to function calls. If an attribute for instance would be an |
| Frank Sachsenheim | d0f8811 | 2018-05-11 21:25:22 +0200 | [diff] [blame] | 560 | awaitable object then this would not result in the expected behavior. |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 561 | |
| 562 | Likewise iterations with a `for` loop support async iterators. |
| 563 | |
| Armin Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 564 | .. _policies: |
| 565 | |
| 566 | Policies |
| 567 | -------- |
| 568 | |
| 569 | Starting with Jinja 2.9 policies can be configured on the environment |
| 570 | which can slightly influence how filters and other template constructs |
| 571 | behave. They can be configured with the |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 572 | :attr:`~jinja2.Environment.policies` attribute. |
| Armin Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 573 | |
| 574 | Example:: |
| 575 | |
| 576 | env.policies['urlize.rel'] = 'nofollow noopener' |
| 577 | |
| Armin Ronacher | 028f058 | 2017-01-07 14:57:44 +0100 | [diff] [blame] | 578 | ``compiler.ascii_str``: |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 579 | This boolean controls on Python 2 if Jinja should store ASCII only |
| Armin Ronacher | 028f058 | 2017-01-07 14:57:44 +0100 | [diff] [blame] | 580 | 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 Ronacher | fb47dfa | 2017-01-10 09:21:14 +0100 | [diff] [blame] | 588 | ``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 Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 594 | ``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 Ronacher | e71a130 | 2017-01-06 21:33:51 +0100 | [diff] [blame] | 603 | ``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 Moennich | e605ff1 | 2017-02-17 23:49:39 +0100 | [diff] [blame] | 615 | .. _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 Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 622 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 623 | Utilities |
| 624 | --------- |
| 625 | |
| 626 | These helper functions and classes are useful if you add custom filters or |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 627 | functions to a Jinja environment. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 628 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 629 | .. autofunction:: jinja2.environmentfilter |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 630 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 631 | .. autofunction:: jinja2.contextfilter |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 632 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 633 | .. autofunction:: jinja2.evalcontextfilter |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 634 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 635 | .. autofunction:: jinja2.environmentfunction |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 636 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 637 | .. autofunction:: jinja2.contextfunction |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 638 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 639 | .. autofunction:: jinja2.evalcontextfunction |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 640 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 641 | .. function:: escape(s) |
| 642 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 643 | 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 Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 649 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 650 | .. autofunction:: jinja2.clear_caches |
| Armin Ronacher | 187bde1 | 2008-05-01 18:19:16 +0200 | [diff] [blame] | 651 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 652 | .. autofunction:: jinja2.is_undefined |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 653 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 654 | .. autoclass:: jinja2.Markup([string]) |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 655 | :members: escape, unescape, striptags |
| 656 | |
| 657 | .. admonition:: Note |
| 658 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 659 | The Jinja :class:`Markup` class is compatible with at least Pylons and |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 660 | Genshi. It's expected that more template engines and framework will pick |
| 661 | up the `__html__` concept soon. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 662 | |
| 663 | |
| 664 | Exceptions |
| 665 | ---------- |
| 666 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 667 | .. autoexception:: jinja2.TemplateError |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 668 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 669 | .. autoexception:: jinja2.UndefinedError |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 670 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 671 | .. autoexception:: jinja2.TemplateNotFound |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 672 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 673 | .. autoexception:: jinja2.TemplatesNotFound |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 674 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 675 | .. autoexception:: jinja2.TemplateSyntaxError |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 676 | |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 677 | .. 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 Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 698 | .. autoexception:: jinja2.TemplateRuntimeError |
| Adrian Moennich | cc1d287 | 2017-02-26 18:00:06 +0100 | [diff] [blame] | 699 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 700 | .. autoexception:: jinja2.TemplateAssertionError |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 701 | |
| 702 | |
| 703 | .. _writing-filters: |
| 704 | |
| 705 | Custom Filters |
| 706 | -------------- |
| 707 | |
| 708 | Custom filters are just regular Python functions that take the left side of |
| Guillaume Paumier | 345e0ba | 2016-04-10 08:58:06 -0700 | [diff] [blame] | 709 | the filter as first argument and the arguments passed to the filter as |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 710 | extra arguments or keyword arguments. |
| 711 | |
| 712 | For example in the filter ``{{ 42|myfilter(23) }}`` the function would be |
| 713 | called with ``myfilter(42, 23)``. Here for example a simple filter that can |
| 714 | be applied to datetime objects to format them:: |
| 715 | |
| 716 | def datetimeformat(value, format='%H:%M / %d-%m-%Y'): |
| 717 | return value.strftime(format) |
| 718 | |
| 719 | You 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 | |
| 724 | Inside 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 | |
| 731 | Filters can also be passed the current template context or environment. This |
| Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 732 | is useful if a filter wants to return an undefined value or check the current |
| Armin Ronacher | 2e3c9c7 | 2010-04-10 13:03:46 +0200 | [diff] [blame] | 733 | :attr:`~Environment.autoescape` setting. For this purpose three decorators |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 734 | exist: :func:`environmentfilter`, :func:`contextfilter` and |
| 735 | :func:`evalcontextfilter`. |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 736 | |
| 737 | Here a small example filter that breaks a text into HTML line breaks and |
| 738 | paragraphs and marks the return value as safe HTML string if autoescaping is |
| 739 | enabled:: |
| 740 | |
| 741 | import re |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 742 | from jinja2 import evalcontextfilter, Markup, escape |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 743 | |
| Mark Amery | 9e410c7 | 2018-11-25 17:49:22 +0000 | [diff] [blame] | 744 | _paragraph_re = re.compile(r'(?:\r\n|\r(?!\n)|\n){2,}') |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 745 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 746 | @evalcontextfilter |
| 747 | def nl2br(eval_ctx, value): |
| Jörn Hees | 1702451 | 2014-06-15 18:31:16 +0200 | [diff] [blame] | 748 | result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br>\n')) |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 749 | for p in _paragraph_re.split(escape(value))) |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 750 | if eval_ctx.autoescape: |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 751 | result = Markup(result) |
| 752 | return result |
| 753 | |
| 754 | Context filters work the same just that the first argument is the current |
| Daniel van Flymen | 96f52e6 | 2017-03-01 14:10:34 -0500 | [diff] [blame] | 755 | active :class:`Context` rather than the environment. |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 756 | |
| 757 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 758 | .. _eval-context: |
| 759 | |
| 760 | Evaluation Context |
| 761 | ------------------ |
| 762 | |
| 763 | The evaluation context (short eval context or eval ctx) is a new object |
| Jakub Wilk | 3fc008b | 2013-05-25 23:37:34 +0200 | [diff] [blame] | 764 | introduced in Jinja 2.4 that makes it possible to activate and deactivate |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 765 | compiled features at runtime. |
| 766 | |
| 767 | Currently it is only used to enable and disable the automatic escaping but |
| 768 | can be used for extensions as well. |
| 769 | |
| 770 | In previous Jinja versions filters and functions were marked as |
| 771 | environment callables in order to check for the autoescape status from the |
| 772 | environment. In new versions it's encouraged to check the setting from the |
| 773 | evaluation context instead. |
| 774 | |
| 775 | Previous 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 | |
| 784 | In new versions you can either use a :func:`contextfilter` and access the |
| 785 | evaluation context from the actual context, or use a |
| 786 | :func:`evalcontextfilter` which directly passes the evaluation context to |
| 787 | the 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 | |
| 803 | The evaluation context must not be modified at runtime. Modifications |
| 804 | must only happen with a :class:`nodes.EvalContextModifier` and |
| 805 | :class:`nodes.ScopedEvalContextModifier` from an extension, not on the |
| 806 | eval context object itself. |
| 807 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 808 | .. autoclass:: jinja2.nodes.EvalContext |
| Armin Ronacher | 30fda27 | 2010-03-15 03:06:04 +0100 | [diff] [blame] | 809 | |
| 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 Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 820 | .. _writing-tests: |
| 821 | |
| 822 | Custom Tests |
| 823 | ------------ |
| 824 | |
| Armin Ronacher | a5d8f55 | 2008-09-11 20:46:34 +0200 | [diff] [blame] | 825 | Tests work like filters just that there is no way for a test to get access |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 826 | to the environment or context and that they can't be chained. The return |
| Armin Ronacher | a5d8f55 | 2008-09-11 20:46:34 +0200 | [diff] [blame] | 827 | value of a test should be `True` or `False`. The purpose of a test is to |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 828 | give the template designers the possibility to perform type and conformability |
| 829 | checks. |
| 830 | |
| Armin Ronacher | a5d8f55 | 2008-09-11 20:46:34 +0200 | [diff] [blame] | 831 | Here a simple test that checks if a variable is a prime number:: |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 832 | |
| 833 | import math |
| 834 | |
| 835 | def is_prime(n): |
| 836 | if n == 2: |
| 837 | return True |
| Deepak Amin | 4965fac | 2019-05-31 14:17:35 -0400 | [diff] [blame] | 838 | for i in range(2, int(math.ceil(math.sqrt(n))) + 1): |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 839 | if n % i == 0: |
| 840 | return False |
| 841 | return True |
| Jon Dufresne | 148b6fb | 2018-08-29 20:58:03 -0700 | [diff] [blame] | 842 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 843 | |
| 844 | You 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 | |
| 849 | A 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 | |
| 862 | The Global Namespace |
| 863 | -------------------- |
| 864 | |
| Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 865 | Variables stored in the :attr:`Environment.globals` dict are special as they |
| 866 | are available for imported templates too, even if they are imported without |
| 867 | context. This is the place where you can put variables and functions |
| 868 | that should be available all the time. Additionally :attr:`Template.globals` |
| 869 | exist that are variables available to a specific template that are available |
| 870 | to all :meth:`~Template.render` calls. |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 871 | |
| 872 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 873 | .. _low-level-api: |
| 874 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 875 | Low Level API |
| 876 | ------------- |
| 877 | |
| 878 | The low level API exposes functionality that can be useful to understand some |
| 879 | implementation details, debugging purposes or advanced :ref:`extension |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 880 | <jinja-extensions>` techniques. Unless you know exactly what you are doing we |
| 881 | don't recommend using any of those. |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 882 | |
| 883 | .. automethod:: Environment.lex |
| 884 | |
| 885 | .. automethod:: Environment.parse |
| 886 | |
| Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 887 | .. automethod:: Environment.preprocess |
| 888 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 889 | .. 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 Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 913 | |
| 914 | .. admonition:: Note |
| 915 | |
| David Lord | 57626a0 | 2019-10-23 12:29:46 -0700 | [diff] [blame] | 916 | 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 Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 919 | in later versions that may be returned by :meth:`~Environment.parse`. |
| Armin Ronacher | 63cf9b8 | 2009-07-26 10:33:36 +0200 | [diff] [blame] | 920 | |
| 921 | The Meta API |
| 922 | ------------ |
| 923 | |
| 924 | .. versionadded:: 2.2 |
| 925 | |
| 926 | The meta API returns some information about abstract syntax trees that |
| 927 | could help applications to implement more advanced template concepts. All |
| 928 | the functions of the meta API operate on an abstract syntax tree as |
| 929 | returned by the :meth:`Environment.parse` method. |
| 930 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 931 | .. autofunction:: jinja2.meta.find_undeclared_variables |
| Armin Ronacher | 63cf9b8 | 2009-07-26 10:33:36 +0200 | [diff] [blame] | 932 | |
| David Lord | 4a59ac9 | 2020-01-26 21:12:52 -0800 | [diff] [blame] | 933 | .. autofunction:: jinja2.meta.find_referenced_templates |