| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 1 | API |
| 2 | === |
| 3 | |
| 4 | .. module:: jinja2 |
| 5 | :synopsis: public Jinja2 API |
| 6 | |
| 7 | This document describes the API to Jinja2 and not the template language. It |
| 8 | will be most useful as reference to those implementing the template interface |
| 9 | to the application and not those who are creating Jinja2 templates. |
| 10 | |
| 11 | Basics |
| 12 | ------ |
| 13 | |
| 14 | Jinja2 uses a central object called the template :class:`Environment`. |
| Carl A Dunham | d546358 | 2014-01-18 15:26:10 -0600 | [diff] [blame] | 15 | 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] | 16 | 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] | 17 | 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] | 18 | :class:`Template` class, an environment is created automatically for you, |
| 19 | albeit a shared one. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 20 | |
| 21 | Most applications will create one :class:`Environment` object on application |
| Kojo Idrissa | d48cb21 | 2016-04-10 14:04:46 -0500 | [diff] [blame] | 22 | 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] | 23 | useful to have multiple environments side by side, if different configurations |
| 24 | are in use. |
| 25 | |
| 26 | The simplest way to configure Jinja2 to load templates for your application |
| 27 | looks roughly like this:: |
| 28 | |
| 29 | from jinja2 import Environment, PackageLoader |
| 30 | env = Environment(loader=PackageLoader('yourapplication', 'templates')) |
| 31 | |
| 32 | This will create a template environment with the default settings and a |
| 33 | loader that looks up the templates in the `templates` folder inside the |
| 34 | `yourapplication` python package. Different loaders are available |
| 35 | and you can also write your own if you want to load templates from a |
| 36 | database or other resources. |
| 37 | |
| 38 | To load a template from this environment you just have to call the |
| 39 | :meth:`get_template` method which then returns the loaded :class:`Template`:: |
| 40 | |
| 41 | template = env.get_template('mytemplate.html') |
| 42 | |
| 43 | To render it with some variables, just call the :meth:`render` method:: |
| 44 | |
| 45 | print template.render(the='variables', go='here') |
| 46 | |
| Éric Araujo | f6b654d | 2015-05-29 16:57:10 -0400 | [diff] [blame] | 47 | Using a template loader rather than passing strings to :class:`Template` |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 48 | or :meth:`Environment.from_string` has multiple advantages. Besides being |
| 49 | a lot easier to use it also enables template inheritance. |
| 50 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 51 | |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 52 | Unicode |
| 53 | ------- |
| 54 | |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 55 | Jinja2 is using Unicode internally which means that you have to pass Unicode |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 56 | objects to the render function or bytestrings that only consist of ASCII |
| 57 | characters. Additionally newlines are normalized to one end of line |
| 58 | sequence which is per default UNIX style (``\n``). |
| 59 | |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 60 | Python 2.x supports two ways of representing string objects. One is the |
| 61 | `str` type and the other is the `unicode` type, both of which extend a type |
| 62 | called `basestring`. Unfortunately the default is `str` which should not |
| 63 | be used to store text based information unless only ASCII characters are |
| Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 64 | 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] | 65 | module level and with Python 3 it will be the default. |
| 66 | |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 67 | 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] | 68 | 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] | 69 | store the string as Unicode by decoding the string with the character |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 70 | encoding from the current Python module. If no encoding is specified this |
| 71 | defaults to 'ASCII' which means that you can't use any non ASCII identifier. |
| 72 | |
| 73 | 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] | 74 | second line of the Python module using the Unicode literal:: |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 75 | |
| 76 | # -*- coding: utf-8 -*- |
| 77 | |
| 78 | We recommend utf-8 as Encoding for Python modules and templates as it's |
| 79 | possible to represent every Unicode character in utf-8 and because it's |
| 80 | backwards compatible to ASCII. For Jinja2 the default encoding of templates |
| 81 | is assumed to be utf-8. |
| 82 | |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 83 | It is not possible to use Jinja2 to process non-Unicode data. The reason |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 84 | for this is that Jinja2 uses Unicode already on the language level. For |
| 85 | example Jinja2 treats the non-breaking space as valid whitespace inside |
| 86 | expressions which requires knowledge of the encoding or operating on an |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 87 | Unicode string. |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 88 | |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 89 | 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] | 90 | `Unicode documentation`_. |
| 91 | |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 92 | Another important thing is how Jinja2 is handling string literals in |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 93 | templates. A naive implementation would be using Unicode strings for |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 94 | all string literals but it turned out in the past that this is problematic |
| 95 | as some libraries are typechecking against `str` explicitly. For example |
| Armin Ronacher | 656d5e7 | 2010-02-09 01:31:47 +0100 | [diff] [blame] | 96 | `datetime.strftime` does not accept Unicode arguments. To not break it |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 97 | completely Jinja2 is returning `str` for strings that fit into ASCII and |
| 98 | for everything else `unicode`: |
| 99 | |
| 100 | >>> m = Template(u"{% set a, b = 'foo', 'föö' %}").module |
| 101 | >>> m.a |
| 102 | 'foo' |
| 103 | >>> m.b |
| 104 | u'f\xf6\xf6' |
| 105 | |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 106 | |
| 107 | .. _Unicode documentation: http://docs.python.org/dev/howto/unicode.html |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 108 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 109 | High Level API |
| 110 | -------------- |
| 111 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 112 | The high-level API is the API you will use in the application to load and |
| 113 | render Jinja2 templates. The :ref:`low-level-api` on the other side is only |
| 114 | useful if you want to dig deeper into Jinja2 or :ref:`develop extensions |
| 115 | <jinja-extensions>`. |
| 116 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 117 | .. autoclass:: Environment([options]) |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 118 | :members: from_string, get_template, select_template, |
| Armin Ronacher | 4684498 | 2011-01-29 20:19:58 +0100 | [diff] [blame] | 119 | get_or_select_template, join_path, extend, compile_expression, |
| Armin Ronacher | 9463850 | 2011-09-26 00:41:25 +0200 | [diff] [blame] | 120 | compile_templates, list_templates, add_extension |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 121 | |
| 122 | .. attribute:: shared |
| 123 | |
| 124 | If a template was created by using the :class:`Template` constructor |
| 125 | an environment is created automatically. These environments are |
| 126 | created as shared environments which means that multiple templates |
| 127 | may have the same anonymous environment. For all shared environments |
| 128 | this attribute is `True`, else `False`. |
| 129 | |
| 130 | .. attribute:: sandboxed |
| 131 | |
| 132 | If the environment is sandboxed this attribute is `True`. For the |
| 133 | sandbox mode have a look at the documentation for the |
| 134 | :class:`~jinja2.sandbox.SandboxedEnvironment`. |
| 135 | |
| 136 | .. attribute:: filters |
| 137 | |
| 138 | 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] | 139 | 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] | 140 | see :ref:`writing-filters`. For valid filter names have a look at |
| 141 | :ref:`identifier-naming`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 142 | |
| 143 | .. attribute:: tests |
| 144 | |
| Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 145 | A dict of test functions for this environment. As long as no |
| 146 | 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] | 147 | see :ref:`writing-tests`. For valid test names have a look at |
| 148 | :ref:`identifier-naming`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 149 | |
| 150 | .. attribute:: globals |
| 151 | |
| 152 | A dict of global variables. These variables are always available |
| Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 153 | 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] | 154 | to modify this dict. For more details see :ref:`global-namespace`. |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 155 | For valid object names have a look at :ref:`identifier-naming`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 156 | |
| Armin Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 157 | .. attribute:: policies |
| 158 | |
| 159 | A dictionary with :ref:`policies`. These can be reconfigured to |
| 160 | change the runtime behavior or certain template features. Usually |
| 161 | these are security related. |
| 162 | |
| ThiefMaster | 1493631 | 2015-04-06 13:54:14 +0200 | [diff] [blame] | 163 | .. attribute:: code_generator_class |
| 164 | |
| 165 | The class used for code generation. This should not be changed |
| 166 | in most cases, unless you need to modify the Python code a |
| 167 | template compiles to. |
| 168 | |
| ThiefMaster | f22fdd5 | 2015-04-06 14:08:46 +0200 | [diff] [blame] | 169 | .. attribute:: context_class |
| 170 | |
| 171 | The context used for templates. This should not be changed |
| 172 | in most cases, unless you need to modify internals of how |
| 173 | template variables are handled. For details, see |
| 174 | :class:`~jinja2.runtime.Context`. |
| 175 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 176 | .. automethod:: overlay([options]) |
| 177 | |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 178 | .. method:: undefined([hint, obj, name, exc]) |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 179 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 180 | Creates a new :class:`Undefined` object for `name`. This is useful |
| 181 | for filters or functions that may return undefined objects for |
| 182 | some operations. All parameters except of `hint` should be provided |
| 183 | as keyword parameters for better readability. The `hint` is used as |
| 184 | error message for the exception if provided, otherwise the error |
| Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 185 | message will be generated from `obj` and `name` automatically. The exception |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 186 | provided as `exc` is raised if something with the generated undefined |
| 187 | object is done that the undefined object does not allow. The default |
| 188 | exception is :exc:`UndefinedError`. If a `hint` is provided the |
| Alex Chan | 972c030 | 2015-04-05 22:42:34 +0100 | [diff] [blame] | 189 | `name` may be omitted. |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 190 | |
| 191 | The most common way to create an undefined object is by providing |
| 192 | a name only:: |
| 193 | |
| 194 | return environment.undefined(name='some_name') |
| 195 | |
| 196 | This means that the name `some_name` is not defined. If the name |
| 197 | was from an attribute of an object it makes sense to tell the |
| 198 | undefined object the holder object to improve the error message:: |
| 199 | |
| 200 | if not hasattr(obj, 'attr'): |
| 201 | return environment.undefined(obj=obj, name='attr') |
| 202 | |
| 203 | For a more complex example you can provide a hint. For example |
| 204 | the :func:`first` filter creates an undefined object that way:: |
| 205 | |
| 206 | return environment.undefined('no first item, sequence was empty') |
| 207 | |
| 208 | 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] | 209 | was accessed) it should be passed to the undefined object, even if |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 210 | a custom `hint` is provided. This gives undefined objects the |
| 211 | possibility to enhance the error message. |
| 212 | |
| 213 | .. autoclass:: Template |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 214 | :members: module, make_module |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 215 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 216 | .. attribute:: globals |
| 217 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 218 | The dict with the globals of that template. It's unsafe to modify |
| 219 | this dict as it may be shared with other templates or the environment |
| 220 | that loaded the template. |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 221 | |
| 222 | .. attribute:: name |
| 223 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 224 | The loading name of the template. If the template was loaded from a |
| 225 | string this is `None`. |
| 226 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 227 | .. attribute:: filename |
| 228 | |
| 229 | The filename of the template on the file system if it was loaded from |
| 230 | there. Otherwise this is `None`. |
| 231 | |
| Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 232 | .. automethod:: render([context]) |
| 233 | |
| 234 | .. automethod:: generate([context]) |
| 235 | |
| 236 | .. automethod:: stream([context]) |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 237 | |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 238 | .. automethod:: render_async([context]) |
| 239 | |
| 240 | .. automethod:: generate_async([context]) |
| 241 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 242 | |
| Armin Ronacher | 6df604e | 2008-05-23 22:18:38 +0200 | [diff] [blame] | 243 | .. autoclass:: jinja2.environment.TemplateStream() |
| Armin Ronacher | 74b5106 | 2008-06-17 11:28:59 +0200 | [diff] [blame] | 244 | :members: disable_buffering, enable_buffering, dump |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 245 | |
| 246 | |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 247 | Autoescaping |
| 248 | ------------ |
| 249 | |
| Armin Ronacher | a27a503 | 2017-01-07 15:55:20 +0100 | [diff] [blame^] | 250 | .. versionchanged:: 2.4 |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 251 | |
| Armin Ronacher | a27a503 | 2017-01-07 15:55:20 +0100 | [diff] [blame^] | 252 | Jinja2 now comes with autoescaping support. As of Jinja 2.9 the |
| 253 | autoescape extension is removed and built-in. However autoescaping is |
| 254 | not yet enabled by default though this might change in the future. |
| 255 | It's recommended to configure a sensible default for autoescaping. This |
| 256 | makes it possible to enable and disable autoescaping on a per-template |
| 257 | basis (HTML versus text for instance). |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 258 | |
| 259 | Here a recommended setup that enables autoescaping for templates ending |
| 260 | in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default |
| 261 | for all other extensions:: |
| 262 | |
| 263 | def guess_autoescape(template_name): |
| 264 | if template_name is None or '.' not in template_name: |
| 265 | return False |
| 266 | ext = template_name.rsplit('.', 1)[1] |
| 267 | return ext in ('html', 'htm', 'xml') |
| 268 | |
| 269 | env = Environment(autoescape=guess_autoescape, |
| Armin Ronacher | a27a503 | 2017-01-07 15:55:20 +0100 | [diff] [blame^] | 270 | loader=PackageLoader('mypackage')) |
| Armin Ronacher | 1da23d1 | 2010-04-05 18:11:18 +0200 | [diff] [blame] | 271 | |
| 272 | When implementing a guessing autoescape function, make sure you also |
| 273 | accept `None` as valid template name. This will be passed when generating |
| 274 | templates from strings. |
| 275 | |
| 276 | Inside the templates the behaviour can be temporarily changed by using |
| 277 | the `autoescape` block (see :ref:`autoescape-overrides`). |
| 278 | |
| 279 | |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 280 | .. _identifier-naming: |
| 281 | |
| 282 | Notes on Identifiers |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 283 | -------------------- |
| Armin Ronacher | d1ff858 | 2008-05-11 00:30:43 +0200 | [diff] [blame] | 284 | |
| 285 | Jinja2 uses the regular Python 2.x naming rules. Valid identifiers have to |
| 286 | match ``[a-zA-Z_][a-zA-Z0-9_]*``. As a matter of fact non ASCII characters |
| 287 | are currently not allowed. This limitation will probably go away as soon as |
| 288 | unicode identifiers are fully specified for Python 3. |
| 289 | |
| 290 | Filters and tests are looked up in separate namespaces and have slightly |
| 291 | modified identifier syntax. Filters and tests may contain dots to group |
| 292 | filters and tests by topic. For example it's perfectly valid to add a |
| 293 | function into the filter dict and call it `to.unicode`. The regular |
| 294 | expression for filter and test identifiers is |
| 295 | ``[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*```. |
| 296 | |
| 297 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 298 | Undefined Types |
| 299 | --------------- |
| 300 | |
| 301 | These classes can be used as undefined types. The :class:`Environment` |
| 302 | constructor takes an `undefined` parameter that can be one of those classes |
| 303 | or a custom subclass of :class:`Undefined`. Whenever the template engine is |
| 304 | unable to look up a name or access an attribute one of those objects is |
| 305 | created and returned. Some operations on undefined values are then allowed, |
| 306 | others fail. |
| 307 | |
| 308 | The closest to regular Python behavior is the `StrictUndefined` which |
| 309 | disallows all operations beside testing if it's an undefined object. |
| 310 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 311 | .. autoclass:: jinja2.Undefined() |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 312 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 313 | .. attribute:: _undefined_hint |
| 314 | |
| 315 | Either `None` or an unicode string with the error message for |
| 316 | the undefined object. |
| 317 | |
| 318 | .. attribute:: _undefined_obj |
| 319 | |
| 320 | Either `None` or the owner object that caused the undefined object |
| 321 | to be created (for example because an attribute does not exist). |
| 322 | |
| 323 | .. attribute:: _undefined_name |
| 324 | |
| 325 | The name for the undefined variable / attribute or just `None` |
| 326 | if no such information exists. |
| 327 | |
| 328 | .. attribute:: _undefined_exception |
| 329 | |
| 330 | The exception that the undefined object wants to raise. This |
| 331 | is usually one of :exc:`UndefinedError` or :exc:`SecurityError`. |
| 332 | |
| 333 | .. method:: _fail_with_undefined_error(\*args, \**kwargs) |
| 334 | |
| 335 | When called with any arguments this method raises |
| 336 | :attr:`_undefined_exception` with an error message generated |
| 337 | from the undefined hints stored on the undefined object. |
| 338 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 339 | .. autoclass:: jinja2.DebugUndefined() |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 340 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 341 | .. autoclass:: jinja2.StrictUndefined() |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 342 | |
| Armin Ronacher | 6e9dfbf | 2014-06-06 22:14:45 +0600 | [diff] [blame] | 343 | There is also a factory function that can decorate undefined objects to |
| 344 | implement logging on failures: |
| 345 | |
| 346 | .. autofunction:: jinja2.make_logging_undefined |
| 347 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 348 | Undefined objects are created by calling :attr:`undefined`. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 349 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 350 | .. admonition:: Implementation |
| 351 | |
| 352 | :class:`Undefined` objects are implemented by overriding the special |
| 353 | `__underscore__` methods. For example the default :class:`Undefined` |
| 354 | class implements `__unicode__` in a way that it returns an empty |
| 355 | string, however `__int__` and others still fail with an exception. To |
| 356 | allow conversion to int by returning ``0`` you can implement your own:: |
| 357 | |
| 358 | class NullUndefined(Undefined): |
| 359 | def __int__(self): |
| 360 | return 0 |
| 361 | def __float__(self): |
| 362 | return 0.0 |
| 363 | |
| 364 | To disallow a method, just override it and raise |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 365 | :attr:`~Undefined._undefined_exception`. Because this is a very common |
| 366 | idom in undefined objects there is the helper method |
| 367 | :meth:`~Undefined._fail_with_undefined_error` that does the error raising |
| 368 | automatically. Here a class that works like the regular :class:`Undefined` |
| 369 | but chokes on iteration:: |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 370 | |
| 371 | class NonIterableUndefined(Undefined): |
| 372 | __iter__ = Undefined._fail_with_undefined_error |
| 373 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 374 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 375 | The Context |
| 376 | ----------- |
| 377 | |
| Armin Ronacher | 6df604e | 2008-05-23 22:18:38 +0200 | [diff] [blame] | 378 | .. autoclass:: jinja2.runtime.Context() |
| Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 379 | :members: resolve, get_exported, get_all |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 380 | |
| 381 | .. attribute:: parent |
| 382 | |
| 383 | A dict of read only, global variables the template looks up. These |
| Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 384 | can either come from another :class:`Context`, from the |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 385 | :attr:`Environment.globals` or :attr:`Template.globals` or points |
| 386 | to a dict created by combining the globals with the variables |
| 387 | passed to the render function. It must not be altered. |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 388 | |
| 389 | .. attribute:: vars |
| 390 | |
| 391 | The template local variables. This list contains environment and |
| 392 | context functions from the :attr:`parent` scope as well as local |
| 393 | modifications and exported variables from the template. The template |
| 394 | will modify this dict during template evaluation but filters and |
| 395 | context functions are not allowed to modify it. |
| 396 | |
| 397 | .. attribute:: environment |
| 398 | |
| 399 | The environment that loaded the template. |
| 400 | |
| 401 | .. attribute:: exported_vars |
| 402 | |
| 403 | This set contains all the names the template exports. The values for |
| 404 | the names are in the :attr:`vars` dict. In order to get a copy of the |
| 405 | exported variables as dict, :meth:`get_exported` can be used. |
| 406 | |
| 407 | .. attribute:: name |
| 408 | |
| 409 | The load name of the template owning this context. |
| 410 | |
| 411 | .. attribute:: blocks |
| 412 | |
| 413 | A dict with the current mapping of blocks in the template. The keys |
| 414 | in this dict are the names of the blocks, and the values a list of |
| 415 | blocks registered. The last item in each list is the current active |
| 416 | block (latest in the inheritance chain). |
| 417 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 418 | .. attribute:: eval_ctx |
| 419 | |
| 420 | The current :ref:`eval-context`. |
| 421 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 422 | .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs) |
| 423 | |
| 424 | |
| 425 | .. admonition:: Implementation |
| 426 | |
| 427 | Context is immutable for the same reason Python's frame locals are |
| 428 | immutable inside functions. Both Jinja2 and Python are not using the |
| 429 | context / frame locals as data storage for variables but only as primary |
| 430 | data source. |
| 431 | |
| 432 | When a template accesses a variable the template does not define, Jinja2 |
| 433 | looks up the variable in the context, after that the variable is treated |
| 434 | as if it was defined in the template. |
| 435 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 436 | |
| Armin Ronacher | 5cdc1ac | 2008-05-07 12:17:18 +0200 | [diff] [blame] | 437 | .. _loaders: |
| 438 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 439 | Loaders |
| 440 | ------- |
| 441 | |
| 442 | Loaders are responsible for loading templates from a resource such as the |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 443 | file system. The environment will keep the compiled modules in memory like |
| 444 | Python's `sys.modules`. Unlike `sys.modules` however this cache is limited in |
| 445 | size by default and templates are automatically reloaded. |
| Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 446 | 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] | 447 | own loader, subclass :class:`BaseLoader` and override `get_source`. |
| 448 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 449 | .. autoclass:: jinja2.BaseLoader |
| Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 450 | :members: get_source, load |
| 451 | |
| 452 | Here a list of the builtin loaders Jinja2 provides: |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 453 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 454 | .. autoclass:: jinja2.FileSystemLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 455 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 456 | .. autoclass:: jinja2.PackageLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 457 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 458 | .. autoclass:: jinja2.DictLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 459 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 460 | .. autoclass:: jinja2.FunctionLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 461 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 462 | .. autoclass:: jinja2.PrefixLoader |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 463 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 464 | .. autoclass:: jinja2.ChoiceLoader |
| 465 | |
| Armin Ronacher | 4684498 | 2011-01-29 20:19:58 +0100 | [diff] [blame] | 466 | .. autoclass:: jinja2.ModuleLoader |
| 467 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 468 | |
| 469 | .. _bytecode-cache: |
| 470 | |
| 471 | Bytecode Cache |
| 472 | -------------- |
| 473 | |
| 474 | Jinja 2.1 and higher support external bytecode caching. Bytecode caches make |
| 475 | it possible to store the generated bytecode on the file system or a different |
| 476 | location to avoid parsing the templates on first use. |
| 477 | |
| 478 | This is especially useful if you have a web application that is initialized on |
| 479 | the first request and Jinja compiles many templates at once which slows down |
| 480 | the application. |
| 481 | |
| Jakub Wilk | 3fc008b | 2013-05-25 23:37:34 +0200 | [diff] [blame] | 482 | 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] | 483 | |
| 484 | .. autoclass:: jinja2.BytecodeCache |
| 485 | :members: load_bytecode, dump_bytecode, clear |
| 486 | |
| 487 | .. autoclass:: jinja2.bccache.Bucket |
| 488 | :members: write_bytecode, load_bytecode, bytecode_from_string, |
| 489 | bytecode_to_string, reset |
| 490 | |
| 491 | .. attribute:: environment |
| 492 | |
| 493 | The :class:`Environment` that created the bucket. |
| 494 | |
| 495 | .. attribute:: key |
| 496 | |
| 497 | The unique cache key for this bucket |
| 498 | |
| 499 | .. attribute:: code |
| 500 | |
| 501 | The bytecode if it's loaded, otherwise `None`. |
| 502 | |
| 503 | |
| 504 | Builtin bytecode caches: |
| 505 | |
| 506 | .. autoclass:: jinja2.FileSystemBytecodeCache |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 507 | |
| Armin Ronacher | aa1d17d | 2008-09-18 18:09:06 +0200 | [diff] [blame] | 508 | .. autoclass:: jinja2.MemcachedBytecodeCache |
| 509 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 510 | |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 511 | Async Support |
| 512 | ------------- |
| 513 | |
| 514 | Starting with version 2.9, Jinja2 also supports the Python `async` and |
| 515 | `await` constructs. As far as template designers go this feature is |
| 516 | entirely opaque to them however as a developer you should be aware of how |
| 517 | it's implemented as it influences what type of APIs you can safely expose |
| 518 | to the template environment. |
| 519 | |
| 520 | First you need to be aware that by default async support is disabled as |
| 521 | enabling it will generate different template code behind the scenes which |
| 522 | passes everything through the asyncio event loop. This is important to |
| 523 | understand because it has some impact to what you are doing: |
| 524 | |
| 525 | * template rendering will require an event loop to be set for the |
| 526 | current thread (``asyncio.get_event_loop`` needs to return one) |
| 527 | * all template generation code internally runs async generators which |
| 528 | means that you will pay a performance penalty even if the non sync |
| 529 | methods are used! |
| 530 | * The sync methods are based on async methods if the async mode is |
| 531 | enabled which means that `render` for instance will internally invoke |
| 532 | `render_async` and run it as part of the current event loop until the |
| 533 | execution finished. |
| 534 | |
| 535 | Awaitable objects can be returned from functions in templates and any |
| 536 | function call in a template will automatically await the result. This |
| 537 | means that you can let provide a method that asynchronously loads data |
| 538 | from a database if you so desire and from the template designer's point of |
| 539 | view this is just another function they can call. This means that the |
| 540 | ``await`` you would normally issue in Python is implied. However this |
| 541 | only applies to function calls. If an attribute for instance would be an |
| 542 | avaitable object then this would not result in the expected behavior. |
| 543 | |
| 544 | Likewise iterations with a `for` loop support async iterators. |
| 545 | |
| Armin Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 546 | .. _policies: |
| 547 | |
| 548 | Policies |
| 549 | -------- |
| 550 | |
| 551 | Starting with Jinja 2.9 policies can be configured on the environment |
| 552 | which can slightly influence how filters and other template constructs |
| 553 | behave. They can be configured with the |
| 554 | :attr:`~jinja2.Environment.policies` attribute. |
| 555 | |
| 556 | Example:: |
| 557 | |
| 558 | env.policies['urlize.rel'] = 'nofollow noopener' |
| 559 | |
| Armin Ronacher | 028f058 | 2017-01-07 14:57:44 +0100 | [diff] [blame] | 560 | ``compiler.ascii_str``: |
| 561 | This boolean controls on Python 2 if Jinja2 should store ASCII only |
| 562 | literals as bytestring instead of unicode strings. This used to be |
| 563 | always enabled for Jinja versions below 2.9 and now can be changed. |
| 564 | Traditionally it was done this way since some APIs in Python 2 failed |
| 565 | badly for unicode strings (for instance the datetime strftime API). |
| 566 | Now however sometimes the inverse is true (for instance str.format). |
| 567 | If this is set to False then all strings are stored as unicode |
| 568 | internally. |
| 569 | |
| Armin Ronacher | e253520 | 2016-12-31 00:43:50 +0100 | [diff] [blame] | 570 | ``urlize.rel``: |
| 571 | A string that defines the items for the `rel` attribute of generated |
| 572 | links with the `urlize` filter. These items are always added. The |
| 573 | default is `noopener`. |
| 574 | |
| 575 | ``urlize.target``: |
| 576 | The default target that is issued for links from the `urlize` filter |
| 577 | if no other target is defined by the call explicitly. |
| 578 | |
| Armin Ronacher | e71a130 | 2017-01-06 21:33:51 +0100 | [diff] [blame] | 579 | ``json.dumps_function``: |
| 580 | If this is set to a value other than `None` then the `tojson` filter |
| 581 | will dump with this function instead of the default one. Note that |
| 582 | this function should accept arbitrary extra arguments which might be |
| 583 | passed in the future from the filter. Currently the only argument |
| 584 | that might be passed is `indent`. The default dump function is |
| 585 | ``json.dumps``. |
| 586 | |
| 587 | ``json.dumps_kwargs``: |
| 588 | Keyword arguments to be passed to the dump function. The default is |
| 589 | ``{'sort_keys': True}``. |
| 590 | |
| Armin Ronacher | d8326d9 | 2016-12-28 22:51:46 +0100 | [diff] [blame] | 591 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 592 | Utilities |
| 593 | --------- |
| 594 | |
| 595 | These helper functions and classes are useful if you add custom filters or |
| 596 | functions to a Jinja2 environment. |
| 597 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 598 | .. autofunction:: jinja2.environmentfilter |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 599 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 600 | .. autofunction:: jinja2.contextfilter |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 601 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 602 | .. autofunction:: jinja2.evalcontextfilter |
| 603 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 604 | .. autofunction:: jinja2.environmentfunction |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 605 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 606 | .. autofunction:: jinja2.contextfunction |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 607 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 608 | .. autofunction:: jinja2.evalcontextfunction |
| 609 | |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 610 | .. function:: escape(s) |
| 611 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 612 | Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s` |
| 613 | to HTML-safe sequences. Use this if you need to display text that might |
| 614 | contain such characters in HTML. This function will not escaped objects |
| 615 | that do have an HTML representation such as already escaped data. |
| 616 | |
| 617 | The return value is a :class:`Markup` string. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 618 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 619 | .. autofunction:: jinja2.clear_caches |
| Armin Ronacher | 187bde1 | 2008-05-01 18:19:16 +0200 | [diff] [blame] | 620 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 621 | .. autofunction:: jinja2.is_undefined |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 622 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 623 | .. autoclass:: jinja2.Markup([string]) |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 624 | :members: escape, unescape, striptags |
| 625 | |
| 626 | .. admonition:: Note |
| 627 | |
| 628 | The Jinja2 :class:`Markup` class is compatible with at least Pylons and |
| 629 | Genshi. It's expected that more template engines and framework will pick |
| 630 | up the `__html__` concept soon. |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 631 | |
| 632 | |
| 633 | Exceptions |
| 634 | ---------- |
| 635 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 636 | .. autoexception:: jinja2.TemplateError |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 637 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 638 | .. autoexception:: jinja2.UndefinedError |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 639 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 640 | .. autoexception:: jinja2.TemplateNotFound |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 641 | |
| Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 642 | .. autoexception:: jinja2.TemplatesNotFound |
| 643 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 644 | .. autoexception:: jinja2.TemplateSyntaxError |
| Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 645 | |
| Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 646 | .. attribute:: message |
| 647 | |
| 648 | The error message as utf-8 bytestring. |
| 649 | |
| 650 | .. attribute:: lineno |
| 651 | |
| 652 | The line number where the error occurred |
| 653 | |
| 654 | .. attribute:: name |
| 655 | |
| 656 | The load name for the template as unicode string. |
| 657 | |
| 658 | .. attribute:: filename |
| 659 | |
| 660 | The filename that loaded the template as bytestring in the encoding |
| 661 | of the file system (most likely utf-8 or mbcs on Windows systems). |
| 662 | |
| 663 | The reason why the filename and error message are bytestrings and not |
| 664 | unicode strings is that Python 2.x is not using unicode for exceptions |
| 665 | and tracebacks as well as the compiler. This will change with Python 3. |
| 666 | |
| Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 667 | .. autoexception:: jinja2.TemplateAssertionError |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 668 | |
| 669 | |
| 670 | .. _writing-filters: |
| 671 | |
| 672 | Custom Filters |
| 673 | -------------- |
| 674 | |
| 675 | 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] | 676 | 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] | 677 | extra arguments or keyword arguments. |
| 678 | |
| 679 | For example in the filter ``{{ 42|myfilter(23) }}`` the function would be |
| 680 | called with ``myfilter(42, 23)``. Here for example a simple filter that can |
| 681 | be applied to datetime objects to format them:: |
| 682 | |
| 683 | def datetimeformat(value, format='%H:%M / %d-%m-%Y'): |
| 684 | return value.strftime(format) |
| 685 | |
| 686 | You can register it on the template environment by updating the |
| 687 | :attr:`~Environment.filters` dict on the environment:: |
| 688 | |
| 689 | environment.filters['datetimeformat'] = datetimeformat |
| 690 | |
| 691 | Inside the template it can then be used as follows: |
| 692 | |
| 693 | .. sourcecode:: jinja |
| 694 | |
| 695 | written on: {{ article.pub_date|datetimeformat }} |
| 696 | publication date: {{ article.pub_date|datetimeformat('%d-%m-%Y') }} |
| 697 | |
| 698 | Filters can also be passed the current template context or environment. This |
| Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 699 | 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] | 700 | :attr:`~Environment.autoescape` setting. For this purpose three decorators |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 701 | exist: :func:`environmentfilter`, :func:`contextfilter` and |
| 702 | :func:`evalcontextfilter`. |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 703 | |
| 704 | Here a small example filter that breaks a text into HTML line breaks and |
| 705 | paragraphs and marks the return value as safe HTML string if autoescaping is |
| 706 | enabled:: |
| 707 | |
| 708 | import re |
| Jeffrey Finkelstein | 449ef02 | 2011-07-01 15:46:54 -0700 | [diff] [blame] | 709 | from jinja2 import evalcontextfilter, Markup, escape |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 710 | |
| 711 | _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}') |
| 712 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 713 | @evalcontextfilter |
| 714 | def nl2br(eval_ctx, value): |
| Jörn Hees | 1702451 | 2014-06-15 18:31:16 +0200 | [diff] [blame] | 715 | 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] | 716 | for p in _paragraph_re.split(escape(value))) |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 717 | if eval_ctx.autoescape: |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 718 | result = Markup(result) |
| 719 | return result |
| 720 | |
| 721 | Context filters work the same just that the first argument is the current |
| Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 722 | active :class:`Context` rather then the environment. |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 723 | |
| 724 | |
| Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 725 | .. _eval-context: |
| 726 | |
| 727 | Evaluation Context |
| 728 | ------------------ |
| 729 | |
| 730 | 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] | 731 | 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] | 732 | compiled features at runtime. |
| 733 | |
| 734 | Currently it is only used to enable and disable the automatic escaping but |
| 735 | can be used for extensions as well. |
| 736 | |
| 737 | In previous Jinja versions filters and functions were marked as |
| 738 | environment callables in order to check for the autoescape status from the |
| 739 | environment. In new versions it's encouraged to check the setting from the |
| 740 | evaluation context instead. |
| 741 | |
| 742 | Previous versions:: |
| 743 | |
| 744 | @environmentfilter |
| 745 | def filter(env, value): |
| 746 | result = do_something(value) |
| 747 | if env.autoescape: |
| 748 | result = Markup(result) |
| 749 | return result |
| 750 | |
| 751 | In new versions you can either use a :func:`contextfilter` and access the |
| 752 | evaluation context from the actual context, or use a |
| 753 | :func:`evalcontextfilter` which directly passes the evaluation context to |
| 754 | the function:: |
| 755 | |
| 756 | @contextfilter |
| 757 | def filter(context, value): |
| 758 | result = do_something(value) |
| 759 | if context.eval_ctx.autoescape: |
| 760 | result = Markup(result) |
| 761 | return result |
| 762 | |
| 763 | @evalcontextfilter |
| 764 | def filter(eval_ctx, value): |
| 765 | result = do_something(value) |
| 766 | if eval_ctx.autoescape: |
| 767 | result = Markup(result) |
| 768 | return result |
| 769 | |
| 770 | The evaluation context must not be modified at runtime. Modifications |
| 771 | must only happen with a :class:`nodes.EvalContextModifier` and |
| 772 | :class:`nodes.ScopedEvalContextModifier` from an extension, not on the |
| 773 | eval context object itself. |
| 774 | |
| Armin Ronacher | 76ae15e | 2010-03-15 09:36:47 +0100 | [diff] [blame] | 775 | .. autoclass:: jinja2.nodes.EvalContext |
| Armin Ronacher | 30fda27 | 2010-03-15 03:06:04 +0100 | [diff] [blame] | 776 | |
| 777 | .. attribute:: autoescape |
| 778 | |
| 779 | `True` or `False` depending on if autoescaping is active or not. |
| 780 | |
| 781 | .. attribute:: volatile |
| 782 | |
| 783 | `True` if the compiler cannot evaluate some expressions at compile |
| 784 | time. At runtime this should always be `False`. |
| 785 | |
| 786 | |
| Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 787 | .. _writing-tests: |
| 788 | |
| 789 | Custom Tests |
| 790 | ------------ |
| 791 | |
| Armin Ronacher | a5d8f55 | 2008-09-11 20:46:34 +0200 | [diff] [blame] | 792 | 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] | 793 | 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] | 794 | 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] | 795 | give the template designers the possibility to perform type and conformability |
| 796 | checks. |
| 797 | |
| Armin Ronacher | a5d8f55 | 2008-09-11 20:46:34 +0200 | [diff] [blame] | 798 | 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] | 799 | |
| 800 | import math |
| 801 | |
| 802 | def is_prime(n): |
| 803 | if n == 2: |
| 804 | return True |
| 805 | for i in xrange(2, int(math.ceil(math.sqrt(n))) + 1): |
| 806 | if n % i == 0: |
| 807 | return False |
| 808 | return True |
| 809 | |
| 810 | |
| 811 | You can register it on the template environment by updating the |
| 812 | :attr:`~Environment.tests` dict on the environment:: |
| 813 | |
| 814 | environment.tests['prime'] = is_prime |
| 815 | |
| 816 | A template designer can then use the test like this: |
| 817 | |
| 818 | .. sourcecode:: jinja |
| 819 | |
| 820 | {% if 42 is prime %} |
| 821 | 42 is a prime number |
| 822 | {% else %} |
| 823 | 42 is not a prime number |
| 824 | {% endif %} |
| 825 | |
| 826 | |
| 827 | .. _global-namespace: |
| 828 | |
| 829 | The Global Namespace |
| 830 | -------------------- |
| 831 | |
| Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 832 | Variables stored in the :attr:`Environment.globals` dict are special as they |
| 833 | are available for imported templates too, even if they are imported without |
| 834 | context. This is the place where you can put variables and functions |
| 835 | that should be available all the time. Additionally :attr:`Template.globals` |
| 836 | exist that are variables available to a specific template that are available |
| 837 | to all :meth:`~Template.render` calls. |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 838 | |
| 839 | |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 840 | .. _low-level-api: |
| 841 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 842 | Low Level API |
| 843 | ------------- |
| 844 | |
| 845 | The low level API exposes functionality that can be useful to understand some |
| 846 | implementation details, debugging purposes or advanced :ref:`extension |
| Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 847 | <jinja-extensions>` techniques. Unless you know exactly what you are doing we |
| 848 | don't recommend using any of those. |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 849 | |
| 850 | .. automethod:: Environment.lex |
| 851 | |
| 852 | .. automethod:: Environment.parse |
| 853 | |
| Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 854 | .. automethod:: Environment.preprocess |
| 855 | |
| Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 856 | .. automethod:: Template.new_context |
| 857 | |
| 858 | .. method:: Template.root_render_func(context) |
| 859 | |
| 860 | This is the low level render function. It's passed a :class:`Context` |
| 861 | that has to be created by :meth:`new_context` of the same template or |
| 862 | a compatible template. This render function is generated by the |
| 863 | compiler from the template code and returns a generator that yields |
| 864 | unicode strings. |
| 865 | |
| 866 | If an exception in the template code happens the template engine will |
| 867 | not rewrite the exception but pass through the original one. As a |
| 868 | matter of fact this function should only be called from within a |
| 869 | :meth:`render` / :meth:`generate` / :meth:`stream` call. |
| 870 | |
| 871 | .. attribute:: Template.blocks |
| 872 | |
| 873 | A dict of block render functions. Each of these functions works exactly |
| 874 | like the :meth:`root_render_func` with the same limitations. |
| 875 | |
| 876 | .. attribute:: Template.is_up_to_date |
| 877 | |
| 878 | This attribute is `False` if there is a newer version of the template |
| 879 | available, otherwise `True`. |
| Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 880 | |
| 881 | .. admonition:: Note |
| 882 | |
| Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 883 | The low-level API is fragile. Future Jinja2 versions will try not to |
| 884 | change it in a backwards incompatible way but modifications in the Jinja2 |
| 885 | core may shine through. For example if Jinja2 introduces a new AST node |
| 886 | in later versions that may be returned by :meth:`~Environment.parse`. |
| Armin Ronacher | 63cf9b8 | 2009-07-26 10:33:36 +0200 | [diff] [blame] | 887 | |
| 888 | The Meta API |
| 889 | ------------ |
| 890 | |
| 891 | .. versionadded:: 2.2 |
| 892 | |
| 893 | The meta API returns some information about abstract syntax trees that |
| 894 | could help applications to implement more advanced template concepts. All |
| 895 | the functions of the meta API operate on an abstract syntax tree as |
| 896 | returned by the :meth:`Environment.parse` method. |
| 897 | |
| 898 | .. autofunction:: jinja2.meta.find_undeclared_variables |
| 899 | |
| 900 | .. autofunction:: jinja2.meta.find_referenced_templates |