Skip to content
Myr

api.rst

This file

Path

docs/api.rst

931 lines · 30.4 KiB

Last changed by

Merge pull request #1444 from pallets/event-loopDavid Lordbefore adoption77674b9

History of this file

Take it

The walk stopped after 200 commits: lines older than 8146e45 point at it.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 1–3why, one step back
1API
2===3

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 4why, one step back
4.. module:: jinja2

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 5why, one step back
5 :noindex:

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 6why, one step back
6 :synopsis: public Jinja API

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 7why, one step back
7

Merge branch '2.11.x'David Lordbefore adoptionc074fd5

lines 8–11why, one step back
8This document describes the API to Jinja and not the template language
9(for that, see :doc:`/templates`). It will be most useful as reference10to those implementing the template interface to the application and not11those who are creating Jinja templates.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 12–15why, one step back
12
13Basics14------15

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 16why, one step back
16Jinja uses a central object called the template :class:`Environment`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 17–27why, one step back
17Instances of this class are used to store the configuration and global objects,
18and are used to load templates from the file system or other locations.19Even if you are creating templates from strings by using the constructor of20:class:`Template` class, an environment is created automatically for you,21albeit a shared one.2223Most applications will create one :class:`Environment` object on application24initialization and use that to load templates. In some cases however, it's25useful to have multiple environments side by side, if different configurations26are in use.27

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 28–29why, one step back
28The simplest way to configure Jinja to load templates for your
29application is to use :class:`~loaders.PackageLoader`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 30why, one step back
30

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 31–32why, one step back
31.. code-block:: python
32

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 33why, one step back
33 from jinja2 import Environment, PackageLoader, select_autoescape

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 34why, one step back
34 env = Environment(

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 35–36why, one step back
35 loader=PackageLoader("yourapp"),
36 autoescape=select_autoescape()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 37–38why, one step back
37 )
38

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 39–43why, one step back
39This will create a template environment with a loader that looks up
40templates in the ``templates`` folder inside the ``yourapp`` Python41package (or next to the ``yourapp.py`` Python module). It also enables42autoescaping for HTML files. This loader only requires that ``yourapp``43is importable, it figures out the absolute path to the folder for you.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 44why, one step back
44

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 45–48why, one step back
45Different loaders are available to load templates in other ways or from
46other locations. They're listed in the `Loaders`_ section below. You can47also write your own if you want to load templates from a source that's48more specialized to your project.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 49why, one step back
49

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 50–51why, one step back
50To load a template from this environment, call the :meth:`get_template`
51method, which returns the loaded :class:`Template`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 52why, one step back
52

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

line 53why, one step back
53.. code-block:: python

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 54why, one step back
54

Merge pull request #1224 from pallets/loader-docsDavid Lordbefore adoptionca8b0b0

lines 55–61why, one step back
55 template = env.get_template("mytemplate.html")
5657To render it with some variables, call the :meth:`render` method.5859.. code-block:: python6061 print(template.render(the="variables", go="here"))

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 62–68why, one step back
62
63Using a template loader rather than passing strings to :class:`Template`64or :meth:`Environment.from_string` has multiple advantages. Besides being65a lot easier to use it also enables template inheritance.6667.. admonition:: Notes on Autoescaping68

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 69why, one step back
69 In future versions of Jinja we might enable autoescaping by default

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 70–77why, one step back
70 for security reasons. As such you are encouraged to explicitly
71 configure autoescaping now instead of relying on the default.727374High Level API75--------------7677The high-level API is the API you will use in the application to load and

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 78–79why, one step back
78render Jinja templates. The :ref:`low-level-api` on the other side is only
79useful if you want to dig deeper into Jinja or :ref:`develop extensions

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 80–98why, one step back
80<jinja-extensions>`.
8182.. autoclass:: Environment([options])83 :members: from_string, get_template, select_template,84 get_or_select_template, join_path, extend, compile_expression,85 compile_templates, list_templates, add_extension8687 .. attribute:: shared8889 If a template was created by using the :class:`Template` constructor90 an environment is created automatically. These environments are91 created as shared environments which means that multiple templates92 may have the same anonymous environment. For all shared environments93 this attribute is `True`, else `False`.9495 .. attribute:: sandboxed9697 If the environment is sandboxed this attribute is `True`. For the98 sandbox mode have a look at the documentation for the

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 99why, one step back
99 :class:`~jinja2.sandbox.SandboxedEnvironment`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 100–116why, one step back
100
101 .. attribute:: filters102103 A dict of filters for this environment. As long as no template was104 loaded it's safe to add new filters or remove old. For custom filters105 see :ref:`writing-filters`. For valid filter names have a look at106 :ref:`identifier-naming`.107108 .. attribute:: tests109110 A dict of test functions for this environment. As long as no111 template was loaded it's safe to modify this dict. For custom tests112 see :ref:`writing-tests`. For valid test names have a look at113 :ref:`identifier-naming`.114115 .. attribute:: globals116

Merge pull request #1374 from pallets/docs-globalsDavid Lordbefore adoption1c863d0

lines 117–120why, one step back
117 A dict of variables that are available in every template loaded
118 by the environment. As long as no template was loaded it's safe119 to modify this. For more details see :ref:`global-namespace`.120 For valid object names see :ref:`identifier-naming`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 121–138why, one step back
121
122 .. attribute:: policies123124 A dictionary with :ref:`policies`. These can be reconfigured to125 change the runtime behavior or certain template features. Usually126 these are security related.127128 .. attribute:: code_generator_class129130 The class used for code generation. This should not be changed131 in most cases, unless you need to modify the Python code a132 template compiles to.133134 .. attribute:: context_class135136 The context used for templates. This should not be changed137 in most cases, unless you need to modify internals of how138 template variables are handled. For details, see

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 139why, one step back
139 :class:`~jinja2.runtime.Context`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 140–182why, one step back
140
141 .. automethod:: overlay([options])142143 .. method:: undefined([hint, obj, name, exc])144145 Creates a new :class:`Undefined` object for `name`. This is useful146 for filters or functions that may return undefined objects for147 some operations. All parameters except of `hint` should be provided148 as keyword parameters for better readability. The `hint` is used as149 error message for the exception if provided, otherwise the error150 message will be generated from `obj` and `name` automatically. The exception151 provided as `exc` is raised if something with the generated undefined152 object is done that the undefined object does not allow. The default153 exception is :exc:`UndefinedError`. If a `hint` is provided the154 `name` may be omitted.155156 The most common way to create an undefined object is by providing157 a name only::158159 return environment.undefined(name='some_name')160161 This means that the name `some_name` is not defined. If the name162 was from an attribute of an object it makes sense to tell the163 undefined object the holder object to improve the error message::164165 if not hasattr(obj, 'attr'):166 return environment.undefined(obj=obj, name='attr')167168 For a more complex example you can provide a hint. For example169 the :func:`first` filter creates an undefined object that way::170171 return environment.undefined('no first item, sequence was empty')172173 If it the `name` or `obj` is known (for example because an attribute174 was accessed) it should be passed to the undefined object, even if175 a custom `hint` is provided. This gives undefined objects the176 possibility to enhance the error message.177178.. autoclass:: Template179 :members: module, make_module180181 .. attribute:: globals182

Merge pull request #1374 from pallets/docs-globalsDavid Lordbefore adoption1c863d0

lines 183–187why, one step back
183 A dict of variables that are available every time the template
184 is rendered, without needing to pass them during render. This185 should not be modified, as depending on how the template was186 loaded it may be shared with the environment and other187 templates.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 188why, one step back
188

Merge pull request #1374 from pallets/docs-globalsDavid Lordbefore adoption1c863d0

lines 189–197why, one step back
189 Defaults to :attr:`Environment.globals` unless extra values are
190 passed to :meth:`Environment.get_template`.191192 Globals are only intended for data that is common to every193 render of the template. Specific data should be passed to194 :meth:`render`.195196 See :ref:`global-namespace`.197

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 198–218why, one step back
198 .. attribute:: name
199200 The loading name of the template. If the template was loaded from a201 string this is `None`.202203 .. attribute:: filename204205 The filename of the template on the file system if it was loaded from206 there. Otherwise this is `None`.207208 .. automethod:: render([context])209210 .. automethod:: generate([context])211212 .. automethod:: stream([context])213214 .. automethod:: render_async([context])215216 .. automethod:: generate_async([context])217218

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 219why, one step back
219.. autoclass:: jinja2.environment.TemplateStream()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 220–227why, one step back
220 :members: disable_buffering, enable_buffering, dump
221222223Autoescaping224------------225226.. versionchanged:: 2.4227

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 228why, one step back
228Jinja now comes with autoescaping support. As of Jinja 2.9 the

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 229–234why, one step back
229autoescape extension is removed and built-in. However autoescaping is
230not yet enabled by default though this will most likely change in the231future. It's recommended to configure a sensible default for232autoescaping. This makes it possible to enable and disable autoescaping233on a per-template basis (HTML versus text for instance).234

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 235why, one step back
235.. autofunction:: jinja2.select_autoescape

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 236–238why, one step back
236
237Here a recommended setup that enables autoescaping for templates ending238in ``'.html'``, ``'.htm'`` and ``'.xml'`` and disabling it by default

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 239why, one step back
239for all other extensions. You can use the :func:`~jinja2.select_autoescape`

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 240–241why, one step back
240function for this::
241

Merge branch '2.11.x'David Lordbefore adoption076d380

line 242why, one step back
242 from jinja2 import Environment, PackageLoader, select_autoescape

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 243–267why, one step back
243 env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']),
244 loader=PackageLoader('mypackage'))245246The :func:`~jinja.select_autoescape` function returns a function that247works roughly like this::248249 def autoescape(template_name):250 if template_name is None:251 return False252 if template_name.endswith(('.html', '.htm', '.xml'))253254When implementing a guessing autoescape function, make sure you also255accept `None` as valid template name. This will be passed when generating256templates from strings. You should always configure autoescaping as257defaults in the future might change.258259Inside the templates the behaviour can be temporarily changed by using260the `autoescape` block (see :ref:`autoescape-overrides`).261262263.. _identifier-naming:264265Notes on Identifiers266--------------------267

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 268why, one step back
268Jinja uses Python naming rules. Valid identifiers can be any combination

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 269why, one step back
269of characters accepted by Python.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 270–273why, one step back
270
271Filters and tests are looked up in separate namespaces and have slightly272modified identifier syntax. Filters and tests may contain dots to group273filters and tests by topic. For example it's perfectly valid to add a

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 274why, one step back
274function into the filter dict and call it `to.str`. The regular

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 275–291why, one step back
275expression for filter and test identifiers is
276``[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*```.277278279Undefined Types280---------------281282These classes can be used as undefined types. The :class:`Environment`283constructor takes an `undefined` parameter that can be one of those classes284or a custom subclass of :class:`Undefined`. Whenever the template engine is285unable to look up a name or access an attribute one of those objects is286created and returned. Some operations on undefined values are then allowed,287others fail.288289The closest to regular Python behavior is the :class:`StrictUndefined` which290disallows all operations beside testing if it's an undefined object.291

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 292why, one step back
292.. autoclass:: jinja2.Undefined()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 293–295why, one step back
293
294 .. attribute:: _undefined_hint295

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

lines 296–297why, one step back
296 Either `None` or a string with the error message for the
297 undefined object.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 298–319why, one step back
298
299 .. attribute:: _undefined_obj300301 Either `None` or the owner object that caused the undefined object302 to be created (for example because an attribute does not exist).303304 .. attribute:: _undefined_name305306 The name for the undefined variable / attribute or just `None`307 if no such information exists.308309 .. attribute:: _undefined_exception310311 The exception that the undefined object wants to raise. This312 is usually one of :exc:`UndefinedError` or :exc:`SecurityError`.313314 .. method:: _fail_with_undefined_error(\*args, \**kwargs)315316 When called with any arguments this method raises317 :attr:`_undefined_exception` with an error message generated318 from the undefined hints stored on the undefined object.319

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 320why, one step back
320.. autoclass:: jinja2.ChainableUndefined()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 321why, one step back
321

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 322why, one step back
322.. autoclass:: jinja2.DebugUndefined()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 323why, one step back
323

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 324why, one step back
324.. autoclass:: jinja2.StrictUndefined()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 325–328why, one step back
325
326There is also a factory function that can decorate undefined objects to327implement logging on failures:328

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 329why, one step back
329.. autofunction:: jinja2.make_logging_undefined

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 330–334why, one step back
330
331Undefined objects are created by calling :attr:`undefined`.332333.. admonition:: Implementation334

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

lines 335–340why, one step back
335 :class:`Undefined` is implemented by overriding the special
336 ``__underscore__`` methods. For example the default337 :class:`Undefined` class implements ``__str__`` to returns an empty338 string, while ``__int__`` and others fail with an exception. To339 allow conversion to int by returning ``0`` you can implement your340 own subclass.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 341why, one step back
341

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

lines 342–343why, one step back
342 .. code-block:: python
343

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 344–346why, one step back
344 class NullUndefined(Undefined):
345 def __int__(self):346 return 0

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 347why, one step back
347

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 348–350why, one step back
348 def __float__(self):
349 return 0.0350

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

lines 351–356why, one step back
351 To disallow a method, override it and raise
352 :attr:`~Undefined._undefined_exception`. Because this is very353 common there is the helper method354 :meth:`~Undefined._fail_with_undefined_error` that raises the error355 with the correct information. Here's a class that works like the356 regular :class:`Undefined` but fails on iteration::

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 357–358why, one step back
357
358 class NonIterableUndefined(Undefined):

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

lines 359–360why, one step back
359 def __iter__(self):
360 self._fail_with_undefined_error()

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 361–365why, one step back
361
362363The Context364-----------365

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 366why, one step back
366.. autoclass:: jinja2.runtime.Context()

Merge pull request #1393 from pallets/deprecate-legacy-resolveDavid Lordbefore adoption06230b0

line 367why, one step back
367 :members: get, resolve, resolve_or_missing, get_exported, get_all

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 368–409why, one step back
368
369 .. attribute:: parent370371 A dict of read only, global variables the template looks up. These372 can either come from another :class:`Context`, from the373 :attr:`Environment.globals` or :attr:`Template.globals` or points374 to a dict created by combining the globals with the variables375 passed to the render function. It must not be altered.376377 .. attribute:: vars378379 The template local variables. This list contains environment and380 context functions from the :attr:`parent` scope as well as local381 modifications and exported variables from the template. The template382 will modify this dict during template evaluation but filters and383 context functions are not allowed to modify it.384385 .. attribute:: environment386387 The environment that loaded the template.388389 .. attribute:: exported_vars390391 This set contains all the names the template exports. The values for392 the names are in the :attr:`vars` dict. In order to get a copy of the393 exported variables as dict, :meth:`get_exported` can be used.394395 .. attribute:: name396397 The load name of the template owning this context.398399 .. attribute:: blocks400401 A dict with the current mapping of blocks in the template. The keys402 in this dict are the names of the blocks, and the values a list of403 blocks registered. The last item in each list is the current active404 block (latest in the inheritance chain).405406 .. attribute:: eval_ctx407408 The current :ref:`eval-context`.409

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 410why, one step back
410 .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs)

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 411–415why, one step back
411
412413.. admonition:: Implementation414415 Context is immutable for the same reason Python's frame locals are

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 416why, one step back
416 immutable inside functions. Both Jinja and Python are not using the

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 417–419why, one step back
417 context / frame locals as data storage for variables but only as primary
418 data source.419

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 420why, one step back
420 When a template accesses a variable the template does not define, Jinja

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 421–436why, one step back
421 looks up the variable in the context, after that the variable is treated
422 as if it was defined in the template.423424425.. _loaders:426427Loaders428-------429430Loaders are responsible for loading templates from a resource such as the431file system. The environment will keep the compiled modules in memory like432Python's `sys.modules`. Unlike `sys.modules` however this cache is limited in433size by default and templates are automatically reloaded.434All loaders are subclasses of :class:`BaseLoader`. If you want to create your435own loader, subclass :class:`BaseLoader` and override `get_source`.436

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 437why, one step back
437.. autoclass:: jinja2.BaseLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 438–439why, one step back
438 :members: get_source, load
439

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 440why, one step back
440Here a list of the builtin loaders Jinja provides:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 441why, one step back
441

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 442why, one step back
442.. autoclass:: jinja2.FileSystemLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 443why, one step back
443

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 444why, one step back
444.. autoclass:: jinja2.PackageLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 445why, one step back
445

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 446why, one step back
446.. autoclass:: jinja2.DictLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 447why, one step back
447

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 448why, one step back
448.. autoclass:: jinja2.FunctionLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 449why, one step back
449

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 450why, one step back
450.. autoclass:: jinja2.PrefixLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 451why, one step back
451

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 452why, one step back
452.. autoclass:: jinja2.ChoiceLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 453why, one step back
453

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 454why, one step back
454.. autoclass:: jinja2.ModuleLoader

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 455–471why, one step back
455
456457.. _bytecode-cache:458459Bytecode Cache460--------------461462Jinja 2.1 and higher support external bytecode caching. Bytecode caches make463it possible to store the generated bytecode on the file system or a different464location to avoid parsing the templates on first use.465466This is especially useful if you have a web application that is initialized on467the first request and Jinja compiles many templates at once which slows down468the application.469470To use a bytecode cache, instantiate it and pass it to the :class:`Environment`.471

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 472why, one step back
472.. autoclass:: jinja2.BytecodeCache

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 473–474why, one step back
473 :members: load_bytecode, dump_bytecode, clear
474

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 475why, one step back
475.. autoclass:: jinja2.bccache.Bucket

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 476–493why, one step back
476 :members: write_bytecode, load_bytecode, bytecode_from_string,
477 bytecode_to_string, reset478479 .. attribute:: environment480481 The :class:`Environment` that created the bucket.482483 .. attribute:: key484485 The unique cache key for this bucket486487 .. attribute:: code488489 The bytecode if it's loaded, otherwise `None`.490491492Builtin bytecode caches:493

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 494why, one step back
494.. autoclass:: jinja2.FileSystemBytecodeCache

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 495why, one step back
495

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 496why, one step back
496.. autoclass:: jinja2.MemcachedBytecodeCache

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 497–501why, one step back
497
498499Async Support500-------------501

Merge branch '2.11.x'David Lordbefore adoption45a76a3

line 502why, one step back
502.. versionadded:: 2.9

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 503why, one step back
503

Merge branch '2.11.x'David Lordbefore adoption45a76a3

lines 504–508why, one step back
504Jinja supports the Python ``async`` and ``await`` syntax. For the
505template designer, this support (when enabled) is entirely transparent,506templates continue to look exactly the same. However, developers should507be aware of the implementation as it affects what types of APIs you can508use.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 509why, one step back
509

Merge branch '2.11.x'David Lordbefore adoption45a76a3

lines 510–513why, one step back
510By default, async support is disabled. Enabling it will cause the
511environment to compile different code behind the scenes in order to512handle async and sync code in an asyncio event loop. This has the513following implications:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 514why, one step back
514

Merge branch '2.11.x'David Lordbefore adoption45a76a3

line 515why, one step back
515- Template rendering requires an event loop to be available to the

Merge pull request #1444 from pallets/event-loopDavid Lordbefore adoption77674b9

lines 516–517why, one step back
516 current thread. :func:`asyncio.get_running_loop` must return an
517 event loop.

Merge branch '2.11.x'David Lordbefore adoption45a76a3

lines 518–520why, one step back
518- The compiled code uses ``await`` for functions and attributes, and
519 uses ``async for`` loops. In order to support using both async and520 sync functions in this context, a small wrapper is placed around

Merge pull request #1444 from pallets/event-loopDavid Lordbefore adoption77674b9

line 521why, one step back
521 all calls and access, which adds overhead compared to purely async

Merge branch '2.11.x'David Lordbefore adoption45a76a3

lines 522–526why, one step back
522 code.
523- Sync methods and filters become wrappers around their corresponding524 async implementations where needed. For example, ``render`` invokes525 ``async_render``, and ``|map`` supports async iterables.526

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 527why, one step back
527Awaitable objects can be returned from functions in templates and any

Merge branch '2.11.x'David Lordbefore adoption45a76a3

lines 528–532why, one step back
528function call in a template will automatically await the result. The
529``await`` you would normally add in Python is implied. For example, you530can provide a method that asynchronously loads data from a database, and531from the template designer's point of view it can be called like any532other function.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 533–542why, one step back
533
534535.. _policies:536537Policies538--------539540Starting with Jinja 2.9 policies can be configured on the environment541which can slightly influence how filters and other template constructs542behave. They can be configured with the

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 543why, one step back
543:attr:`~jinja2.Environment.policies` attribute.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 544–563why, one step back
544
545Example::546547 env.policies['urlize.rel'] = 'nofollow noopener'548549``truncate.leeway``:550 Configures the leeway default for the `truncate` filter. Leeway as551 introduced in 2.9 but to restore compatibility with older templates552 it can be configured to `0` to get the old behavior back. The default553 is `5`.554555``urlize.rel``:556 A string that defines the items for the `rel` attribute of generated557 links with the `urlize` filter. These items are always added. The558 default is `noopener`.559560``urlize.target``:561 The default target that is issued for links from the `urlize` filter562 if no other target is defined by the call explicitly.563

Merge pull request #1195 from bebleo/add_mailto_link_supportDavid Lordbefore adoptionf1b1caf

lines 564–567why, one step back
564``urlize.extra_schemes``:
565 Recognize URLs that start with these schemes in addition to the566 default ``http://``, ``https://``, and ``mailto:``.567

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 568–591why, one step back
568``json.dumps_function``:
569 If this is set to a value other than `None` then the `tojson` filter570 will dump with this function instead of the default one. Note that571 this function should accept arbitrary extra arguments which might be572 passed in the future from the filter. Currently the only argument573 that might be passed is `indent`. The default dump function is574 ``json.dumps``.575576``json.dumps_kwargs``:577 Keyword arguments to be passed to the dump function. The default is578 ``{'sort_keys': True}``.579580.. _ext-i18n-trimmed:581582``ext.i18n.trimmed``:583 If this is set to `True`, ``{% trans %}`` blocks of the584 :ref:`i18n-extension` will always unify linebreaks and surrounding585 whitespace as if the `trimmed` modifier was used.586587588Utilities589---------590591These helper functions and classes are useful if you add custom filters or

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 592why, one step back
592functions to a Jinja environment.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 593why, one step back
593

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 594why, one step back
594.. autofunction:: jinja2.pass_context

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 595why, one step back
595

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 596–599why, one step back
596.. autofunction:: jinja2.pass_eval_context
597598.. autofunction:: jinja2.pass_environment599

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 600why, one step back
600.. autofunction:: jinja2.contextfilter

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 601why, one step back
601

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 602why, one step back
602.. autofunction:: jinja2.evalcontextfilter

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 603why, one step back
603

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 604why, one step back
604.. autofunction:: jinja2.environmentfilter

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 605why, one step back
605

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 606why, one step back
606.. autofunction:: jinja2.contextfunction

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 607why, one step back
607

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 608why, one step back
608.. autofunction:: jinja2.evalcontextfunction

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 609why, one step back
609

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 610–611why, one step back
610.. autofunction:: jinja2.environmentfunction
611

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 612why, one step back
612.. autofunction:: jinja2.clear_caches

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 613why, one step back
613

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 614why, one step back
614.. autofunction:: jinja2.is_undefined

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 615–619why, one step back
615
616617Exceptions618----------619

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 620why, one step back
620.. autoexception:: jinja2.TemplateError

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 621why, one step back
621

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 622why, one step back
622.. autoexception:: jinja2.UndefinedError

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 623why, one step back
623

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 624why, one step back
624.. autoexception:: jinja2.TemplateNotFound

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 625why, one step back
625

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 626why, one step back
626.. autoexception:: jinja2.TemplatesNotFound

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 627why, one step back
627

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 628why, one step back
628.. autoexception:: jinja2.TemplateSyntaxError

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 629–631why, one step back
629
630 .. attribute:: message631

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 632why, one step back
632 The error message.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 633–635why, one step back
633
634 .. attribute:: lineno635

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 636why, one step back
636 The line number where the error occurred.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 637–639why, one step back
637
638 .. attribute:: name639

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 640why, one step back
640 The load name for the template.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 641–643why, one step back
641
642 .. attribute:: filename643

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

lines 644–645why, one step back
644 The filename that loaded the template in the encoding of the
645 file system (most likely utf-8, or mbcs on Windows systems).

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 646why, one step back
646

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 647why, one step back
647.. autoexception:: jinja2.TemplateRuntimeError

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 648why, one step back
648

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 649why, one step back
649.. autoexception:: jinja2.TemplateAssertionError

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 650–656why, one step back
650
651652.. _writing-filters:653654Custom Filters655--------------656

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 657–659why, one step back
657Filters are Python functions that take the value to the left of the
658filter as the first argument and produce a new value. Arguments passed659to the filter are passed after the value.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 660why, one step back
660

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 661–662why, one step back
661For example, the filter ``{{ 42|myfilter(23) }}`` is called behind the
662scenes as ``myfilter(42, 23)``.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 663why, one step back
663

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 664–666why, one step back
664Jinja comes with some :ref:`built-in filters <builtin-filters>`. To use
665a custom filter, write a function that takes at least a ``value``666argument, then register it in :attr:`Environment.filters`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 667why, one step back
667

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 668why, one step back
668Here's a filter that formats datetime objects:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 669why, one step back
669

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 670why, one step back
670.. code-block:: python

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 671why, one step back
671

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 672–673why, one step back
672 def datetime_format(value, format="%H:%M %d-%m-%y"):
673 return value.strftime(format)

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 674why, one step back
674

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 675–678why, one step back
675 environment.filters["datetime_format"] = datetime_format
676677Now it can be used in templates:678

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 679–680why, one step back
679.. sourcecode:: jinja
680

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 681–682why, one step back
681 {{ article.pub_date|datetimeformat }}
682 {{ article.pub_date|datetimeformat("%B %Y") }}

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 683why, one step back
683

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 684–686why, one step back
684Some decorators are available to tell Jinja to pass extra information to
685the filter. The object is passed as the first argument, making the value686being filtered the second argument.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 687why, one step back
687

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 688–690why, one step back
688- :func:`pass_environment` passes the :class:`Environment`.
689- :func:`pass_eval_context` passes the :ref:`eval-context`.690- :func:`pass_context` passes the current

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 691why, one step back
691 :class:`~jinja2.runtime.Context`.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 692why, one step back
692

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 693–695why, one step back
693Here's a filter that converts line breaks into HTML ``<br>`` and ``<p>``
694tags. It uses the eval context to check if autoescape is currently695enabled before escaping the input and marking the output safe.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 696why, one step back
696

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 697why, one step back
697.. code-block:: python

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 698why, one step back
698

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 699why, one step back
699 import re

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 700why, one step back
700 from jinja2 import pass_eval_context

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 701–702why, one step back
701 from markupsafe import Markup, escape
702

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 703why, one step back
703 @pass_eval_context

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 704why, one step back
704 def nl2br(eval_ctx, value):

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 705–710why, one step back
705 br = "<br>\n"
706707 if eval_ctx.autoescape:708 value = escape(value)709 br = Markup(br)710

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 711why, one step back
711 result = "\n\n".join(

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 712–713why, one step back
712 f"<p>{br.join(p.splitlines())}<\p>"
713 for p in re.split(r"(?:\r\n|\r(?!\n)|\n){2,}", value)

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 714why, one step back
714 )

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 715why, one step back
715 return Markup(result) if autoescape else result

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 716–717why, one step back
716
717

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

line 718why, one step back
718.. _writing-tests:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 719why, one step back
719

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 720–765why, one step back
720Custom Tests
721------------722723Test are Python functions that take the value to the left of the test as724the first argument, and return ``True`` or ``False``. Arguments passed725to the test are passed after the value.726727For example, the test ``{{ 42 is even }}`` is called behind the scenes728as ``is_even(42)``.729730Jinja comes with some :ref:`built-in tests <builtin-tests>`. To use a731custom tests, write a function that takes at least a ``value`` argument,732then register it in :attr:`Environment.tests`.733734Here's a test that checks if a value is a prime number:735736.. code-block:: python737738 import math739740 def is_prime(n):741 if n == 2:742 return True743744 for i in range(2, int(math.ceil(math.sqrt(n))) + 1):745 if n % i == 0:746 return False747748 return True749750 environment.tests["prime"] = is_prime751752Now it can be used in templates:753754.. sourcecode:: jinja755756 {% if value is prime %}757 {{ value }} is a prime number758 {% else %}759 {{ value }} is not a prime number760 {% endif %}761762Some decorators are available to tell Jinja to pass extra information to763the filter. The object is passed as the first argument, making the value764being filtered the second argument.765

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 766–768why, one step back
766- :func:`pass_environment` passes the :class:`Environment`.
767- :func:`pass_eval_context` passes the :ref:`eval-context`.768- :func:`pass_context` passes the current

Merge pull request #1383 from pallets/test-decoratorsDavid Lordbefore adoptionbeabf30

lines 769–771why, one step back
769 :class:`~jinja2.runtime.Context`.
770771

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 772–776why, one step back
772.. _eval-context:
773774Evaluation Context775------------------776

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 777–778why, one step back
777The evaluation context (short eval context or eval ctx) makes it
778possible to activate and deactivate compiled features at runtime.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 779why, one step back
779

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 780–781why, one step back
780Currently it is only used to enable and disable automatic escaping, but
781it can be used by extensions as well.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 782why, one step back
782

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 783–785why, one step back
783The ``autoescape`` setting should be checked on the evaluation context,
784not the environment. The evaluation context will have the computed value785for the current template.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 786why, one step back
786

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 787why, one step back
787Instead of ``pass_environment``:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 788why, one step back
788

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 789–791why, one step back
789.. code-block:: python
790791 @pass_environment

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 792–793why, one step back
792 def filter(env, value):
793 result = do_something(value)

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 794why, one step back
794

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 795–796why, one step back
795 if env.autoescape:
796 result = Markup(result)

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 797why, one step back
797

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 798–799why, one step back
798 return result
799

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 800why, one step back
800Use ``pass_eval_context`` if you only need the setting:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 801why, one step back
801

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 802–805why, one step back
802.. code-block:: python
803804 @pass_eval_context805 def filter(eval_ctx, value):

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 806why, one step back
806 result = do_something(value)

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 807–808why, one step back
807
808 if eval_ctx.autoescape:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 809why, one step back
809 result = Markup(result)

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 810why, one step back
810

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 811–812why, one step back
811 return result
812

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 813–818why, one step back
813Or use ``pass_context`` if you need other context behavior as well:
814815.. code-block:: python816817 @pass_context818 def filter(context, value):

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 819why, one step back
819 result = do_something(value)

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

lines 820–821why, one step back
820
821 if context.eval_ctx.autoescape:

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 822why, one step back
822 result = Markup(result)

Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8

line 823why, one step back
823

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 824–830why, one step back
824 return result
825826The evaluation context must not be modified at runtime. Modifications827must only happen with a :class:`nodes.EvalContextModifier` and828:class:`nodes.ScopedEvalContextModifier` from an extension, not on the829eval context object itself.830

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 831why, one step back
831.. autoclass:: jinja2.nodes.EvalContext

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 832–847why, one step back
832
833 .. attribute:: autoescape834835 `True` or `False` depending on if autoescaping is active or not.836837 .. attribute:: volatile838839 `True` if the compiler cannot evaluate some expressions at compile840 time. At runtime this should always be `False`.841842843.. _global-namespace:844845The Global Namespace846--------------------847

Merge pull request #1374 from pallets/docs-globalsDavid Lordbefore adoption1c863d0

lines 848–868why, one step back
848The global namespace stores variables and functions that should be
849available without needing to pass them to :meth:`Template.render`. They850are also available to templates that are imported or included without851context. Most applications should only use :attr:`Environment.globals`.852853:attr:`Environment.globals` are intended for data that is common to all854templates loaded by that environment. :attr:`Template.globals` are855intended for data that is common to all renders of that template, and856default to :attr:`Environment.globals` unless they're given in857:meth:`Environment.get_template`, etc. Data that is specific to a858render should be passed as context to :meth:`Template.render`.859860Only one set of globals is used during any specific rendering. If861templates A and B both have template globals, and B extends A, then862only B's globals are used for both when using ``b.render()``.863864Environment globals should not be changed after loading any templates,865and template globals should not be changed at any time after loading the866template. Changing globals after loading a template will result in867unexpected behavior as they may be shared between the environment and868other templates.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 869–894why, one step back
869
870871.. _low-level-api:872873Low Level API874-------------875876The low level API exposes functionality that can be useful to understand some877implementation details, debugging purposes or advanced :ref:`extension878<jinja-extensions>` techniques. Unless you know exactly what you are doing we879don't recommend using any of those.880881.. automethod:: Environment.lex882883.. automethod:: Environment.parse884885.. automethod:: Environment.preprocess886887.. automethod:: Template.new_context888889.. method:: Template.root_render_func(context)890891 This is the low level render function. It's passed a :class:`Context`892 that has to be created by :meth:`new_context` of the same template or893 a compatible template. This render function is generated by the894 compiler from the template code and returns a generator that yields

Merge pull request #1136 from pallets/drop-python2David Lordbefore adoption3915eb5

line 895why, one step back
895 strings.

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 896–913why, one step back
896
897 If an exception in the template code happens the template engine will898 not rewrite the exception but pass through the original one. As a899 matter of fact this function should only be called from within a900 :meth:`render` / :meth:`generate` / :meth:`stream` call.901902.. attribute:: Template.blocks903904 A dict of block render functions. Each of these functions works exactly905 like the :meth:`root_render_func` with the same limitations.906907.. attribute:: Template.is_up_to_date908909 This attribute is `False` if there is a newer version of the template910 available, otherwise `True`.911912.. admonition:: Note913

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 914–916why, one step back
914 The low-level API is fragile. Future Jinja versions will try not to
915 change it in a backwards incompatible way but modifications in the Jinja916 core may shine through. For example if Jinja introduces a new AST node

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

lines 917–928why, one step back
917 in later versions that may be returned by :meth:`~Environment.parse`.
918919The Meta API920------------921922.. versionadded:: 2.2923924The meta API returns some information about abstract syntax trees that925could help applications to implement more advanced template concepts. All926the functions of the meta API operate on an abstract syntax tree as927returned by the :meth:`Environment.parse` method.928

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 929why, one step back
929.. autofunction:: jinja2.meta.find_undeclared_variables

Merge pull request #1079 from ThiefMaster/cond-expr-undefinedDavid Lordbefore adoption8146e45

line 930why, one step back
930

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

line 931why, one step back
931.. autofunction:: jinja2.meta.find_referenced_templates