jinja2.environment

Classes for managing templates and their runtime and compile time options.

   1"""Classes for managing templates and their runtime and compile time
   2options.
   3"""
   4
   5import os
   6import typing
   7import typing as t
   8import weakref
   9from collections import ChainMap
  10from functools import lru_cache
  11from functools import partial
  12from functools import reduce
  13from types import CodeType
  14
  15from markupsafe import Markup
  16
  17from . import nodes
  18from .compiler import CodeGenerator
  19from .compiler import generate
  20from .defaults import BLOCK_END_STRING
  21from .defaults import BLOCK_START_STRING
  22from .defaults import COMMENT_END_STRING
  23from .defaults import COMMENT_START_STRING
  24from .defaults import DEFAULT_FILTERS  # type: ignore[attr-defined]
  25from .defaults import DEFAULT_NAMESPACE
  26from .defaults import DEFAULT_POLICIES
  27from .defaults import DEFAULT_TESTS  # type: ignore[attr-defined]
  28from .defaults import KEEP_TRAILING_NEWLINE
  29from .defaults import LINE_COMMENT_PREFIX
  30from .defaults import LINE_STATEMENT_PREFIX
  31from .defaults import LSTRIP_BLOCKS
  32from .defaults import NEWLINE_SEQUENCE
  33from .defaults import TRIM_BLOCKS
  34from .defaults import VARIABLE_END_STRING
  35from .defaults import VARIABLE_START_STRING
  36from .exceptions import TemplateNotFound
  37from .exceptions import TemplateRuntimeError
  38from .exceptions import TemplatesNotFound
  39from .exceptions import TemplateSyntaxError
  40from .exceptions import UndefinedError
  41from .lexer import get_lexer
  42from .lexer import Lexer
  43from .lexer import TokenStream
  44from .nodes import EvalContext
  45from .parser import Parser
  46from .runtime import Context
  47from .runtime import new_context
  48from .runtime import Undefined
  49from .utils import _PassArg
  50from .utils import concat
  51from .utils import consume
  52from .utils import import_string
  53from .utils import internalcode
  54from .utils import LRUCache
  55from .utils import missing
  56
  57if t.TYPE_CHECKING:
  58    import typing_extensions as te
  59
  60    from .bccache import BytecodeCache
  61    from .ext import Extension
  62    from .loaders import BaseLoader
  63
  64_env_bound = t.TypeVar("_env_bound", bound="Environment")
  65
  66
  67# for direct template usage we have up to ten living environments
  68@lru_cache(maxsize=10)
  69def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_bound:
  70    """Return a new spontaneous environment. A spontaneous environment
  71    is used for templates created directly rather than through an
  72    existing environment.
  73
  74    :param cls: Environment class to create.
  75    :param args: Positional arguments passed to environment.
  76    """
  77    env = cls(*args)
  78    env.shared = True
  79    return env
  80
  81
  82def create_cache(
  83    size: int,
  84) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
  85    """Return the cache class for the given size."""
  86    if size == 0:
  87        return None
  88
  89    if size < 0:
  90        return {}
  91
  92    return LRUCache(size)  # type: ignore
  93
  94
  95def copy_cache(
  96    cache: t.Optional[t.MutableMapping[t.Any, t.Any]],
  97) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
  98    """Create an empty copy of the given cache."""
  99    if cache is None:
 100        return None
 101
 102    if type(cache) is dict:  # noqa E721
 103        return {}
 104
 105    return LRUCache(cache.capacity)  # type: ignore
 106
 107
 108def load_extensions(
 109    environment: "Environment",
 110    extensions: t.Sequence[t.Union[str, t.Type["Extension"]]],
 111) -> t.Dict[str, "Extension"]:
 112    """Load the extensions from the list and bind it to the environment.
 113    Returns a dict of instantiated extensions.
 114    """
 115    result = {}
 116
 117    for extension in extensions:
 118        if isinstance(extension, str):
 119            extension = t.cast(t.Type["Extension"], import_string(extension))
 120
 121        result[extension.identifier] = extension(environment)
 122
 123    return result
 124
 125
 126def _environment_config_check(environment: _env_bound) -> _env_bound:
 127    """Perform a sanity check on the environment."""
 128    assert issubclass(
 129        environment.undefined, Undefined
 130    ), "'undefined' must be a subclass of 'jinja2.Undefined'."
 131    assert (
 132        environment.block_start_string
 133        != environment.variable_start_string
 134        != environment.comment_start_string
 135    ), "block, variable and comment start strings must be different."
 136    assert environment.newline_sequence in {
 137        "\r",
 138        "\r\n",
 139        "\n",
 140    }, "'newline_sequence' must be one of '\\n', '\\r\\n', or '\\r'."
 141    return environment
 142
 143
 144class Environment:
 145    r"""The core component of Jinja is the `Environment`.  It contains
 146    important shared variables like configuration, filters, tests,
 147    globals and others.  Instances of this class may be modified if
 148    they are not shared and if no template was loaded so far.
 149    Modifications on environments after the first template was loaded
 150    will lead to surprising effects and undefined behavior.
 151
 152    Here are the possible initialization parameters:
 153
 154        `block_start_string`
 155            The string marking the beginning of a block.  Defaults to ``'{%'``.
 156
 157        `block_end_string`
 158            The string marking the end of a block.  Defaults to ``'%}'``.
 159
 160        `variable_start_string`
 161            The string marking the beginning of a print statement.
 162            Defaults to ``'{{'``.
 163
 164        `variable_end_string`
 165            The string marking the end of a print statement.  Defaults to
 166            ``'}}'``.
 167
 168        `comment_start_string`
 169            The string marking the beginning of a comment.  Defaults to ``'{#'``.
 170
 171        `comment_end_string`
 172            The string marking the end of a comment.  Defaults to ``'#}'``.
 173
 174        `line_statement_prefix`
 175            If given and a string, this will be used as prefix for line based
 176            statements.  See also :ref:`line-statements`.
 177
 178        `line_comment_prefix`
 179            If given and a string, this will be used as prefix for line based
 180            comments.  See also :ref:`line-statements`.
 181
 182            .. versionadded:: 2.2
 183
 184        `trim_blocks`
 185            If this is set to ``True`` the first newline after a block is
 186            removed (block, not variable tag!).  Defaults to `False`.
 187
 188        `lstrip_blocks`
 189            If this is set to ``True`` leading spaces and tabs are stripped
 190            from the start of a line to a block.  Defaults to `False`.
 191
 192        `newline_sequence`
 193            The sequence that starts a newline.  Must be one of ``'\r'``,
 194            ``'\n'`` or ``'\r\n'``.  The default is ``'\n'`` which is a
 195            useful default for Linux and OS X systems as well as web
 196            applications.
 197
 198        `keep_trailing_newline`
 199            Preserve the trailing newline when rendering templates.
 200            The default is ``False``, which causes a single newline,
 201            if present, to be stripped from the end of the template.
 202
 203            .. versionadded:: 2.7
 204
 205        `extensions`
 206            List of Jinja extensions to use.  This can either be import paths
 207            as strings or extension classes.  For more information have a
 208            look at :ref:`the extensions documentation <jinja-extensions>`.
 209
 210        `optimized`
 211            should the optimizer be enabled?  Default is ``True``.
 212
 213        `undefined`
 214            :class:`Undefined` or a subclass of it that is used to represent
 215            undefined values in the template.
 216
 217        `finalize`
 218            A callable that can be used to process the result of a variable
 219            expression before it is output.  For example one can convert
 220            ``None`` implicitly into an empty string here.
 221
 222        `autoescape`
 223            If set to ``True`` the XML/HTML autoescaping feature is enabled by
 224            default.  For more details about autoescaping see
 225            :class:`~markupsafe.Markup`.  As of Jinja 2.4 this can also
 226            be a callable that is passed the template name and has to
 227            return ``True`` or ``False`` depending on autoescape should be
 228            enabled by default.
 229
 230            .. versionchanged:: 2.4
 231               `autoescape` can now be a function
 232
 233        `loader`
 234            The template loader for this environment.
 235
 236        `cache_size`
 237            The size of the cache.  Per default this is ``400`` which means
 238            that if more than 400 templates are loaded the loader will clean
 239            out the least recently used template.  If the cache size is set to
 240            ``0`` templates are recompiled all the time, if the cache size is
 241            ``-1`` the cache will not be cleaned.
 242
 243            .. versionchanged:: 2.8
 244               The cache size was increased to 400 from a low 50.
 245
 246        `auto_reload`
 247            Some loaders load templates from locations where the template
 248            sources may change (ie: file system or database).  If
 249            ``auto_reload`` is set to ``True`` (default) every time a template is
 250            requested the loader checks if the source changed and if yes, it
 251            will reload the template.  For higher performance it's possible to
 252            disable that.
 253
 254        `bytecode_cache`
 255            If set to a bytecode cache object, this object will provide a
 256            cache for the internal Jinja bytecode so that templates don't
 257            have to be parsed if they were not changed.
 258
 259            See :ref:`bytecode-cache` for more information.
 260
 261        `enable_async`
 262            If set to true this enables async template execution which
 263            allows using async functions and generators.
 264    """
 265
 266    #: if this environment is sandboxed.  Modifying this variable won't make
 267    #: the environment sandboxed though.  For a real sandboxed environment
 268    #: have a look at jinja2.sandbox.  This flag alone controls the code
 269    #: generation by the compiler.
 270    sandboxed = False
 271
 272    #: True if the environment is just an overlay
 273    overlayed = False
 274
 275    #: the environment this environment is linked to if it is an overlay
 276    linked_to: t.Optional["Environment"] = None
 277
 278    #: shared environments have this set to `True`.  A shared environment
 279    #: must not be modified
 280    shared = False
 281
 282    #: the class that is used for code generation.  See
 283    #: :class:`~jinja2.compiler.CodeGenerator` for more information.
 284    code_generator_class: t.Type["CodeGenerator"] = CodeGenerator
 285
 286    concat = "".join
 287
 288    #: the context class that is used for templates.  See
 289    #: :class:`~jinja2.runtime.Context` for more information.
 290    context_class: t.Type[Context] = Context
 291
 292    template_class: t.Type["Template"]
 293
 294    def __init__(
 295        self,
 296        block_start_string: str = BLOCK_START_STRING,
 297        block_end_string: str = BLOCK_END_STRING,
 298        variable_start_string: str = VARIABLE_START_STRING,
 299        variable_end_string: str = VARIABLE_END_STRING,
 300        comment_start_string: str = COMMENT_START_STRING,
 301        comment_end_string: str = COMMENT_END_STRING,
 302        line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
 303        line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
 304        trim_blocks: bool = TRIM_BLOCKS,
 305        lstrip_blocks: bool = LSTRIP_BLOCKS,
 306        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
 307        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
 308        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
 309        optimized: bool = True,
 310        undefined: t.Type[Undefined] = Undefined,
 311        finalize: t.Optional[t.Callable[..., t.Any]] = None,
 312        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
 313        loader: t.Optional["BaseLoader"] = None,
 314        cache_size: int = 400,
 315        auto_reload: bool = True,
 316        bytecode_cache: t.Optional["BytecodeCache"] = None,
 317        enable_async: bool = False,
 318    ):
 319        # !!Important notice!!
 320        #   The constructor accepts quite a few arguments that should be
 321        #   passed by keyword rather than position.  However it's important to
 322        #   not change the order of arguments because it's used at least
 323        #   internally in those cases:
 324        #       -   spontaneous environments (i18n extension and Template)
 325        #       -   unittests
 326        #   If parameter changes are required only add parameters at the end
 327        #   and don't change the arguments (or the defaults!) of the arguments
 328        #   existing already.
 329
 330        # lexer / parser information
 331        self.block_start_string = block_start_string
 332        self.block_end_string = block_end_string
 333        self.variable_start_string = variable_start_string
 334        self.variable_end_string = variable_end_string
 335        self.comment_start_string = comment_start_string
 336        self.comment_end_string = comment_end_string
 337        self.line_statement_prefix = line_statement_prefix
 338        self.line_comment_prefix = line_comment_prefix
 339        self.trim_blocks = trim_blocks
 340        self.lstrip_blocks = lstrip_blocks
 341        self.newline_sequence = newline_sequence
 342        self.keep_trailing_newline = keep_trailing_newline
 343
 344        # runtime information
 345        self.undefined: t.Type[Undefined] = undefined
 346        self.optimized = optimized
 347        self.finalize = finalize
 348        self.autoescape = autoescape
 349
 350        # defaults
 351        self.filters = DEFAULT_FILTERS.copy()
 352        self.tests = DEFAULT_TESTS.copy()
 353        self.globals = DEFAULT_NAMESPACE.copy()
 354
 355        # set the loader provided
 356        self.loader = loader
 357        self.cache = create_cache(cache_size)
 358        self.bytecode_cache = bytecode_cache
 359        self.auto_reload = auto_reload
 360
 361        # configurable policies
 362        self.policies = DEFAULT_POLICIES.copy()
 363
 364        # load extensions
 365        self.extensions = load_extensions(self, extensions)
 366
 367        self.is_async = enable_async
 368        _environment_config_check(self)
 369
 370    def add_extension(self, extension: t.Union[str, t.Type["Extension"]]) -> None:
 371        """Adds an extension after the environment was created.
 372
 373        .. versionadded:: 2.5
 374        """
 375        self.extensions.update(load_extensions(self, [extension]))
 376
 377    def extend(self, **attributes: t.Any) -> None:
 378        """Add the items to the instance of the environment if they do not exist
 379        yet.  This is used by :ref:`extensions <writing-extensions>` to register
 380        callbacks and configuration values without breaking inheritance.
 381        """
 382        for key, value in attributes.items():
 383            if not hasattr(self, key):
 384                setattr(self, key, value)
 385
 386    def overlay(
 387        self,
 388        block_start_string: str = missing,
 389        block_end_string: str = missing,
 390        variable_start_string: str = missing,
 391        variable_end_string: str = missing,
 392        comment_start_string: str = missing,
 393        comment_end_string: str = missing,
 394        line_statement_prefix: t.Optional[str] = missing,
 395        line_comment_prefix: t.Optional[str] = missing,
 396        trim_blocks: bool = missing,
 397        lstrip_blocks: bool = missing,
 398        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = missing,
 399        keep_trailing_newline: bool = missing,
 400        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = missing,
 401        optimized: bool = missing,
 402        undefined: t.Type[Undefined] = missing,
 403        finalize: t.Optional[t.Callable[..., t.Any]] = missing,
 404        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = missing,
 405        loader: t.Optional["BaseLoader"] = missing,
 406        cache_size: int = missing,
 407        auto_reload: bool = missing,
 408        bytecode_cache: t.Optional["BytecodeCache"] = missing,
 409        enable_async: bool = missing,
 410    ) -> "te.Self":
 411        """Create a new overlay environment that shares all the data with the
 412        current environment except for cache and the overridden attributes.
 413        Extensions cannot be removed for an overlayed environment.  An overlayed
 414        environment automatically gets all the extensions of the environment it
 415        is linked to plus optional extra extensions.
 416
 417        Creating overlays should happen after the initial environment was set
 418        up completely.  Not all attributes are truly linked, some are just
 419        copied over so modifications on the original environment may not shine
 420        through.
 421
 422        .. versionchanged:: 3.1.5
 423            ``enable_async`` is applied correctly.
 424
 425        .. versionchanged:: 3.1.2
 426            Added the ``newline_sequence``, ``keep_trailing_newline``,
 427            and ``enable_async`` parameters to match ``__init__``.
 428        """
 429        args = dict(locals())
 430        del args["self"], args["cache_size"], args["extensions"], args["enable_async"]
 431
 432        rv = object.__new__(self.__class__)
 433        rv.__dict__.update(self.__dict__)
 434        rv.overlayed = True
 435        rv.linked_to = self
 436
 437        for key, value in args.items():
 438            if value is not missing:
 439                setattr(rv, key, value)
 440
 441        if cache_size is not missing:
 442            rv.cache = create_cache(cache_size)
 443        else:
 444            rv.cache = copy_cache(self.cache)
 445
 446        rv.extensions = {}
 447        for key, value in self.extensions.items():
 448            rv.extensions[key] = value.bind(rv)
 449        if extensions is not missing:
 450            rv.extensions.update(load_extensions(rv, extensions))
 451
 452        if enable_async is not missing:
 453            rv.is_async = enable_async
 454
 455        return _environment_config_check(rv)
 456
 457    @property
 458    def lexer(self) -> Lexer:
 459        """The lexer for this environment."""
 460        return get_lexer(self)
 461
 462    def iter_extensions(self) -> t.Iterator["Extension"]:
 463        """Iterates over the extensions by priority."""
 464        return iter(sorted(self.extensions.values(), key=lambda x: x.priority))
 465
 466    def getitem(
 467        self, obj: t.Any, argument: t.Union[str, t.Any]
 468    ) -> t.Union[t.Any, Undefined]:
 469        """Get an item or attribute of an object but prefer the item."""
 470        try:
 471            return obj[argument]
 472        except (AttributeError, TypeError, LookupError):
 473            if isinstance(argument, str):
 474                try:
 475                    attr = str(argument)
 476                except Exception:
 477                    pass
 478                else:
 479                    try:
 480                        return getattr(obj, attr)
 481                    except AttributeError:
 482                        pass
 483            return self.undefined(obj=obj, name=argument)
 484
 485    def getattr(self, obj: t.Any, attribute: str) -> t.Any:
 486        """Get an item or attribute of an object but prefer the attribute.
 487        Unlike :meth:`getitem` the attribute *must* be a string.
 488        """
 489        try:
 490            return getattr(obj, attribute)
 491        except AttributeError:
 492            pass
 493        try:
 494            return obj[attribute]
 495        except (TypeError, LookupError, AttributeError):
 496            return self.undefined(obj=obj, name=attribute)
 497
 498    def _filter_test_common(
 499        self,
 500        name: t.Union[str, Undefined],
 501        value: t.Any,
 502        args: t.Optional[t.Sequence[t.Any]],
 503        kwargs: t.Optional[t.Mapping[str, t.Any]],
 504        context: t.Optional[Context],
 505        eval_ctx: t.Optional[EvalContext],
 506        is_filter: bool,
 507    ) -> t.Any:
 508        if is_filter:
 509            env_map = self.filters
 510            type_name = "filter"
 511        else:
 512            env_map = self.tests
 513            type_name = "test"
 514
 515        func = env_map.get(name)  # type: ignore
 516
 517        if func is None:
 518            msg = f"No {type_name} named {name!r}."
 519
 520            if isinstance(name, Undefined):
 521                try:
 522                    name._fail_with_undefined_error()
 523                except Exception as e:
 524                    msg = f"{msg} ({e}; did you forget to quote the callable name?)"
 525
 526            raise TemplateRuntimeError(msg)
 527
 528        args = [value, *(args if args is not None else ())]
 529        kwargs = kwargs if kwargs is not None else {}
 530        pass_arg = _PassArg.from_obj(func)
 531
 532        if pass_arg is _PassArg.context:
 533            if context is None:
 534                raise TemplateRuntimeError(
 535                    f"Attempted to invoke a context {type_name} without context."
 536                )
 537
 538            args.insert(0, context)
 539        elif pass_arg is _PassArg.eval_context:
 540            if eval_ctx is None:
 541                if context is not None:
 542                    eval_ctx = context.eval_ctx
 543                else:
 544                    eval_ctx = EvalContext(self)
 545
 546            args.insert(0, eval_ctx)
 547        elif pass_arg is _PassArg.environment:
 548            args.insert(0, self)
 549
 550        return func(*args, **kwargs)
 551
 552    def call_filter(
 553        self,
 554        name: str,
 555        value: t.Any,
 556        args: t.Optional[t.Sequence[t.Any]] = None,
 557        kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
 558        context: t.Optional[Context] = None,
 559        eval_ctx: t.Optional[EvalContext] = None,
 560    ) -> t.Any:
 561        """Invoke a filter on a value the same way the compiler does.
 562
 563        This might return a coroutine if the filter is running from an
 564        environment in async mode and the filter supports async
 565        execution. It's your responsibility to await this if needed.
 566
 567        .. versionadded:: 2.7
 568        """
 569        return self._filter_test_common(
 570            name, value, args, kwargs, context, eval_ctx, True
 571        )
 572
 573    def call_test(
 574        self,
 575        name: str,
 576        value: t.Any,
 577        args: t.Optional[t.Sequence[t.Any]] = None,
 578        kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
 579        context: t.Optional[Context] = None,
 580        eval_ctx: t.Optional[EvalContext] = None,
 581    ) -> t.Any:
 582        """Invoke a test on a value the same way the compiler does.
 583
 584        This might return a coroutine if the test is running from an
 585        environment in async mode and the test supports async execution.
 586        It's your responsibility to await this if needed.
 587
 588        .. versionchanged:: 3.0
 589            Tests support ``@pass_context``, etc. decorators. Added
 590            the ``context`` and ``eval_ctx`` parameters.
 591
 592        .. versionadded:: 2.7
 593        """
 594        return self._filter_test_common(
 595            name, value, args, kwargs, context, eval_ctx, False
 596        )
 597
 598    @internalcode
 599    def parse(
 600        self,
 601        source: str,
 602        name: t.Optional[str] = None,
 603        filename: t.Optional[str] = None,
 604    ) -> nodes.Template:
 605        """Parse the sourcecode and return the abstract syntax tree.  This
 606        tree of nodes is used by the compiler to convert the template into
 607        executable source- or bytecode.  This is useful for debugging or to
 608        extract information from templates.
 609
 610        If you are :ref:`developing Jinja extensions <writing-extensions>`
 611        this gives you a good overview of the node tree generated.
 612        """
 613        try:
 614            return self._parse(source, name, filename)
 615        except TemplateSyntaxError:
 616            self.handle_exception(source=source)
 617
 618    def _parse(
 619        self, source: str, name: t.Optional[str], filename: t.Optional[str]
 620    ) -> nodes.Template:
 621        """Internal parsing function used by `parse` and `compile`."""
 622        return Parser(self, source, name, filename).parse()
 623
 624    def lex(
 625        self,
 626        source: str,
 627        name: t.Optional[str] = None,
 628        filename: t.Optional[str] = None,
 629    ) -> t.Iterator[t.Tuple[int, str, str]]:
 630        """Lex the given sourcecode and return a generator that yields
 631        tokens as tuples in the form ``(lineno, token_type, value)``.
 632        This can be useful for :ref:`extension development <writing-extensions>`
 633        and debugging templates.
 634
 635        This does not perform preprocessing.  If you want the preprocessing
 636        of the extensions to be applied you have to filter source through
 637        the :meth:`preprocess` method.
 638        """
 639        source = str(source)
 640        try:
 641            return self.lexer.tokeniter(source, name, filename)
 642        except TemplateSyntaxError:
 643            self.handle_exception(source=source)
 644
 645    def preprocess(
 646        self,
 647        source: str,
 648        name: t.Optional[str] = None,
 649        filename: t.Optional[str] = None,
 650    ) -> str:
 651        """Preprocesses the source with all extensions.  This is automatically
 652        called for all parsing and compiling methods but *not* for :meth:`lex`
 653        because there you usually only want the actual source tokenized.
 654        """
 655        return reduce(
 656            lambda s, e: e.preprocess(s, name, filename),
 657            self.iter_extensions(),
 658            str(source),
 659        )
 660
 661    def _tokenize(
 662        self,
 663        source: str,
 664        name: t.Optional[str],
 665        filename: t.Optional[str] = None,
 666        state: t.Optional[str] = None,
 667    ) -> TokenStream:
 668        """Called by the parser to do the preprocessing and filtering
 669        for all the extensions.  Returns a :class:`~jinja2.lexer.TokenStream`.
 670        """
 671        source = self.preprocess(source, name, filename)
 672        stream = self.lexer.tokenize(source, name, filename, state)
 673
 674        for ext in self.iter_extensions():
 675            stream = ext.filter_stream(stream)  # type: ignore
 676
 677            if not isinstance(stream, TokenStream):
 678                stream = TokenStream(stream, name, filename)
 679
 680        return stream
 681
 682    def _generate(
 683        self,
 684        source: nodes.Template,
 685        name: t.Optional[str],
 686        filename: t.Optional[str],
 687        defer_init: bool = False,
 688    ) -> str:
 689        """Internal hook that can be overridden to hook a different generate
 690        method in.
 691
 692        .. versionadded:: 2.5
 693        """
 694        return generate(  # type: ignore
 695            source,
 696            self,
 697            name,
 698            filename,
 699            defer_init=defer_init,
 700            optimized=self.optimized,
 701        )
 702
 703    def _compile(self, source: str, filename: str) -> CodeType:
 704        """Internal hook that can be overridden to hook a different compile
 705        method in.
 706
 707        .. versionadded:: 2.5
 708        """
 709        return compile(source, filename, "exec")
 710
 711    @typing.overload
 712    def compile(
 713        self,
 714        source: t.Union[str, nodes.Template],
 715        name: t.Optional[str] = None,
 716        filename: t.Optional[str] = None,
 717        raw: "te.Literal[False]" = False,
 718        defer_init: bool = False,
 719    ) -> CodeType: ...
 720
 721    @typing.overload
 722    def compile(
 723        self,
 724        source: t.Union[str, nodes.Template],
 725        name: t.Optional[str] = None,
 726        filename: t.Optional[str] = None,
 727        raw: "te.Literal[True]" = ...,
 728        defer_init: bool = False,
 729    ) -> str: ...
 730
 731    @internalcode
 732    def compile(
 733        self,
 734        source: t.Union[str, nodes.Template],
 735        name: t.Optional[str] = None,
 736        filename: t.Optional[str] = None,
 737        raw: bool = False,
 738        defer_init: bool = False,
 739    ) -> t.Union[str, CodeType]:
 740        """Compile a node or template source code.  The `name` parameter is
 741        the load name of the template after it was joined using
 742        :meth:`join_path` if necessary, not the filename on the file system.
 743        the `filename` parameter is the estimated filename of the template on
 744        the file system.  If the template came from a database or memory this
 745        can be omitted.
 746
 747        The return value of this method is a python code object.  If the `raw`
 748        parameter is `True` the return value will be a string with python
 749        code equivalent to the bytecode returned otherwise.  This method is
 750        mainly used internally.
 751
 752        `defer_init` is use internally to aid the module code generator.  This
 753        causes the generated code to be able to import without the global
 754        environment variable to be set.
 755
 756        .. versionadded:: 2.4
 757           `defer_init` parameter added.
 758        """
 759        source_hint = None
 760        try:
 761            if isinstance(source, str):
 762                source_hint = source
 763                source = self._parse(source, name, filename)
 764            source = self._generate(source, name, filename, defer_init=defer_init)
 765            if raw:
 766                return source
 767            if filename is None:
 768                filename = "<template>"
 769            return self._compile(source, filename)
 770        except TemplateSyntaxError:
 771            self.handle_exception(source=source_hint)
 772
 773    def compile_expression(
 774        self, source: str, undefined_to_none: bool = True
 775    ) -> "TemplateExpression":
 776        """A handy helper method that returns a callable that accepts keyword
 777        arguments that appear as variables in the expression.  If called it
 778        returns the result of the expression.
 779
 780        This is useful if applications want to use the same rules as Jinja
 781        in template "configuration files" or similar situations.
 782
 783        Example usage:
 784
 785        >>> env = Environment()
 786        >>> expr = env.compile_expression('foo == 42')
 787        >>> expr(foo=23)
 788        False
 789        >>> expr(foo=42)
 790        True
 791
 792        Per default the return value is converted to `None` if the
 793        expression returns an undefined value.  This can be changed
 794        by setting `undefined_to_none` to `False`.
 795
 796        >>> env.compile_expression('var')() is None
 797        True
 798        >>> env.compile_expression('var', undefined_to_none=False)()
 799        Undefined
 800
 801        .. versionadded:: 2.1
 802        """
 803        parser = Parser(self, source, state="variable")
 804        try:
 805            expr = parser.parse_expression()
 806            if not parser.stream.eos:
 807                raise TemplateSyntaxError(
 808                    "chunk after expression", parser.stream.current.lineno, None, None
 809                )
 810            expr.set_environment(self)
 811        except TemplateSyntaxError:
 812            self.handle_exception(source=source)
 813
 814        body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)]
 815        template = self.from_string(nodes.Template(body, lineno=1))
 816        return TemplateExpression(template, undefined_to_none)
 817
 818    def compile_templates(
 819        self,
 820        target: t.Union[str, "os.PathLike[str]"],
 821        extensions: t.Optional[t.Collection[str]] = None,
 822        filter_func: t.Optional[t.Callable[[str], bool]] = None,
 823        zip: t.Optional[str] = "deflated",
 824        log_function: t.Optional[t.Callable[[str], None]] = None,
 825        ignore_errors: bool = True,
 826    ) -> None:
 827        """Finds all the templates the loader can find, compiles them
 828        and stores them in `target`.  If `zip` is `None`, instead of in a
 829        zipfile, the templates will be stored in a directory.
 830        By default a deflate zip algorithm is used. To switch to
 831        the stored algorithm, `zip` can be set to ``'stored'``.
 832
 833        `extensions` and `filter_func` are passed to :meth:`list_templates`.
 834        Each template returned will be compiled to the target folder or
 835        zipfile.
 836
 837        By default template compilation errors are ignored.  In case a
 838        log function is provided, errors are logged.  If you want template
 839        syntax errors to abort the compilation you can set `ignore_errors`
 840        to `False` and you will get an exception on syntax errors.
 841
 842        .. versionadded:: 2.4
 843        """
 844        from .loaders import ModuleLoader
 845
 846        if log_function is None:
 847
 848            def log_function(x: str) -> None:
 849                pass
 850
 851        assert log_function is not None
 852        assert self.loader is not None, "No loader configured."
 853
 854        def write_file(filename: str, data: str) -> None:
 855            if zip:
 856                info = ZipInfo(filename)
 857                info.external_attr = 0o755 << 16
 858                zip_file.writestr(info, data)
 859            else:
 860                with open(os.path.join(target, filename), "wb") as f:
 861                    f.write(data.encode("utf8"))
 862
 863        if zip is not None:
 864            from zipfile import ZIP_DEFLATED
 865            from zipfile import ZIP_STORED
 866            from zipfile import ZipFile
 867            from zipfile import ZipInfo
 868
 869            zip_file = ZipFile(
 870                target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip]
 871            )
 872            log_function(f"Compiling into Zip archive {target!r}")
 873        else:
 874            if not os.path.isdir(target):
 875                os.makedirs(target)
 876            log_function(f"Compiling into folder {target!r}")
 877
 878        try:
 879            for name in self.list_templates(extensions, filter_func):
 880                source, filename, _ = self.loader.get_source(self, name)
 881                try:
 882                    code = self.compile(source, name, filename, True, True)
 883                except TemplateSyntaxError as e:
 884                    if not ignore_errors:
 885                        raise
 886                    log_function(f'Could not compile "{name}": {e}')
 887                    continue
 888
 889                filename = ModuleLoader.get_module_filename(name)
 890
 891                write_file(filename, code)
 892                log_function(f'Compiled "{name}" as {filename}')
 893        finally:
 894            if zip:
 895                zip_file.close()
 896
 897        log_function("Finished compiling templates")
 898
 899    def list_templates(
 900        self,
 901        extensions: t.Optional[t.Collection[str]] = None,
 902        filter_func: t.Optional[t.Callable[[str], bool]] = None,
 903    ) -> t.List[str]:
 904        """Returns a list of templates for this environment.  This requires
 905        that the loader supports the loader's
 906        :meth:`~BaseLoader.list_templates` method.
 907
 908        If there are other files in the template folder besides the
 909        actual templates, the returned list can be filtered.  There are two
 910        ways: either `extensions` is set to a list of file extensions for
 911        templates, or a `filter_func` can be provided which is a callable that
 912        is passed a template name and should return `True` if it should end up
 913        in the result list.
 914
 915        If the loader does not support that, a :exc:`TypeError` is raised.
 916
 917        .. versionadded:: 2.4
 918        """
 919        assert self.loader is not None, "No loader configured."
 920        names = self.loader.list_templates()
 921
 922        if extensions is not None:
 923            if filter_func is not None:
 924                raise TypeError(
 925                    "either extensions or filter_func can be passed, but not both"
 926                )
 927
 928            def filter_func(x: str) -> bool:
 929                return "." in x and x.rsplit(".", 1)[1] in extensions
 930
 931        if filter_func is not None:
 932            names = [name for name in names if filter_func(name)]
 933
 934        return names
 935
 936    def handle_exception(self, source: t.Optional[str] = None) -> "te.NoReturn":
 937        """Exception handling helper.  This is used internally to either raise
 938        rewritten exceptions or return a rendered traceback for the template.
 939        """
 940        from .debug import rewrite_traceback_stack
 941
 942        raise rewrite_traceback_stack(source=source)
 943
 944    def join_path(self, template: str, parent: str) -> str:
 945        """Join a template with the parent.  By default all the lookups are
 946        relative to the loader root so this method returns the `template`
 947        parameter unchanged, but if the paths should be relative to the
 948        parent template, this function can be used to calculate the real
 949        template name.
 950
 951        Subclasses may override this method and implement template path
 952        joining here.
 953        """
 954        return template
 955
 956    @internalcode
 957    def _load_template(
 958        self, name: str, globals: t.Optional[t.MutableMapping[str, t.Any]]
 959    ) -> "Template":
 960        if self.loader is None:
 961            raise TypeError("no loader for this environment specified")
 962        cache_key = (weakref.ref(self.loader), name)
 963        if self.cache is not None:
 964            template = self.cache.get(cache_key)
 965            if template is not None and (
 966                not self.auto_reload or template.is_up_to_date
 967            ):
 968                # template.globals is a ChainMap, modifying it will only
 969                # affect the template, not the environment globals.
 970                if globals:
 971                    template.globals.update(globals)
 972
 973                return template
 974
 975        template = self.loader.load(self, name, self.make_globals(globals))
 976
 977        if self.cache is not None:
 978            self.cache[cache_key] = template
 979        return template
 980
 981    @internalcode
 982    def get_template(
 983        self,
 984        name: t.Union[str, "Template"],
 985        parent: t.Optional[str] = None,
 986        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
 987    ) -> "Template":
 988        """Load a template by name with :attr:`loader` and return a
 989        :class:`Template`. If the template does not exist a
 990        :exc:`TemplateNotFound` exception is raised.
 991
 992        :param name: Name of the template to load. When loading
 993            templates from the filesystem, "/" is used as the path
 994            separator, even on Windows.
 995        :param parent: The name of the parent template importing this
 996            template. :meth:`join_path` can be used to implement name
 997            transformations with this.
 998        :param globals: Extend the environment :attr:`globals` with
 999            these extra variables available for all renders of this
1000            template. If the template has already been loaded and
1001            cached, its globals are updated with any new items.
1002
1003        .. versionchanged:: 3.0
1004            If a template is loaded from cache, ``globals`` will update
1005            the template's globals instead of ignoring the new values.
1006
1007        .. versionchanged:: 2.4
1008            If ``name`` is a :class:`Template` object it is returned
1009            unchanged.
1010        """
1011        if isinstance(name, Template):
1012            return name
1013        if parent is not None:
1014            name = self.join_path(name, parent)
1015
1016        return self._load_template(name, globals)
1017
1018    @internalcode
1019    def select_template(
1020        self,
1021        names: t.Iterable[t.Union[str, "Template"]],
1022        parent: t.Optional[str] = None,
1023        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1024    ) -> "Template":
1025        """Like :meth:`get_template`, but tries loading multiple names.
1026        If none of the names can be loaded a :exc:`TemplatesNotFound`
1027        exception is raised.
1028
1029        :param names: List of template names to try loading in order.
1030        :param parent: The name of the parent template importing this
1031            template. :meth:`join_path` can be used to implement name
1032            transformations with this.
1033        :param globals: Extend the environment :attr:`globals` with
1034            these extra variables available for all renders of this
1035            template. If the template has already been loaded and
1036            cached, its globals are updated with any new items.
1037
1038        .. versionchanged:: 3.0
1039            If a template is loaded from cache, ``globals`` will update
1040            the template's globals instead of ignoring the new values.
1041
1042        .. versionchanged:: 2.11
1043            If ``names`` is :class:`Undefined`, an :exc:`UndefinedError`
1044            is raised instead. If no templates were found and ``names``
1045            contains :class:`Undefined`, the message is more helpful.
1046
1047        .. versionchanged:: 2.4
1048            If ``names`` contains a :class:`Template` object it is
1049            returned unchanged.
1050
1051        .. versionadded:: 2.3
1052        """
1053        if isinstance(names, Undefined):
1054            names._fail_with_undefined_error()
1055
1056        if not names:
1057            raise TemplatesNotFound(
1058                message="Tried to select from an empty list of templates."
1059            )
1060
1061        for name in names:
1062            if isinstance(name, Template):
1063                return name
1064            if parent is not None:
1065                name = self.join_path(name, parent)
1066            try:
1067                return self._load_template(name, globals)
1068            except (TemplateNotFound, UndefinedError):
1069                pass
1070        raise TemplatesNotFound(names)  # type: ignore
1071
1072    @internalcode
1073    def get_or_select_template(
1074        self,
1075        template_name_or_list: t.Union[
1076            str, "Template", t.List[t.Union[str, "Template"]]
1077        ],
1078        parent: t.Optional[str] = None,
1079        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1080    ) -> "Template":
1081        """Use :meth:`select_template` if an iterable of template names
1082        is given, or :meth:`get_template` if one name is given.
1083
1084        .. versionadded:: 2.3
1085        """
1086        if isinstance(template_name_or_list, (str, Undefined)):
1087            return self.get_template(template_name_or_list, parent, globals)
1088        elif isinstance(template_name_or_list, Template):
1089            return template_name_or_list
1090        return self.select_template(template_name_or_list, parent, globals)
1091
1092    def from_string(
1093        self,
1094        source: t.Union[str, nodes.Template],
1095        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1096        template_class: t.Optional[t.Type["Template"]] = None,
1097    ) -> "Template":
1098        """Load a template from a source string without using
1099        :attr:`loader`.
1100
1101        :param source: Jinja source to compile into a template.
1102        :param globals: Extend the environment :attr:`globals` with
1103            these extra variables available for all renders of this
1104            template. If the template has already been loaded and
1105            cached, its globals are updated with any new items.
1106        :param template_class: Return an instance of this
1107            :class:`Template` class.
1108        """
1109        gs = self.make_globals(globals)
1110        cls = template_class or self.template_class
1111        return cls.from_code(self, self.compile(source), gs, None)
1112
1113    def make_globals(
1114        self, d: t.Optional[t.MutableMapping[str, t.Any]]
1115    ) -> t.MutableMapping[str, t.Any]:
1116        """Make the globals map for a template. Any given template
1117        globals overlay the environment :attr:`globals`.
1118
1119        Returns a :class:`collections.ChainMap`. This allows any changes
1120        to a template's globals to only affect that template, while
1121        changes to the environment's globals are still reflected.
1122        However, avoid modifying any globals after a template is loaded.
1123
1124        :param d: Dict of template-specific globals.
1125
1126        .. versionchanged:: 3.0
1127            Use :class:`collections.ChainMap` to always prevent mutating
1128            environment globals.
1129        """
1130        if d is None:
1131            d = {}
1132
1133        return ChainMap(d, self.globals)
1134
1135
1136class Template:
1137    """A compiled template that can be rendered.
1138
1139    Use the methods on :class:`Environment` to create or load templates.
1140    The environment is used to configure how templates are compiled and
1141    behave.
1142
1143    It is also possible to create a template object directly. This is
1144    not usually recommended. The constructor takes most of the same
1145    arguments as :class:`Environment`. All templates created with the
1146    same environment arguments share the same ephemeral ``Environment``
1147    instance behind the scenes.
1148
1149    A template object should be considered immutable. Modifications on
1150    the object are not supported.
1151    """
1152
1153    #: Type of environment to create when creating a template directly
1154    #: rather than through an existing environment.
1155    environment_class: t.Type[Environment] = Environment
1156
1157    environment: Environment
1158    globals: t.MutableMapping[str, t.Any]
1159    name: t.Optional[str]
1160    filename: t.Optional[str]
1161    blocks: t.Dict[str, t.Callable[[Context], t.Iterator[str]]]
1162    root_render_func: t.Callable[[Context], t.Iterator[str]]
1163    _module: t.Optional["TemplateModule"]
1164    _debug_info: str
1165    _uptodate: t.Optional[t.Callable[[], bool]]
1166
1167    def __new__(
1168        cls,
1169        source: t.Union[str, nodes.Template],
1170        block_start_string: str = BLOCK_START_STRING,
1171        block_end_string: str = BLOCK_END_STRING,
1172        variable_start_string: str = VARIABLE_START_STRING,
1173        variable_end_string: str = VARIABLE_END_STRING,
1174        comment_start_string: str = COMMENT_START_STRING,
1175        comment_end_string: str = COMMENT_END_STRING,
1176        line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
1177        line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
1178        trim_blocks: bool = TRIM_BLOCKS,
1179        lstrip_blocks: bool = LSTRIP_BLOCKS,
1180        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
1181        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
1182        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
1183        optimized: bool = True,
1184        undefined: t.Type[Undefined] = Undefined,
1185        finalize: t.Optional[t.Callable[..., t.Any]] = None,
1186        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
1187        enable_async: bool = False,
1188    ) -> t.Any:  # it returns a `Template`, but this breaks the sphinx build...
1189        env = get_spontaneous_environment(
1190            cls.environment_class,  # type: ignore
1191            block_start_string,
1192            block_end_string,
1193            variable_start_string,
1194            variable_end_string,
1195            comment_start_string,
1196            comment_end_string,
1197            line_statement_prefix,
1198            line_comment_prefix,
1199            trim_blocks,
1200            lstrip_blocks,
1201            newline_sequence,
1202            keep_trailing_newline,
1203            frozenset(extensions),
1204            optimized,
1205            undefined,  # type: ignore
1206            finalize,
1207            autoescape,
1208            None,
1209            0,
1210            False,
1211            None,
1212            enable_async,
1213        )
1214        return env.from_string(source, template_class=cls)
1215
1216    @classmethod
1217    def from_code(
1218        cls,
1219        environment: Environment,
1220        code: CodeType,
1221        globals: t.MutableMapping[str, t.Any],
1222        uptodate: t.Optional[t.Callable[[], bool]] = None,
1223    ) -> "Template":
1224        """Creates a template object from compiled code and the globals.  This
1225        is used by the loaders and environment to create a template object.
1226        """
1227        namespace = {"environment": environment, "__file__": code.co_filename}
1228        exec(code, namespace)
1229        rv = cls._from_namespace(environment, namespace, globals)
1230        rv._uptodate = uptodate
1231        return rv
1232
1233    @classmethod
1234    def from_module_dict(
1235        cls,
1236        environment: Environment,
1237        module_dict: t.MutableMapping[str, t.Any],
1238        globals: t.MutableMapping[str, t.Any],
1239    ) -> "Template":
1240        """Creates a template object from a module.  This is used by the
1241        module loader to create a template object.
1242
1243        .. versionadded:: 2.4
1244        """
1245        return cls._from_namespace(environment, module_dict, globals)
1246
1247    @classmethod
1248    def _from_namespace(
1249        cls,
1250        environment: Environment,
1251        namespace: t.MutableMapping[str, t.Any],
1252        globals: t.MutableMapping[str, t.Any],
1253    ) -> "Template":
1254        t: Template = object.__new__(cls)
1255        t.environment = environment
1256        t.globals = globals
1257        t.name = namespace["name"]
1258        t.filename = namespace["__file__"]
1259        t.blocks = namespace["blocks"]
1260
1261        # render function and module
1262        t.root_render_func = namespace["root"]
1263        t._module = None
1264
1265        # debug and loader helpers
1266        t._debug_info = namespace["debug_info"]
1267        t._uptodate = None
1268
1269        # store the reference
1270        namespace["environment"] = environment
1271        namespace["__jinja_template__"] = t
1272
1273        return t
1274
1275    def render(self, *args: t.Any, **kwargs: t.Any) -> str:
1276        """This method accepts the same arguments as the `dict` constructor:
1277        A dict, a dict subclass or some keyword arguments.  If no arguments
1278        are given the context will be empty.  These two calls do the same::
1279
1280            template.render(knights='that say nih')
1281            template.render({'knights': 'that say nih'})
1282
1283        This will return the rendered template as a string.
1284        """
1285        if self.environment.is_async:
1286            import asyncio
1287
1288            return asyncio.run(self.render_async(*args, **kwargs))
1289
1290        ctx = self.new_context(dict(*args, **kwargs))
1291
1292        try:
1293            return self.environment.concat(self.root_render_func(ctx))  # type: ignore
1294        except Exception:
1295            self.environment.handle_exception()
1296
1297    async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:
1298        """This works similar to :meth:`render` but returns a coroutine
1299        that when awaited returns the entire rendered template string.  This
1300        requires the async feature to be enabled.
1301
1302        Example usage::
1303
1304            await template.render_async(knights='that say nih; asynchronously')
1305        """
1306        if not self.environment.is_async:
1307            raise RuntimeError(
1308                "The environment was not created with async mode enabled."
1309            )
1310
1311        ctx = self.new_context(dict(*args, **kwargs))
1312
1313        try:
1314            return self.environment.concat(  # type: ignore
1315                [n async for n in self.root_render_func(ctx)]  # type: ignore
1316            )
1317        except Exception:
1318            return self.environment.handle_exception()
1319
1320    def stream(self, *args: t.Any, **kwargs: t.Any) -> "TemplateStream":
1321        """Works exactly like :meth:`generate` but returns a
1322        :class:`TemplateStream`.
1323        """
1324        return TemplateStream(self.generate(*args, **kwargs))
1325
1326    def generate(self, *args: t.Any, **kwargs: t.Any) -> t.Iterator[str]:
1327        """For very large templates it can be useful to not render the whole
1328        template at once but evaluate each statement after another and yield
1329        piece for piece.  This method basically does exactly that and returns
1330        a generator that yields one item after another as strings.
1331
1332        It accepts the same arguments as :meth:`render`.
1333        """
1334        if self.environment.is_async:
1335            import asyncio
1336
1337            async def to_list() -> t.List[str]:
1338                return [x async for x in self.generate_async(*args, **kwargs)]
1339
1340            yield from asyncio.run(to_list())
1341            return
1342
1343        ctx = self.new_context(dict(*args, **kwargs))
1344
1345        try:
1346            yield from self.root_render_func(ctx)
1347        except Exception:
1348            yield self.environment.handle_exception()
1349
1350    async def generate_async(
1351        self, *args: t.Any, **kwargs: t.Any
1352    ) -> t.AsyncGenerator[str, object]:
1353        """An async version of :meth:`generate`.  Works very similarly but
1354        returns an async iterator instead.
1355        """
1356        if not self.environment.is_async:
1357            raise RuntimeError(
1358                "The environment was not created with async mode enabled."
1359            )
1360
1361        ctx = self.new_context(dict(*args, **kwargs))
1362
1363        try:
1364            agen = self.root_render_func(ctx)
1365            try:
1366                async for event in agen:  # type: ignore
1367                    yield event
1368            finally:
1369                # we can't use async with aclosing(...) because that's only
1370                # in 3.10+
1371                await agen.aclose()  # type: ignore
1372        except Exception:
1373            yield self.environment.handle_exception()
1374
1375    def new_context(
1376        self,
1377        vars: t.Optional[t.Dict[str, t.Any]] = None,
1378        shared: bool = False,
1379        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1380    ) -> Context:
1381        """Create a new :class:`Context` for this template.  The vars
1382        provided will be passed to the template.  Per default the globals
1383        are added to the context.  If shared is set to `True` the data
1384        is passed as is to the context without adding the globals.
1385
1386        `locals` can be a dict of local variables for internal usage.
1387        """
1388        return new_context(
1389            self.environment, self.name, self.blocks, vars, shared, self.globals, locals
1390        )
1391
1392    def make_module(
1393        self,
1394        vars: t.Optional[t.Dict[str, t.Any]] = None,
1395        shared: bool = False,
1396        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1397    ) -> "TemplateModule":
1398        """This method works like the :attr:`module` attribute when called
1399        without arguments but it will evaluate the template on every call
1400        rather than caching it.  It's also possible to provide
1401        a dict which is then used as context.  The arguments are the same
1402        as for the :meth:`new_context` method.
1403        """
1404        ctx = self.new_context(vars, shared, locals)
1405        return TemplateModule(self, ctx)
1406
1407    async def make_module_async(
1408        self,
1409        vars: t.Optional[t.Dict[str, t.Any]] = None,
1410        shared: bool = False,
1411        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1412    ) -> "TemplateModule":
1413        """As template module creation can invoke template code for
1414        asynchronous executions this method must be used instead of the
1415        normal :meth:`make_module` one.  Likewise the module attribute
1416        becomes unavailable in async mode.
1417        """
1418        ctx = self.new_context(vars, shared, locals)
1419        return TemplateModule(
1420            self,
1421            ctx,
1422            [x async for x in self.root_render_func(ctx)],  # type: ignore
1423        )
1424
1425    @internalcode
1426    def _get_default_module(self, ctx: t.Optional[Context] = None) -> "TemplateModule":
1427        """If a context is passed in, this means that the template was
1428        imported. Imported templates have access to the current
1429        template's globals by default, but they can only be accessed via
1430        the context during runtime.
1431
1432        If there are new globals, we need to create a new module because
1433        the cached module is already rendered and will not have access
1434        to globals from the current context. This new module is not
1435        cached because the template can be imported elsewhere, and it
1436        should have access to only the current template's globals.
1437        """
1438        if self.environment.is_async:
1439            raise RuntimeError("Module is not available in async mode.")
1440
1441        if ctx is not None:
1442            keys = ctx.globals_keys - self.globals.keys()
1443
1444            if keys:
1445                return self.make_module({k: ctx.parent[k] for k in keys})
1446
1447        if self._module is None:
1448            self._module = self.make_module()
1449
1450        return self._module
1451
1452    async def _get_default_module_async(
1453        self, ctx: t.Optional[Context] = None
1454    ) -> "TemplateModule":
1455        if ctx is not None:
1456            keys = ctx.globals_keys - self.globals.keys()
1457
1458            if keys:
1459                return await self.make_module_async({k: ctx.parent[k] for k in keys})
1460
1461        if self._module is None:
1462            self._module = await self.make_module_async()
1463
1464        return self._module
1465
1466    @property
1467    def module(self) -> "TemplateModule":
1468        """The template as module.  This is used for imports in the
1469        template runtime but is also useful if one wants to access
1470        exported template variables from the Python layer:
1471
1472        >>> t = Template('{% macro foo() %}42{% endmacro %}23')
1473        >>> str(t.module)
1474        '23'
1475        >>> t.module.foo() == u'42'
1476        True
1477
1478        This attribute is not available if async mode is enabled.
1479        """
1480        return self._get_default_module()
1481
1482    def get_corresponding_lineno(self, lineno: int) -> int:
1483        """Return the source line number of a line number in the
1484        generated bytecode as they are not in sync.
1485        """
1486        for template_line, code_line in reversed(self.debug_info):
1487            if code_line <= lineno:
1488                return template_line
1489        return 1
1490
1491    @property
1492    def is_up_to_date(self) -> bool:
1493        """If this variable is `False` there is a newer version available."""
1494        if self._uptodate is None:
1495            return True
1496        return self._uptodate()
1497
1498    @property
1499    def debug_info(self) -> t.List[t.Tuple[int, int]]:
1500        """The debug info mapping."""
1501        if self._debug_info:
1502            return [
1503                tuple(map(int, x.split("=")))  # type: ignore
1504                for x in self._debug_info.split("&")
1505            ]
1506
1507        return []
1508
1509    def __repr__(self) -> str:
1510        if self.name is None:
1511            name = f"memory:{id(self):x}"
1512        else:
1513            name = repr(self.name)
1514        return f"<{type(self).__name__} {name}>"
1515
1516
1517class TemplateModule:
1518    """Represents an imported template.  All the exported names of the
1519    template are available as attributes on this object.  Additionally
1520    converting it into a string renders the contents.
1521    """
1522
1523    def __init__(
1524        self,
1525        template: Template,
1526        context: Context,
1527        body_stream: t.Optional[t.Iterable[str]] = None,
1528    ) -> None:
1529        if body_stream is None:
1530            if context.environment.is_async:
1531                raise RuntimeError(
1532                    "Async mode requires a body stream to be passed to"
1533                    " a template module. Use the async methods of the"
1534                    " API you are using."
1535                )
1536
1537            body_stream = list(template.root_render_func(context))
1538
1539        self._body_stream = body_stream
1540        self.__dict__.update(context.get_exported())
1541        self.__name__ = template.name
1542
1543    def __html__(self) -> Markup:
1544        return Markup(concat(self._body_stream))
1545
1546    def __str__(self) -> str:
1547        return concat(self._body_stream)
1548
1549    def __repr__(self) -> str:
1550        if self.__name__ is None:
1551            name = f"memory:{id(self):x}"
1552        else:
1553            name = repr(self.__name__)
1554        return f"<{type(self).__name__} {name}>"
1555
1556
1557class TemplateExpression:
1558    """The :meth:`jinja2.Environment.compile_expression` method returns an
1559    instance of this object.  It encapsulates the expression-like access
1560    to the template with an expression it wraps.
1561    """
1562
1563    def __init__(self, template: Template, undefined_to_none: bool) -> None:
1564        self._template = template
1565        self._undefined_to_none = undefined_to_none
1566
1567    def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Optional[t.Any]:
1568        context = self._template.new_context(dict(*args, **kwargs))
1569        consume(self._template.root_render_func(context))
1570        rv = context.vars["result"]
1571        if self._undefined_to_none and isinstance(rv, Undefined):
1572            rv = None
1573        return rv
1574
1575
1576class TemplateStream:
1577    """A template stream works pretty much like an ordinary python generator
1578    but it can buffer multiple items to reduce the number of total iterations.
1579    Per default the output is unbuffered which means that for every unbuffered
1580    instruction in the template one string is yielded.
1581
1582    If buffering is enabled with a buffer size of 5, five items are combined
1583    into a new string.  This is mainly useful if you are streaming
1584    big templates to a client via WSGI which flushes after each iteration.
1585    """
1586
1587    def __init__(self, gen: t.Iterator[str]) -> None:
1588        self._gen = gen
1589        self.disable_buffering()
1590
1591    def dump(
1592        self,
1593        fp: t.Union[str, t.IO[bytes]],
1594        encoding: t.Optional[str] = None,
1595        errors: t.Optional[str] = "strict",
1596    ) -> None:
1597        """Dump the complete stream into a file or file-like object.
1598        Per default strings are written, if you want to encode
1599        before writing specify an `encoding`.
1600
1601        Example usage::
1602
1603            Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
1604        """
1605        close = False
1606
1607        if isinstance(fp, str):
1608            if encoding is None:
1609                encoding = "utf-8"
1610
1611            real_fp: t.IO[bytes] = open(fp, "wb")
1612            close = True
1613        else:
1614            real_fp = fp
1615
1616        try:
1617            if encoding is not None:
1618                iterable = (x.encode(encoding, errors) for x in self)  # type: ignore
1619            else:
1620                iterable = self  # type: ignore
1621
1622            if hasattr(real_fp, "writelines"):
1623                real_fp.writelines(iterable)
1624            else:
1625                for item in iterable:
1626                    real_fp.write(item)
1627        finally:
1628            if close:
1629                real_fp.close()
1630
1631    def disable_buffering(self) -> None:
1632        """Disable the output buffering."""
1633        self._next = partial(next, self._gen)
1634        self.buffered = False
1635
1636    def _buffered_generator(self, size: int) -> t.Iterator[str]:
1637        buf: t.List[str] = []
1638        c_size = 0
1639        push = buf.append
1640
1641        while True:
1642            try:
1643                while c_size < size:
1644                    c = next(self._gen)
1645                    push(c)
1646                    if c:
1647                        c_size += 1
1648            except StopIteration:
1649                if not c_size:
1650                    return
1651            yield concat(buf)
1652            del buf[:]
1653            c_size = 0
1654
1655    def enable_buffering(self, size: int = 5) -> None:
1656        """Enable buffering.  Buffer `size` items before yielding them."""
1657        if size <= 1:
1658            raise ValueError("buffer size too small")
1659
1660        self.buffered = True
1661        self._next = partial(next, self._buffered_generator(size))
1662
1663    def __iter__(self) -> "TemplateStream":
1664        return self
1665
1666    def __next__(self) -> str:
1667        return self._next()  # type: ignore
1668
1669
1670# hook in default template class.  if anyone reads this comment: ignore that
1671# it's possible to use custom templates ;-)
1672Environment.template_class = Template
@lru_cache(maxsize=10)
def get_spontaneous_environment(cls: Type[~_env_bound], *args: Any) -> ~_env_bound:
69@lru_cache(maxsize=10)
70def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_bound:
71    """Return a new spontaneous environment. A spontaneous environment
72    is used for templates created directly rather than through an
73    existing environment.
74
75    :param cls: Environment class to create.
76    :param args: Positional arguments passed to environment.
77    """
78    env = cls(*args)
79    env.shared = True
80    return env

Return a new spontaneous environment. A spontaneous environment is used for templates created directly rather than through an existing environment.

Parameters
  • cls: Environment class to create.
  • args: Positional arguments passed to environment.
def create_cache( size: int) -> Optional[MutableMapping[Tuple[weakref.ReferenceType[Any], str], Template]]:
83def create_cache(
84    size: int,
85) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
86    """Return the cache class for the given size."""
87    if size == 0:
88        return None
89
90    if size < 0:
91        return {}
92
93    return LRUCache(size)  # type: ignore

Return the cache class for the given size.

def copy_cache( cache: Optional[MutableMapping[Any, Any]]) -> Optional[MutableMapping[Tuple[weakref.ReferenceType[Any], str], Template]]:
 96def copy_cache(
 97    cache: t.Optional[t.MutableMapping[t.Any, t.Any]],
 98) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
 99    """Create an empty copy of the given cache."""
100    if cache is None:
101        return None
102
103    if type(cache) is dict:  # noqa E721
104        return {}
105
106    return LRUCache(cache.capacity)  # type: ignore

Create an empty copy of the given cache.

def load_extensions( environment: Environment, extensions: Sequence[Union[str, Type[jinja2.ext.Extension]]]) -> Dict[str, jinja2.ext.Extension]:
109def load_extensions(
110    environment: "Environment",
111    extensions: t.Sequence[t.Union[str, t.Type["Extension"]]],
112) -> t.Dict[str, "Extension"]:
113    """Load the extensions from the list and bind it to the environment.
114    Returns a dict of instantiated extensions.
115    """
116    result = {}
117
118    for extension in extensions:
119        if isinstance(extension, str):
120            extension = t.cast(t.Type["Extension"], import_string(extension))
121
122        result[extension.identifier] = extension(environment)
123
124    return result

Load the extensions from the list and bind it to the environment. Returns a dict of instantiated extensions.

class Environment:
 145class Environment:
 146    r"""The core component of Jinja is the `Environment`.  It contains
 147    important shared variables like configuration, filters, tests,
 148    globals and others.  Instances of this class may be modified if
 149    they are not shared and if no template was loaded so far.
 150    Modifications on environments after the first template was loaded
 151    will lead to surprising effects and undefined behavior.
 152
 153    Here are the possible initialization parameters:
 154
 155        `block_start_string`
 156            The string marking the beginning of a block.  Defaults to ``'{%'``.
 157
 158        `block_end_string`
 159            The string marking the end of a block.  Defaults to ``'%}'``.
 160
 161        `variable_start_string`
 162            The string marking the beginning of a print statement.
 163            Defaults to ``'{{'``.
 164
 165        `variable_end_string`
 166            The string marking the end of a print statement.  Defaults to
 167            ``'}}'``.
 168
 169        `comment_start_string`
 170            The string marking the beginning of a comment.  Defaults to ``'{#'``.
 171
 172        `comment_end_string`
 173            The string marking the end of a comment.  Defaults to ``'#}'``.
 174
 175        `line_statement_prefix`
 176            If given and a string, this will be used as prefix for line based
 177            statements.  See also :ref:`line-statements`.
 178
 179        `line_comment_prefix`
 180            If given and a string, this will be used as prefix for line based
 181            comments.  See also :ref:`line-statements`.
 182
 183            .. versionadded:: 2.2
 184
 185        `trim_blocks`
 186            If this is set to ``True`` the first newline after a block is
 187            removed (block, not variable tag!).  Defaults to `False`.
 188
 189        `lstrip_blocks`
 190            If this is set to ``True`` leading spaces and tabs are stripped
 191            from the start of a line to a block.  Defaults to `False`.
 192
 193        `newline_sequence`
 194            The sequence that starts a newline.  Must be one of ``'\r'``,
 195            ``'\n'`` or ``'\r\n'``.  The default is ``'\n'`` which is a
 196            useful default for Linux and OS X systems as well as web
 197            applications.
 198
 199        `keep_trailing_newline`
 200            Preserve the trailing newline when rendering templates.
 201            The default is ``False``, which causes a single newline,
 202            if present, to be stripped from the end of the template.
 203
 204            .. versionadded:: 2.7
 205
 206        `extensions`
 207            List of Jinja extensions to use.  This can either be import paths
 208            as strings or extension classes.  For more information have a
 209            look at :ref:`the extensions documentation <jinja-extensions>`.
 210
 211        `optimized`
 212            should the optimizer be enabled?  Default is ``True``.
 213
 214        `undefined`
 215            :class:`Undefined` or a subclass of it that is used to represent
 216            undefined values in the template.
 217
 218        `finalize`
 219            A callable that can be used to process the result of a variable
 220            expression before it is output.  For example one can convert
 221            ``None`` implicitly into an empty string here.
 222
 223        `autoescape`
 224            If set to ``True`` the XML/HTML autoescaping feature is enabled by
 225            default.  For more details about autoescaping see
 226            :class:`~markupsafe.Markup`.  As of Jinja 2.4 this can also
 227            be a callable that is passed the template name and has to
 228            return ``True`` or ``False`` depending on autoescape should be
 229            enabled by default.
 230
 231            .. versionchanged:: 2.4
 232               `autoescape` can now be a function
 233
 234        `loader`
 235            The template loader for this environment.
 236
 237        `cache_size`
 238            The size of the cache.  Per default this is ``400`` which means
 239            that if more than 400 templates are loaded the loader will clean
 240            out the least recently used template.  If the cache size is set to
 241            ``0`` templates are recompiled all the time, if the cache size is
 242            ``-1`` the cache will not be cleaned.
 243
 244            .. versionchanged:: 2.8
 245               The cache size was increased to 400 from a low 50.
 246
 247        `auto_reload`
 248            Some loaders load templates from locations where the template
 249            sources may change (ie: file system or database).  If
 250            ``auto_reload`` is set to ``True`` (default) every time a template is
 251            requested the loader checks if the source changed and if yes, it
 252            will reload the template.  For higher performance it's possible to
 253            disable that.
 254
 255        `bytecode_cache`
 256            If set to a bytecode cache object, this object will provide a
 257            cache for the internal Jinja bytecode so that templates don't
 258            have to be parsed if they were not changed.
 259
 260            See :ref:`bytecode-cache` for more information.
 261
 262        `enable_async`
 263            If set to true this enables async template execution which
 264            allows using async functions and generators.
 265    """
 266
 267    #: if this environment is sandboxed.  Modifying this variable won't make
 268    #: the environment sandboxed though.  For a real sandboxed environment
 269    #: have a look at jinja2.sandbox.  This flag alone controls the code
 270    #: generation by the compiler.
 271    sandboxed = False
 272
 273    #: True if the environment is just an overlay
 274    overlayed = False
 275
 276    #: the environment this environment is linked to if it is an overlay
 277    linked_to: t.Optional["Environment"] = None
 278
 279    #: shared environments have this set to `True`.  A shared environment
 280    #: must not be modified
 281    shared = False
 282
 283    #: the class that is used for code generation.  See
 284    #: :class:`~jinja2.compiler.CodeGenerator` for more information.
 285    code_generator_class: t.Type["CodeGenerator"] = CodeGenerator
 286
 287    concat = "".join
 288
 289    #: the context class that is used for templates.  See
 290    #: :class:`~jinja2.runtime.Context` for more information.
 291    context_class: t.Type[Context] = Context
 292
 293    template_class: t.Type["Template"]
 294
 295    def __init__(
 296        self,
 297        block_start_string: str = BLOCK_START_STRING,
 298        block_end_string: str = BLOCK_END_STRING,
 299        variable_start_string: str = VARIABLE_START_STRING,
 300        variable_end_string: str = VARIABLE_END_STRING,
 301        comment_start_string: str = COMMENT_START_STRING,
 302        comment_end_string: str = COMMENT_END_STRING,
 303        line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
 304        line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
 305        trim_blocks: bool = TRIM_BLOCKS,
 306        lstrip_blocks: bool = LSTRIP_BLOCKS,
 307        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
 308        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
 309        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
 310        optimized: bool = True,
 311        undefined: t.Type[Undefined] = Undefined,
 312        finalize: t.Optional[t.Callable[..., t.Any]] = None,
 313        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
 314        loader: t.Optional["BaseLoader"] = None,
 315        cache_size: int = 400,
 316        auto_reload: bool = True,
 317        bytecode_cache: t.Optional["BytecodeCache"] = None,
 318        enable_async: bool = False,
 319    ):
 320        # !!Important notice!!
 321        #   The constructor accepts quite a few arguments that should be
 322        #   passed by keyword rather than position.  However it's important to
 323        #   not change the order of arguments because it's used at least
 324        #   internally in those cases:
 325        #       -   spontaneous environments (i18n extension and Template)
 326        #       -   unittests
 327        #   If parameter changes are required only add parameters at the end
 328        #   and don't change the arguments (or the defaults!) of the arguments
 329        #   existing already.
 330
 331        # lexer / parser information
 332        self.block_start_string = block_start_string
 333        self.block_end_string = block_end_string
 334        self.variable_start_string = variable_start_string
 335        self.variable_end_string = variable_end_string
 336        self.comment_start_string = comment_start_string
 337        self.comment_end_string = comment_end_string
 338        self.line_statement_prefix = line_statement_prefix
 339        self.line_comment_prefix = line_comment_prefix
 340        self.trim_blocks = trim_blocks
 341        self.lstrip_blocks = lstrip_blocks
 342        self.newline_sequence = newline_sequence
 343        self.keep_trailing_newline = keep_trailing_newline
 344
 345        # runtime information
 346        self.undefined: t.Type[Undefined] = undefined
 347        self.optimized = optimized
 348        self.finalize = finalize
 349        self.autoescape = autoescape
 350
 351        # defaults
 352        self.filters = DEFAULT_FILTERS.copy()
 353        self.tests = DEFAULT_TESTS.copy()
 354        self.globals = DEFAULT_NAMESPACE.copy()
 355
 356        # set the loader provided
 357        self.loader = loader
 358        self.cache = create_cache(cache_size)
 359        self.bytecode_cache = bytecode_cache
 360        self.auto_reload = auto_reload
 361
 362        # configurable policies
 363        self.policies = DEFAULT_POLICIES.copy()
 364
 365        # load extensions
 366        self.extensions = load_extensions(self, extensions)
 367
 368        self.is_async = enable_async
 369        _environment_config_check(self)
 370
 371    def add_extension(self, extension: t.Union[str, t.Type["Extension"]]) -> None:
 372        """Adds an extension after the environment was created.
 373
 374        .. versionadded:: 2.5
 375        """
 376        self.extensions.update(load_extensions(self, [extension]))
 377
 378    def extend(self, **attributes: t.Any) -> None:
 379        """Add the items to the instance of the environment if they do not exist
 380        yet.  This is used by :ref:`extensions <writing-extensions>` to register
 381        callbacks and configuration values without breaking inheritance.
 382        """
 383        for key, value in attributes.items():
 384            if not hasattr(self, key):
 385                setattr(self, key, value)
 386
 387    def overlay(
 388        self,
 389        block_start_string: str = missing,
 390        block_end_string: str = missing,
 391        variable_start_string: str = missing,
 392        variable_end_string: str = missing,
 393        comment_start_string: str = missing,
 394        comment_end_string: str = missing,
 395        line_statement_prefix: t.Optional[str] = missing,
 396        line_comment_prefix: t.Optional[str] = missing,
 397        trim_blocks: bool = missing,
 398        lstrip_blocks: bool = missing,
 399        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = missing,
 400        keep_trailing_newline: bool = missing,
 401        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = missing,
 402        optimized: bool = missing,
 403        undefined: t.Type[Undefined] = missing,
 404        finalize: t.Optional[t.Callable[..., t.Any]] = missing,
 405        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = missing,
 406        loader: t.Optional["BaseLoader"] = missing,
 407        cache_size: int = missing,
 408        auto_reload: bool = missing,
 409        bytecode_cache: t.Optional["BytecodeCache"] = missing,
 410        enable_async: bool = missing,
 411    ) -> "te.Self":
 412        """Create a new overlay environment that shares all the data with the
 413        current environment except for cache and the overridden attributes.
 414        Extensions cannot be removed for an overlayed environment.  An overlayed
 415        environment automatically gets all the extensions of the environment it
 416        is linked to plus optional extra extensions.
 417
 418        Creating overlays should happen after the initial environment was set
 419        up completely.  Not all attributes are truly linked, some are just
 420        copied over so modifications on the original environment may not shine
 421        through.
 422
 423        .. versionchanged:: 3.1.5
 424            ``enable_async`` is applied correctly.
 425
 426        .. versionchanged:: 3.1.2
 427            Added the ``newline_sequence``, ``keep_trailing_newline``,
 428            and ``enable_async`` parameters to match ``__init__``.
 429        """
 430        args = dict(locals())
 431        del args["self"], args["cache_size"], args["extensions"], args["enable_async"]
 432
 433        rv = object.__new__(self.__class__)
 434        rv.__dict__.update(self.__dict__)
 435        rv.overlayed = True
 436        rv.linked_to = self
 437
 438        for key, value in args.items():
 439            if value is not missing:
 440                setattr(rv, key, value)
 441
 442        if cache_size is not missing:
 443            rv.cache = create_cache(cache_size)
 444        else:
 445            rv.cache = copy_cache(self.cache)
 446
 447        rv.extensions = {}
 448        for key, value in self.extensions.items():
 449            rv.extensions[key] = value.bind(rv)
 450        if extensions is not missing:
 451            rv.extensions.update(load_extensions(rv, extensions))
 452
 453        if enable_async is not missing:
 454            rv.is_async = enable_async
 455
 456        return _environment_config_check(rv)
 457
 458    @property
 459    def lexer(self) -> Lexer:
 460        """The lexer for this environment."""
 461        return get_lexer(self)
 462
 463    def iter_extensions(self) -> t.Iterator["Extension"]:
 464        """Iterates over the extensions by priority."""
 465        return iter(sorted(self.extensions.values(), key=lambda x: x.priority))
 466
 467    def getitem(
 468        self, obj: t.Any, argument: t.Union[str, t.Any]
 469    ) -> t.Union[t.Any, Undefined]:
 470        """Get an item or attribute of an object but prefer the item."""
 471        try:
 472            return obj[argument]
 473        except (AttributeError, TypeError, LookupError):
 474            if isinstance(argument, str):
 475                try:
 476                    attr = str(argument)
 477                except Exception:
 478                    pass
 479                else:
 480                    try:
 481                        return getattr(obj, attr)
 482                    except AttributeError:
 483                        pass
 484            return self.undefined(obj=obj, name=argument)
 485
 486    def getattr(self, obj: t.Any, attribute: str) -> t.Any:
 487        """Get an item or attribute of an object but prefer the attribute.
 488        Unlike :meth:`getitem` the attribute *must* be a string.
 489        """
 490        try:
 491            return getattr(obj, attribute)
 492        except AttributeError:
 493            pass
 494        try:
 495            return obj[attribute]
 496        except (TypeError, LookupError, AttributeError):
 497            return self.undefined(obj=obj, name=attribute)
 498
 499    def _filter_test_common(
 500        self,
 501        name: t.Union[str, Undefined],
 502        value: t.Any,
 503        args: t.Optional[t.Sequence[t.Any]],
 504        kwargs: t.Optional[t.Mapping[str, t.Any]],
 505        context: t.Optional[Context],
 506        eval_ctx: t.Optional[EvalContext],
 507        is_filter: bool,
 508    ) -> t.Any:
 509        if is_filter:
 510            env_map = self.filters
 511            type_name = "filter"
 512        else:
 513            env_map = self.tests
 514            type_name = "test"
 515
 516        func = env_map.get(name)  # type: ignore
 517
 518        if func is None:
 519            msg = f"No {type_name} named {name!r}."
 520
 521            if isinstance(name, Undefined):
 522                try:
 523                    name._fail_with_undefined_error()
 524                except Exception as e:
 525                    msg = f"{msg} ({e}; did you forget to quote the callable name?)"
 526
 527            raise TemplateRuntimeError(msg)
 528
 529        args = [value, *(args if args is not None else ())]
 530        kwargs = kwargs if kwargs is not None else {}
 531        pass_arg = _PassArg.from_obj(func)
 532
 533        if pass_arg is _PassArg.context:
 534            if context is None:
 535                raise TemplateRuntimeError(
 536                    f"Attempted to invoke a context {type_name} without context."
 537                )
 538
 539            args.insert(0, context)
 540        elif pass_arg is _PassArg.eval_context:
 541            if eval_ctx is None:
 542                if context is not None:
 543                    eval_ctx = context.eval_ctx
 544                else:
 545                    eval_ctx = EvalContext(self)
 546
 547            args.insert(0, eval_ctx)
 548        elif pass_arg is _PassArg.environment:
 549            args.insert(0, self)
 550
 551        return func(*args, **kwargs)
 552
 553    def call_filter(
 554        self,
 555        name: str,
 556        value: t.Any,
 557        args: t.Optional[t.Sequence[t.Any]] = None,
 558        kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
 559        context: t.Optional[Context] = None,
 560        eval_ctx: t.Optional[EvalContext] = None,
 561    ) -> t.Any:
 562        """Invoke a filter on a value the same way the compiler does.
 563
 564        This might return a coroutine if the filter is running from an
 565        environment in async mode and the filter supports async
 566        execution. It's your responsibility to await this if needed.
 567
 568        .. versionadded:: 2.7
 569        """
 570        return self._filter_test_common(
 571            name, value, args, kwargs, context, eval_ctx, True
 572        )
 573
 574    def call_test(
 575        self,
 576        name: str,
 577        value: t.Any,
 578        args: t.Optional[t.Sequence[t.Any]] = None,
 579        kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
 580        context: t.Optional[Context] = None,
 581        eval_ctx: t.Optional[EvalContext] = None,
 582    ) -> t.Any:
 583        """Invoke a test on a value the same way the compiler does.
 584
 585        This might return a coroutine if the test is running from an
 586        environment in async mode and the test supports async execution.
 587        It's your responsibility to await this if needed.
 588
 589        .. versionchanged:: 3.0
 590            Tests support ``@pass_context``, etc. decorators. Added
 591            the ``context`` and ``eval_ctx`` parameters.
 592
 593        .. versionadded:: 2.7
 594        """
 595        return self._filter_test_common(
 596            name, value, args, kwargs, context, eval_ctx, False
 597        )
 598
 599    @internalcode
 600    def parse(
 601        self,
 602        source: str,
 603        name: t.Optional[str] = None,
 604        filename: t.Optional[str] = None,
 605    ) -> nodes.Template:
 606        """Parse the sourcecode and return the abstract syntax tree.  This
 607        tree of nodes is used by the compiler to convert the template into
 608        executable source- or bytecode.  This is useful for debugging or to
 609        extract information from templates.
 610
 611        If you are :ref:`developing Jinja extensions <writing-extensions>`
 612        this gives you a good overview of the node tree generated.
 613        """
 614        try:
 615            return self._parse(source, name, filename)
 616        except TemplateSyntaxError:
 617            self.handle_exception(source=source)
 618
 619    def _parse(
 620        self, source: str, name: t.Optional[str], filename: t.Optional[str]
 621    ) -> nodes.Template:
 622        """Internal parsing function used by `parse` and `compile`."""
 623        return Parser(self, source, name, filename).parse()
 624
 625    def lex(
 626        self,
 627        source: str,
 628        name: t.Optional[str] = None,
 629        filename: t.Optional[str] = None,
 630    ) -> t.Iterator[t.Tuple[int, str, str]]:
 631        """Lex the given sourcecode and return a generator that yields
 632        tokens as tuples in the form ``(lineno, token_type, value)``.
 633        This can be useful for :ref:`extension development <writing-extensions>`
 634        and debugging templates.
 635
 636        This does not perform preprocessing.  If you want the preprocessing
 637        of the extensions to be applied you have to filter source through
 638        the :meth:`preprocess` method.
 639        """
 640        source = str(source)
 641        try:
 642            return self.lexer.tokeniter(source, name, filename)
 643        except TemplateSyntaxError:
 644            self.handle_exception(source=source)
 645
 646    def preprocess(
 647        self,
 648        source: str,
 649        name: t.Optional[str] = None,
 650        filename: t.Optional[str] = None,
 651    ) -> str:
 652        """Preprocesses the source with all extensions.  This is automatically
 653        called for all parsing and compiling methods but *not* for :meth:`lex`
 654        because there you usually only want the actual source tokenized.
 655        """
 656        return reduce(
 657            lambda s, e: e.preprocess(s, name, filename),
 658            self.iter_extensions(),
 659            str(source),
 660        )
 661
 662    def _tokenize(
 663        self,
 664        source: str,
 665        name: t.Optional[str],
 666        filename: t.Optional[str] = None,
 667        state: t.Optional[str] = None,
 668    ) -> TokenStream:
 669        """Called by the parser to do the preprocessing and filtering
 670        for all the extensions.  Returns a :class:`~jinja2.lexer.TokenStream`.
 671        """
 672        source = self.preprocess(source, name, filename)
 673        stream = self.lexer.tokenize(source, name, filename, state)
 674
 675        for ext in self.iter_extensions():
 676            stream = ext.filter_stream(stream)  # type: ignore
 677
 678            if not isinstance(stream, TokenStream):
 679                stream = TokenStream(stream, name, filename)
 680
 681        return stream
 682
 683    def _generate(
 684        self,
 685        source: nodes.Template,
 686        name: t.Optional[str],
 687        filename: t.Optional[str],
 688        defer_init: bool = False,
 689    ) -> str:
 690        """Internal hook that can be overridden to hook a different generate
 691        method in.
 692
 693        .. versionadded:: 2.5
 694        """
 695        return generate(  # type: ignore
 696            source,
 697            self,
 698            name,
 699            filename,
 700            defer_init=defer_init,
 701            optimized=self.optimized,
 702        )
 703
 704    def _compile(self, source: str, filename: str) -> CodeType:
 705        """Internal hook that can be overridden to hook a different compile
 706        method in.
 707
 708        .. versionadded:: 2.5
 709        """
 710        return compile(source, filename, "exec")
 711
 712    @typing.overload
 713    def compile(
 714        self,
 715        source: t.Union[str, nodes.Template],
 716        name: t.Optional[str] = None,
 717        filename: t.Optional[str] = None,
 718        raw: "te.Literal[False]" = False,
 719        defer_init: bool = False,
 720    ) -> CodeType: ...
 721
 722    @typing.overload
 723    def compile(
 724        self,
 725        source: t.Union[str, nodes.Template],
 726        name: t.Optional[str] = None,
 727        filename: t.Optional[str] = None,
 728        raw: "te.Literal[True]" = ...,
 729        defer_init: bool = False,
 730    ) -> str: ...
 731
 732    @internalcode
 733    def compile(
 734        self,
 735        source: t.Union[str, nodes.Template],
 736        name: t.Optional[str] = None,
 737        filename: t.Optional[str] = None,
 738        raw: bool = False,
 739        defer_init: bool = False,
 740    ) -> t.Union[str, CodeType]:
 741        """Compile a node or template source code.  The `name` parameter is
 742        the load name of the template after it was joined using
 743        :meth:`join_path` if necessary, not the filename on the file system.
 744        the `filename` parameter is the estimated filename of the template on
 745        the file system.  If the template came from a database or memory this
 746        can be omitted.
 747
 748        The return value of this method is a python code object.  If the `raw`
 749        parameter is `True` the return value will be a string with python
 750        code equivalent to the bytecode returned otherwise.  This method is
 751        mainly used internally.
 752
 753        `defer_init` is use internally to aid the module code generator.  This
 754        causes the generated code to be able to import without the global
 755        environment variable to be set.
 756
 757        .. versionadded:: 2.4
 758           `defer_init` parameter added.
 759        """
 760        source_hint = None
 761        try:
 762            if isinstance(source, str):
 763                source_hint = source
 764                source = self._parse(source, name, filename)
 765            source = self._generate(source, name, filename, defer_init=defer_init)
 766            if raw:
 767                return source
 768            if filename is None:
 769                filename = "<template>"
 770            return self._compile(source, filename)
 771        except TemplateSyntaxError:
 772            self.handle_exception(source=source_hint)
 773
 774    def compile_expression(
 775        self, source: str, undefined_to_none: bool = True
 776    ) -> "TemplateExpression":
 777        """A handy helper method that returns a callable that accepts keyword
 778        arguments that appear as variables in the expression.  If called it
 779        returns the result of the expression.
 780
 781        This is useful if applications want to use the same rules as Jinja
 782        in template "configuration files" or similar situations.
 783
 784        Example usage:
 785
 786        >>> env = Environment()
 787        >>> expr = env.compile_expression('foo == 42')
 788        >>> expr(foo=23)
 789        False
 790        >>> expr(foo=42)
 791        True
 792
 793        Per default the return value is converted to `None` if the
 794        expression returns an undefined value.  This can be changed
 795        by setting `undefined_to_none` to `False`.
 796
 797        >>> env.compile_expression('var')() is None
 798        True
 799        >>> env.compile_expression('var', undefined_to_none=False)()
 800        Undefined
 801
 802        .. versionadded:: 2.1
 803        """
 804        parser = Parser(self, source, state="variable")
 805        try:
 806            expr = parser.parse_expression()
 807            if not parser.stream.eos:
 808                raise TemplateSyntaxError(
 809                    "chunk after expression", parser.stream.current.lineno, None, None
 810                )
 811            expr.set_environment(self)
 812        except TemplateSyntaxError:
 813            self.handle_exception(source=source)
 814
 815        body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)]
 816        template = self.from_string(nodes.Template(body, lineno=1))
 817        return TemplateExpression(template, undefined_to_none)
 818
 819    def compile_templates(
 820        self,
 821        target: t.Union[str, "os.PathLike[str]"],
 822        extensions: t.Optional[t.Collection[str]] = None,
 823        filter_func: t.Optional[t.Callable[[str], bool]] = None,
 824        zip: t.Optional[str] = "deflated",
 825        log_function: t.Optional[t.Callable[[str], None]] = None,
 826        ignore_errors: bool = True,
 827    ) -> None:
 828        """Finds all the templates the loader can find, compiles them
 829        and stores them in `target`.  If `zip` is `None`, instead of in a
 830        zipfile, the templates will be stored in a directory.
 831        By default a deflate zip algorithm is used. To switch to
 832        the stored algorithm, `zip` can be set to ``'stored'``.
 833
 834        `extensions` and `filter_func` are passed to :meth:`list_templates`.
 835        Each template returned will be compiled to the target folder or
 836        zipfile.
 837
 838        By default template compilation errors are ignored.  In case a
 839        log function is provided, errors are logged.  If you want template
 840        syntax errors to abort the compilation you can set `ignore_errors`
 841        to `False` and you will get an exception on syntax errors.
 842
 843        .. versionadded:: 2.4
 844        """
 845        from .loaders import ModuleLoader
 846
 847        if log_function is None:
 848
 849            def log_function(x: str) -> None:
 850                pass
 851
 852        assert log_function is not None
 853        assert self.loader is not None, "No loader configured."
 854
 855        def write_file(filename: str, data: str) -> None:
 856            if zip:
 857                info = ZipInfo(filename)
 858                info.external_attr = 0o755 << 16
 859                zip_file.writestr(info, data)
 860            else:
 861                with open(os.path.join(target, filename), "wb") as f:
 862                    f.write(data.encode("utf8"))
 863
 864        if zip is not None:
 865            from zipfile import ZIP_DEFLATED
 866            from zipfile import ZIP_STORED
 867            from zipfile import ZipFile
 868            from zipfile import ZipInfo
 869
 870            zip_file = ZipFile(
 871                target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip]
 872            )
 873            log_function(f"Compiling into Zip archive {target!r}")
 874        else:
 875            if not os.path.isdir(target):
 876                os.makedirs(target)
 877            log_function(f"Compiling into folder {target!r}")
 878
 879        try:
 880            for name in self.list_templates(extensions, filter_func):
 881                source, filename, _ = self.loader.get_source(self, name)
 882                try:
 883                    code = self.compile(source, name, filename, True, True)
 884                except TemplateSyntaxError as e:
 885                    if not ignore_errors:
 886                        raise
 887                    log_function(f'Could not compile "{name}": {e}')
 888                    continue
 889
 890                filename = ModuleLoader.get_module_filename(name)
 891
 892                write_file(filename, code)
 893                log_function(f'Compiled "{name}" as {filename}')
 894        finally:
 895            if zip:
 896                zip_file.close()
 897
 898        log_function("Finished compiling templates")
 899
 900    def list_templates(
 901        self,
 902        extensions: t.Optional[t.Collection[str]] = None,
 903        filter_func: t.Optional[t.Callable[[str], bool]] = None,
 904    ) -> t.List[str]:
 905        """Returns a list of templates for this environment.  This requires
 906        that the loader supports the loader's
 907        :meth:`~BaseLoader.list_templates` method.
 908
 909        If there are other files in the template folder besides the
 910        actual templates, the returned list can be filtered.  There are two
 911        ways: either `extensions` is set to a list of file extensions for
 912        templates, or a `filter_func` can be provided which is a callable that
 913        is passed a template name and should return `True` if it should end up
 914        in the result list.
 915
 916        If the loader does not support that, a :exc:`TypeError` is raised.
 917
 918        .. versionadded:: 2.4
 919        """
 920        assert self.loader is not None, "No loader configured."
 921        names = self.loader.list_templates()
 922
 923        if extensions is not None:
 924            if filter_func is not None:
 925                raise TypeError(
 926                    "either extensions or filter_func can be passed, but not both"
 927                )
 928
 929            def filter_func(x: str) -> bool:
 930                return "." in x and x.rsplit(".", 1)[1] in extensions
 931
 932        if filter_func is not None:
 933            names = [name for name in names if filter_func(name)]
 934
 935        return names
 936
 937    def handle_exception(self, source: t.Optional[str] = None) -> "te.NoReturn":
 938        """Exception handling helper.  This is used internally to either raise
 939        rewritten exceptions or return a rendered traceback for the template.
 940        """
 941        from .debug import rewrite_traceback_stack
 942
 943        raise rewrite_traceback_stack(source=source)
 944
 945    def join_path(self, template: str, parent: str) -> str:
 946        """Join a template with the parent.  By default all the lookups are
 947        relative to the loader root so this method returns the `template`
 948        parameter unchanged, but if the paths should be relative to the
 949        parent template, this function can be used to calculate the real
 950        template name.
 951
 952        Subclasses may override this method and implement template path
 953        joining here.
 954        """
 955        return template
 956
 957    @internalcode
 958    def _load_template(
 959        self, name: str, globals: t.Optional[t.MutableMapping[str, t.Any]]
 960    ) -> "Template":
 961        if self.loader is None:
 962            raise TypeError("no loader for this environment specified")
 963        cache_key = (weakref.ref(self.loader), name)
 964        if self.cache is not None:
 965            template = self.cache.get(cache_key)
 966            if template is not None and (
 967                not self.auto_reload or template.is_up_to_date
 968            ):
 969                # template.globals is a ChainMap, modifying it will only
 970                # affect the template, not the environment globals.
 971                if globals:
 972                    template.globals.update(globals)
 973
 974                return template
 975
 976        template = self.loader.load(self, name, self.make_globals(globals))
 977
 978        if self.cache is not None:
 979            self.cache[cache_key] = template
 980        return template
 981
 982    @internalcode
 983    def get_template(
 984        self,
 985        name: t.Union[str, "Template"],
 986        parent: t.Optional[str] = None,
 987        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
 988    ) -> "Template":
 989        """Load a template by name with :attr:`loader` and return a
 990        :class:`Template`. If the template does not exist a
 991        :exc:`TemplateNotFound` exception is raised.
 992
 993        :param name: Name of the template to load. When loading
 994            templates from the filesystem, "/" is used as the path
 995            separator, even on Windows.
 996        :param parent: The name of the parent template importing this
 997            template. :meth:`join_path` can be used to implement name
 998            transformations with this.
 999        :param globals: Extend the environment :attr:`globals` with
1000            these extra variables available for all renders of this
1001            template. If the template has already been loaded and
1002            cached, its globals are updated with any new items.
1003
1004        .. versionchanged:: 3.0
1005            If a template is loaded from cache, ``globals`` will update
1006            the template's globals instead of ignoring the new values.
1007
1008        .. versionchanged:: 2.4
1009            If ``name`` is a :class:`Template` object it is returned
1010            unchanged.
1011        """
1012        if isinstance(name, Template):
1013            return name
1014        if parent is not None:
1015            name = self.join_path(name, parent)
1016
1017        return self._load_template(name, globals)
1018
1019    @internalcode
1020    def select_template(
1021        self,
1022        names: t.Iterable[t.Union[str, "Template"]],
1023        parent: t.Optional[str] = None,
1024        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1025    ) -> "Template":
1026        """Like :meth:`get_template`, but tries loading multiple names.
1027        If none of the names can be loaded a :exc:`TemplatesNotFound`
1028        exception is raised.
1029
1030        :param names: List of template names to try loading in order.
1031        :param parent: The name of the parent template importing this
1032            template. :meth:`join_path` can be used to implement name
1033            transformations with this.
1034        :param globals: Extend the environment :attr:`globals` with
1035            these extra variables available for all renders of this
1036            template. If the template has already been loaded and
1037            cached, its globals are updated with any new items.
1038
1039        .. versionchanged:: 3.0
1040            If a template is loaded from cache, ``globals`` will update
1041            the template's globals instead of ignoring the new values.
1042
1043        .. versionchanged:: 2.11
1044            If ``names`` is :class:`Undefined`, an :exc:`UndefinedError`
1045            is raised instead. If no templates were found and ``names``
1046            contains :class:`Undefined`, the message is more helpful.
1047
1048        .. versionchanged:: 2.4
1049            If ``names`` contains a :class:`Template` object it is
1050            returned unchanged.
1051
1052        .. versionadded:: 2.3
1053        """
1054        if isinstance(names, Undefined):
1055            names._fail_with_undefined_error()
1056
1057        if not names:
1058            raise TemplatesNotFound(
1059                message="Tried to select from an empty list of templates."
1060            )
1061
1062        for name in names:
1063            if isinstance(name, Template):
1064                return name
1065            if parent is not None:
1066                name = self.join_path(name, parent)
1067            try:
1068                return self._load_template(name, globals)
1069            except (TemplateNotFound, UndefinedError):
1070                pass
1071        raise TemplatesNotFound(names)  # type: ignore
1072
1073    @internalcode
1074    def get_or_select_template(
1075        self,
1076        template_name_or_list: t.Union[
1077            str, "Template", t.List[t.Union[str, "Template"]]
1078        ],
1079        parent: t.Optional[str] = None,
1080        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1081    ) -> "Template":
1082        """Use :meth:`select_template` if an iterable of template names
1083        is given, or :meth:`get_template` if one name is given.
1084
1085        .. versionadded:: 2.3
1086        """
1087        if isinstance(template_name_or_list, (str, Undefined)):
1088            return self.get_template(template_name_or_list, parent, globals)
1089        elif isinstance(template_name_or_list, Template):
1090            return template_name_or_list
1091        return self.select_template(template_name_or_list, parent, globals)
1092
1093    def from_string(
1094        self,
1095        source: t.Union[str, nodes.Template],
1096        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1097        template_class: t.Optional[t.Type["Template"]] = None,
1098    ) -> "Template":
1099        """Load a template from a source string without using
1100        :attr:`loader`.
1101
1102        :param source: Jinja source to compile into a template.
1103        :param globals: Extend the environment :attr:`globals` with
1104            these extra variables available for all renders of this
1105            template. If the template has already been loaded and
1106            cached, its globals are updated with any new items.
1107        :param template_class: Return an instance of this
1108            :class:`Template` class.
1109        """
1110        gs = self.make_globals(globals)
1111        cls = template_class or self.template_class
1112        return cls.from_code(self, self.compile(source), gs, None)
1113
1114    def make_globals(
1115        self, d: t.Optional[t.MutableMapping[str, t.Any]]
1116    ) -> t.MutableMapping[str, t.Any]:
1117        """Make the globals map for a template. Any given template
1118        globals overlay the environment :attr:`globals`.
1119
1120        Returns a :class:`collections.ChainMap`. This allows any changes
1121        to a template's globals to only affect that template, while
1122        changes to the environment's globals are still reflected.
1123        However, avoid modifying any globals after a template is loaded.
1124
1125        :param d: Dict of template-specific globals.
1126
1127        .. versionchanged:: 3.0
1128            Use :class:`collections.ChainMap` to always prevent mutating
1129            environment globals.
1130        """
1131        if d is None:
1132            d = {}
1133
1134        return ChainMap(d, self.globals)

The core component of Jinja is the Environment. It contains important shared variables like configuration, filters, tests, globals and others. Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior.

Here are the possible initialization parameters:

block_start_string The string marking the beginning of a block. Defaults to '{%'.

block_end_string The string marking the end of a block. Defaults to '%}'.

variable_start_string The string marking the beginning of a print statement. Defaults to '{{'.

variable_end_string The string marking the end of a print statement. Defaults to '}}'.

comment_start_string The string marking the beginning of a comment. Defaults to '{#'.

comment_end_string The string marking the end of a comment. Defaults to '#}'.

line_statement_prefix If given and a string, this will be used as prefix for line based statements. See also :ref:line-statements.

line_comment_prefix If given and a string, this will be used as prefix for line based comments. See also :ref:line-statements.

*New in version 2.2.*

trim_blocks If this is set to True the first newline after a block is removed (block, not variable tag!). Defaults to False.

lstrip_blocks If this is set to True leading spaces and tabs are stripped from the start of a line to a block. Defaults to False.

newline_sequence The sequence that starts a newline. Must be one of '\r', '\n' or '\r\n'. The default is '\n' which is a useful default for Linux and OS X systems as well as web applications.

keep_trailing_newline Preserve the trailing newline when rendering templates. The default is False, which causes a single newline, if present, to be stripped from the end of the template.

*New in version 2.7.*

extensions List of Jinja extensions to use. This can either be import paths as strings or extension classes. For more information have a look at :ref:the extensions documentation <jinja-extensions>.

optimized should the optimizer be enabled? Default is True.

undefined Undefined or a subclass of it that is used to represent undefined values in the template.

finalize A callable that can be used to process the result of a variable expression before it is output. For example one can convert None implicitly into an empty string here.

autoescape If set to True the XML/HTML autoescaping feature is enabled by default. For more details about autoescaping see ~markupsafe.Markup. As of Jinja 2.4 this can also be a callable that is passed the template name and has to return True or False depending on autoescape should be enabled by default.

*Changed in version 2.4:*
`autoescape` can now be a function

loader The template loader for this environment.

cache_size The size of the cache. Per default this is 400 which means that if more than 400 templates are loaded the loader will clean out the least recently used template. If the cache size is set to 0 templates are recompiled all the time, if the cache size is -1 the cache will not be cleaned.

*Changed in version 2.8:*
The cache size was increased to 400 from a low 50.

auto_reload Some loaders load templates from locations where the template sources may change (ie: file system or database). If auto_reload is set to True (default) every time a template is requested the loader checks if the source changed and if yes, it will reload the template. For higher performance it's possible to disable that.

bytecode_cache If set to a bytecode cache object, this object will provide a cache for the internal Jinja bytecode so that templates don't have to be parsed if they were not changed.

See :ref:`bytecode-cache` for more information.

enable_async If set to true this enables async template execution which allows using async functions and generators.

Environment( block_start_string: str = '{%', block_end_string: str = '%}', variable_start_string: str = '{{', variable_end_string: str = '}}', comment_start_string: str = '{#', comment_end_string: str = '#}', line_statement_prefix: Optional[str] = None, line_comment_prefix: Optional[str] = None, trim_blocks: bool = False, lstrip_blocks: bool = False, newline_sequence: Literal['\n', '\r\n', '\r'] = '\n', keep_trailing_newline: bool = False, extensions: Sequence[Union[str, Type[jinja2.ext.Extension]]] = (), optimized: bool = True, undefined: Type[jinja2.runtime.Undefined] = <class 'jinja2.runtime.Undefined'>, finalize: Optional[Callable[..., Any]] = None, autoescape: Union[bool, Callable[[Optional[str]], bool]] = False, loader: Optional[jinja2.loaders.BaseLoader] = None, cache_size: int = 400, auto_reload: bool = True, bytecode_cache: Optional[jinja2.bccache.BytecodeCache] = None, enable_async: bool = False)
295    def __init__(
296        self,
297        block_start_string: str = BLOCK_START_STRING,
298        block_end_string: str = BLOCK_END_STRING,
299        variable_start_string: str = VARIABLE_START_STRING,
300        variable_end_string: str = VARIABLE_END_STRING,
301        comment_start_string: str = COMMENT_START_STRING,
302        comment_end_string: str = COMMENT_END_STRING,
303        line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
304        line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
305        trim_blocks: bool = TRIM_BLOCKS,
306        lstrip_blocks: bool = LSTRIP_BLOCKS,
307        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
308        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
309        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
310        optimized: bool = True,
311        undefined: t.Type[Undefined] = Undefined,
312        finalize: t.Optional[t.Callable[..., t.Any]] = None,
313        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
314        loader: t.Optional["BaseLoader"] = None,
315        cache_size: int = 400,
316        auto_reload: bool = True,
317        bytecode_cache: t.Optional["BytecodeCache"] = None,
318        enable_async: bool = False,
319    ):
320        # !!Important notice!!
321        #   The constructor accepts quite a few arguments that should be
322        #   passed by keyword rather than position.  However it's important to
323        #   not change the order of arguments because it's used at least
324        #   internally in those cases:
325        #       -   spontaneous environments (i18n extension and Template)
326        #       -   unittests
327        #   If parameter changes are required only add parameters at the end
328        #   and don't change the arguments (or the defaults!) of the arguments
329        #   existing already.
330
331        # lexer / parser information
332        self.block_start_string = block_start_string
333        self.block_end_string = block_end_string
334        self.variable_start_string = variable_start_string
335        self.variable_end_string = variable_end_string
336        self.comment_start_string = comment_start_string
337        self.comment_end_string = comment_end_string
338        self.line_statement_prefix = line_statement_prefix
339        self.line_comment_prefix = line_comment_prefix
340        self.trim_blocks = trim_blocks
341        self.lstrip_blocks = lstrip_blocks
342        self.newline_sequence = newline_sequence
343        self.keep_trailing_newline = keep_trailing_newline
344
345        # runtime information
346        self.undefined: t.Type[Undefined] = undefined
347        self.optimized = optimized
348        self.finalize = finalize
349        self.autoescape = autoescape
350
351        # defaults
352        self.filters = DEFAULT_FILTERS.copy()
353        self.tests = DEFAULT_TESTS.copy()
354        self.globals = DEFAULT_NAMESPACE.copy()
355
356        # set the loader provided
357        self.loader = loader
358        self.cache = create_cache(cache_size)
359        self.bytecode_cache = bytecode_cache
360        self.auto_reload = auto_reload
361
362        # configurable policies
363        self.policies = DEFAULT_POLICIES.copy()
364
365        # load extensions
366        self.extensions = load_extensions(self, extensions)
367
368        self.is_async = enable_async
369        _environment_config_check(self)
sandboxed = False
overlayed = False
linked_to: Optional[Environment] = None
shared = False
code_generator_class: Type[jinja2.compiler.CodeGenerator] = <class 'jinja2.compiler.CodeGenerator'>
def concat(iterable, /):

Concatenate any number of strings.

The string whose method is called is inserted in between each given string. The result is returned as a new string.

Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'

context_class: Type[jinja2.runtime.Context] = <class 'jinja2.runtime.Context'>
template_class: Type[Template] = <class 'Template'>
block_start_string
block_end_string
variable_start_string
variable_end_string
comment_start_string
comment_end_string
line_statement_prefix
line_comment_prefix
trim_blocks
lstrip_blocks
newline_sequence
keep_trailing_newline
undefined: Type[jinja2.runtime.Undefined]
optimized
finalize
autoescape
filters
tests
globals
loader
cache
bytecode_cache
auto_reload
policies
extensions
is_async
def add_extension(self, extension: Union[str, Type[jinja2.ext.Extension]]) -> None:
371    def add_extension(self, extension: t.Union[str, t.Type["Extension"]]) -> None:
372        """Adds an extension after the environment was created.
373
374        .. versionadded:: 2.5
375        """
376        self.extensions.update(load_extensions(self, [extension]))

Adds an extension after the environment was created.

New in version 2.5.

def extend(self, **attributes: Any) -> None:
378    def extend(self, **attributes: t.Any) -> None:
379        """Add the items to the instance of the environment if they do not exist
380        yet.  This is used by :ref:`extensions <writing-extensions>` to register
381        callbacks and configuration values without breaking inheritance.
382        """
383        for key, value in attributes.items():
384            if not hasattr(self, key):
385                setattr(self, key, value)

Add the items to the instance of the environment if they do not exist yet. This is used by :ref:extensions <writing-extensions> to register callbacks and configuration values without breaking inheritance.

def overlay( self, block_start_string: str = missing, block_end_string: str = missing, variable_start_string: str = missing, variable_end_string: str = missing, comment_start_string: str = missing, comment_end_string: str = missing, line_statement_prefix: Optional[str] = missing, line_comment_prefix: Optional[str] = missing, trim_blocks: bool = missing, lstrip_blocks: bool = missing, newline_sequence: Literal['\n', '\r\n', '\r'] = missing, keep_trailing_newline: bool = missing, extensions: Sequence[Union[str, Type[jinja2.ext.Extension]]] = missing, optimized: bool = missing, undefined: Type[jinja2.runtime.Undefined] = missing, finalize: Optional[Callable[..., Any]] = missing, autoescape: Union[bool, Callable[[Optional[str]], bool]] = missing, loader: Optional[jinja2.loaders.BaseLoader] = missing, cache_size: int = missing, auto_reload: bool = missing, bytecode_cache: Optional[jinja2.bccache.BytecodeCache] = missing, enable_async: bool = missing) -> Self:
387    def overlay(
388        self,
389        block_start_string: str = missing,
390        block_end_string: str = missing,
391        variable_start_string: str = missing,
392        variable_end_string: str = missing,
393        comment_start_string: str = missing,
394        comment_end_string: str = missing,
395        line_statement_prefix: t.Optional[str] = missing,
396        line_comment_prefix: t.Optional[str] = missing,
397        trim_blocks: bool = missing,
398        lstrip_blocks: bool = missing,
399        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = missing,
400        keep_trailing_newline: bool = missing,
401        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = missing,
402        optimized: bool = missing,
403        undefined: t.Type[Undefined] = missing,
404        finalize: t.Optional[t.Callable[..., t.Any]] = missing,
405        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = missing,
406        loader: t.Optional["BaseLoader"] = missing,
407        cache_size: int = missing,
408        auto_reload: bool = missing,
409        bytecode_cache: t.Optional["BytecodeCache"] = missing,
410        enable_async: bool = missing,
411    ) -> "te.Self":
412        """Create a new overlay environment that shares all the data with the
413        current environment except for cache and the overridden attributes.
414        Extensions cannot be removed for an overlayed environment.  An overlayed
415        environment automatically gets all the extensions of the environment it
416        is linked to plus optional extra extensions.
417
418        Creating overlays should happen after the initial environment was set
419        up completely.  Not all attributes are truly linked, some are just
420        copied over so modifications on the original environment may not shine
421        through.
422
423        .. versionchanged:: 3.1.5
424            ``enable_async`` is applied correctly.
425
426        .. versionchanged:: 3.1.2
427            Added the ``newline_sequence``, ``keep_trailing_newline``,
428            and ``enable_async`` parameters to match ``__init__``.
429        """
430        args = dict(locals())
431        del args["self"], args["cache_size"], args["extensions"], args["enable_async"]
432
433        rv = object.__new__(self.__class__)
434        rv.__dict__.update(self.__dict__)
435        rv.overlayed = True
436        rv.linked_to = self
437
438        for key, value in args.items():
439            if value is not missing:
440                setattr(rv, key, value)
441
442        if cache_size is not missing:
443            rv.cache = create_cache(cache_size)
444        else:
445            rv.cache = copy_cache(self.cache)
446
447        rv.extensions = {}
448        for key, value in self.extensions.items():
449            rv.extensions[key] = value.bind(rv)
450        if extensions is not missing:
451            rv.extensions.update(load_extensions(rv, extensions))
452
453        if enable_async is not missing:
454            rv.is_async = enable_async
455
456        return _environment_config_check(rv)

Create a new overlay environment that shares all the data with the current environment except for cache and the overridden attributes. Extensions cannot be removed for an overlayed environment. An overlayed environment automatically gets all the extensions of the environment it is linked to plus optional extra extensions.

Creating overlays should happen after the initial environment was set up completely. Not all attributes are truly linked, some are just copied over so modifications on the original environment may not shine through.

Changed in version 3.1.5: enable_async is applied correctly.

Changed in version 3.1.2: Added the newline_sequence, keep_trailing_newline, and enable_async parameters to match __init__.

lexer: jinja2.lexer.Lexer
458    @property
459    def lexer(self) -> Lexer:
460        """The lexer for this environment."""
461        return get_lexer(self)

The lexer for this environment.

def iter_extensions(self) -> Iterator[jinja2.ext.Extension]:
463    def iter_extensions(self) -> t.Iterator["Extension"]:
464        """Iterates over the extensions by priority."""
465        return iter(sorted(self.extensions.values(), key=lambda x: x.priority))

Iterates over the extensions by priority.

def getitem( self, obj: Any, argument: Union[str, Any]) -> Union[Any, jinja2.runtime.Undefined]:
467    def getitem(
468        self, obj: t.Any, argument: t.Union[str, t.Any]
469    ) -> t.Union[t.Any, Undefined]:
470        """Get an item or attribute of an object but prefer the item."""
471        try:
472            return obj[argument]
473        except (AttributeError, TypeError, LookupError):
474            if isinstance(argument, str):
475                try:
476                    attr = str(argument)
477                except Exception:
478                    pass
479                else:
480                    try:
481                        return getattr(obj, attr)
482                    except AttributeError:
483                        pass
484            return self.undefined(obj=obj, name=argument)

Get an item or attribute of an object but prefer the item.

def getattr(self, obj: Any, attribute: str) -> Any:
486    def getattr(self, obj: t.Any, attribute: str) -> t.Any:
487        """Get an item or attribute of an object but prefer the attribute.
488        Unlike :meth:`getitem` the attribute *must* be a string.
489        """
490        try:
491            return getattr(obj, attribute)
492        except AttributeError:
493            pass
494        try:
495            return obj[attribute]
496        except (TypeError, LookupError, AttributeError):
497            return self.undefined(obj=obj, name=attribute)

Get an item or attribute of an object but prefer the attribute. Unlike getitem() the attribute must be a string.

def call_filter( self, name: str, value: Any, args: Optional[Sequence[Any]] = None, kwargs: Optional[Mapping[str, Any]] = None, context: Optional[jinja2.runtime.Context] = None, eval_ctx: Optional[jinja2.nodes.EvalContext] = None) -> Any:
553    def call_filter(
554        self,
555        name: str,
556        value: t.Any,
557        args: t.Optional[t.Sequence[t.Any]] = None,
558        kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
559        context: t.Optional[Context] = None,
560        eval_ctx: t.Optional[EvalContext] = None,
561    ) -> t.Any:
562        """Invoke a filter on a value the same way the compiler does.
563
564        This might return a coroutine if the filter is running from an
565        environment in async mode and the filter supports async
566        execution. It's your responsibility to await this if needed.
567
568        .. versionadded:: 2.7
569        """
570        return self._filter_test_common(
571            name, value, args, kwargs, context, eval_ctx, True
572        )

Invoke a filter on a value the same way the compiler does.

This might return a coroutine if the filter is running from an environment in async mode and the filter supports async execution. It's your responsibility to await this if needed.

New in version 2.7.

def call_test( self, name: str, value: Any, args: Optional[Sequence[Any]] = None, kwargs: Optional[Mapping[str, Any]] = None, context: Optional[jinja2.runtime.Context] = None, eval_ctx: Optional[jinja2.nodes.EvalContext] = None) -> Any:
574    def call_test(
575        self,
576        name: str,
577        value: t.Any,
578        args: t.Optional[t.Sequence[t.Any]] = None,
579        kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
580        context: t.Optional[Context] = None,
581        eval_ctx: t.Optional[EvalContext] = None,
582    ) -> t.Any:
583        """Invoke a test on a value the same way the compiler does.
584
585        This might return a coroutine if the test is running from an
586        environment in async mode and the test supports async execution.
587        It's your responsibility to await this if needed.
588
589        .. versionchanged:: 3.0
590            Tests support ``@pass_context``, etc. decorators. Added
591            the ``context`` and ``eval_ctx`` parameters.
592
593        .. versionadded:: 2.7
594        """
595        return self._filter_test_common(
596            name, value, args, kwargs, context, eval_ctx, False
597        )

Invoke a test on a value the same way the compiler does.

This might return a coroutine if the test is running from an environment in async mode and the test supports async execution. It's your responsibility to await this if needed.

Changed in version 3.0: Tests support @pass_context, etc. decorators. Added the context and eval_ctx parameters.

New in version 2.7.

@internalcode
def parse( self, source: str, name: Optional[str] = None, filename: Optional[str] = None) -> jinja2.nodes.Template:
599    @internalcode
600    def parse(
601        self,
602        source: str,
603        name: t.Optional[str] = None,
604        filename: t.Optional[str] = None,
605    ) -> nodes.Template:
606        """Parse the sourcecode and return the abstract syntax tree.  This
607        tree of nodes is used by the compiler to convert the template into
608        executable source- or bytecode.  This is useful for debugging or to
609        extract information from templates.
610
611        If you are :ref:`developing Jinja extensions <writing-extensions>`
612        this gives you a good overview of the node tree generated.
613        """
614        try:
615            return self._parse(source, name, filename)
616        except TemplateSyntaxError:
617            self.handle_exception(source=source)

Parse the sourcecode and return the abstract syntax tree. This tree of nodes is used by the compiler to convert the template into executable source- or bytecode. This is useful for debugging or to extract information from templates.

If you are :ref:developing Jinja extensions <writing-extensions> this gives you a good overview of the node tree generated.

def lex( self, source: str, name: Optional[str] = None, filename: Optional[str] = None) -> Iterator[Tuple[int, str, str]]:
625    def lex(
626        self,
627        source: str,
628        name: t.Optional[str] = None,
629        filename: t.Optional[str] = None,
630    ) -> t.Iterator[t.Tuple[int, str, str]]:
631        """Lex the given sourcecode and return a generator that yields
632        tokens as tuples in the form ``(lineno, token_type, value)``.
633        This can be useful for :ref:`extension development <writing-extensions>`
634        and debugging templates.
635
636        This does not perform preprocessing.  If you want the preprocessing
637        of the extensions to be applied you have to filter source through
638        the :meth:`preprocess` method.
639        """
640        source = str(source)
641        try:
642            return self.lexer.tokeniter(source, name, filename)
643        except TemplateSyntaxError:
644            self.handle_exception(source=source)

Lex the given sourcecode and return a generator that yields tokens as tuples in the form (lineno, token_type, value). This can be useful for :ref:extension development <writing-extensions> and debugging templates.

This does not perform preprocessing. If you want the preprocessing of the extensions to be applied you have to filter source through the preprocess() method.

def preprocess( self, source: str, name: Optional[str] = None, filename: Optional[str] = None) -> str:
646    def preprocess(
647        self,
648        source: str,
649        name: t.Optional[str] = None,
650        filename: t.Optional[str] = None,
651    ) -> str:
652        """Preprocesses the source with all extensions.  This is automatically
653        called for all parsing and compiling methods but *not* for :meth:`lex`
654        because there you usually only want the actual source tokenized.
655        """
656        return reduce(
657            lambda s, e: e.preprocess(s, name, filename),
658            self.iter_extensions(),
659            str(source),
660        )

Preprocesses the source with all extensions. This is automatically called for all parsing and compiling methods but not for lex() because there you usually only want the actual source tokenized.

@internalcode
def compile( self, source: Union[str, jinja2.nodes.Template], name: Optional[str] = None, filename: Optional[str] = None, raw: bool = False, defer_init: bool = False) -> Union[str, code]:
732    @internalcode
733    def compile(
734        self,
735        source: t.Union[str, nodes.Template],
736        name: t.Optional[str] = None,
737        filename: t.Optional[str] = None,
738        raw: bool = False,
739        defer_init: bool = False,
740    ) -> t.Union[str, CodeType]:
741        """Compile a node or template source code.  The `name` parameter is
742        the load name of the template after it was joined using
743        :meth:`join_path` if necessary, not the filename on the file system.
744        the `filename` parameter is the estimated filename of the template on
745        the file system.  If the template came from a database or memory this
746        can be omitted.
747
748        The return value of this method is a python code object.  If the `raw`
749        parameter is `True` the return value will be a string with python
750        code equivalent to the bytecode returned otherwise.  This method is
751        mainly used internally.
752
753        `defer_init` is use internally to aid the module code generator.  This
754        causes the generated code to be able to import without the global
755        environment variable to be set.
756
757        .. versionadded:: 2.4
758           `defer_init` parameter added.
759        """
760        source_hint = None
761        try:
762            if isinstance(source, str):
763                source_hint = source
764                source = self._parse(source, name, filename)
765            source = self._generate(source, name, filename, defer_init=defer_init)
766            if raw:
767                return source
768            if filename is None:
769                filename = "<template>"
770            return self._compile(source, filename)
771        except TemplateSyntaxError:
772            self.handle_exception(source=source_hint)

Compile a node or template source code. The name parameter is the load name of the template after it was joined using join_path() if necessary, not the filename on the file system. the filename parameter is the estimated filename of the template on the file system. If the template came from a database or memory this can be omitted.

The return value of this method is a python code object. If the raw parameter is True the return value will be a string with python code equivalent to the bytecode returned otherwise. This method is mainly used internally.

defer_init is use internally to aid the module code generator. This causes the generated code to be able to import without the global environment variable to be set.

New in version 2.4: defer_init parameter added.

def compile_expression( self, source: str, undefined_to_none: bool = True) -> TemplateExpression:
774    def compile_expression(
775        self, source: str, undefined_to_none: bool = True
776    ) -> "TemplateExpression":
777        """A handy helper method that returns a callable that accepts keyword
778        arguments that appear as variables in the expression.  If called it
779        returns the result of the expression.
780
781        This is useful if applications want to use the same rules as Jinja
782        in template "configuration files" or similar situations.
783
784        Example usage:
785
786        >>> env = Environment()
787        >>> expr = env.compile_expression('foo == 42')
788        >>> expr(foo=23)
789        False
790        >>> expr(foo=42)
791        True
792
793        Per default the return value is converted to `None` if the
794        expression returns an undefined value.  This can be changed
795        by setting `undefined_to_none` to `False`.
796
797        >>> env.compile_expression('var')() is None
798        True
799        >>> env.compile_expression('var', undefined_to_none=False)()
800        Undefined
801
802        .. versionadded:: 2.1
803        """
804        parser = Parser(self, source, state="variable")
805        try:
806            expr = parser.parse_expression()
807            if not parser.stream.eos:
808                raise TemplateSyntaxError(
809                    "chunk after expression", parser.stream.current.lineno, None, None
810                )
811            expr.set_environment(self)
812        except TemplateSyntaxError:
813            self.handle_exception(source=source)
814
815        body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)]
816        template = self.from_string(nodes.Template(body, lineno=1))
817        return TemplateExpression(template, undefined_to_none)

A handy helper method that returns a callable that accepts keyword arguments that appear as variables in the expression. If called it returns the result of the expression.

This is useful if applications want to use the same rules as Jinja in template "configuration files" or similar situations.

Example usage:

>>> env = Environment()
>>> expr = env.compile_expression('foo == 42')
>>> expr(foo=23)
False
>>> expr(foo=42)
True

Per default the return value is converted to None if the expression returns an undefined value. This can be changed by setting undefined_to_none to False.

>>> env.compile_expression('var')() is None
True
>>> env.compile_expression('var', undefined_to_none=False)()
Undefined

New in version 2.1.

def compile_templates( self, target: Union[str, os.PathLike[str]], extensions: Optional[Collection[str]] = None, filter_func: Optional[Callable[[str], bool]] = None, zip: Optional[str] = 'deflated', log_function: Optional[Callable[[str], NoneType]] = None, ignore_errors: bool = True) -> None:
819    def compile_templates(
820        self,
821        target: t.Union[str, "os.PathLike[str]"],
822        extensions: t.Optional[t.Collection[str]] = None,
823        filter_func: t.Optional[t.Callable[[str], bool]] = None,
824        zip: t.Optional[str] = "deflated",
825        log_function: t.Optional[t.Callable[[str], None]] = None,
826        ignore_errors: bool = True,
827    ) -> None:
828        """Finds all the templates the loader can find, compiles them
829        and stores them in `target`.  If `zip` is `None`, instead of in a
830        zipfile, the templates will be stored in a directory.
831        By default a deflate zip algorithm is used. To switch to
832        the stored algorithm, `zip` can be set to ``'stored'``.
833
834        `extensions` and `filter_func` are passed to :meth:`list_templates`.
835        Each template returned will be compiled to the target folder or
836        zipfile.
837
838        By default template compilation errors are ignored.  In case a
839        log function is provided, errors are logged.  If you want template
840        syntax errors to abort the compilation you can set `ignore_errors`
841        to `False` and you will get an exception on syntax errors.
842
843        .. versionadded:: 2.4
844        """
845        from .loaders import ModuleLoader
846
847        if log_function is None:
848
849            def log_function(x: str) -> None:
850                pass
851
852        assert log_function is not None
853        assert self.loader is not None, "No loader configured."
854
855        def write_file(filename: str, data: str) -> None:
856            if zip:
857                info = ZipInfo(filename)
858                info.external_attr = 0o755 << 16
859                zip_file.writestr(info, data)
860            else:
861                with open(os.path.join(target, filename), "wb") as f:
862                    f.write(data.encode("utf8"))
863
864        if zip is not None:
865            from zipfile import ZIP_DEFLATED
866            from zipfile import ZIP_STORED
867            from zipfile import ZipFile
868            from zipfile import ZipInfo
869
870            zip_file = ZipFile(
871                target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip]
872            )
873            log_function(f"Compiling into Zip archive {target!r}")
874        else:
875            if not os.path.isdir(target):
876                os.makedirs(target)
877            log_function(f"Compiling into folder {target!r}")
878
879        try:
880            for name in self.list_templates(extensions, filter_func):
881                source, filename, _ = self.loader.get_source(self, name)
882                try:
883                    code = self.compile(source, name, filename, True, True)
884                except TemplateSyntaxError as e:
885                    if not ignore_errors:
886                        raise
887                    log_function(f'Could not compile "{name}": {e}')
888                    continue
889
890                filename = ModuleLoader.get_module_filename(name)
891
892                write_file(filename, code)
893                log_function(f'Compiled "{name}" as {filename}')
894        finally:
895            if zip:
896                zip_file.close()
897
898        log_function("Finished compiling templates")

Finds all the templates the loader can find, compiles them and stores them in target. If zip is None, instead of in a zipfile, the templates will be stored in a directory. By default a deflate zip algorithm is used. To switch to the stored algorithm, zip can be set to 'stored'.

extensions and filter_func are passed to list_templates(). Each template returned will be compiled to the target folder or zipfile.

By default template compilation errors are ignored. In case a log function is provided, errors are logged. If you want template syntax errors to abort the compilation you can set ignore_errors to False and you will get an exception on syntax errors.

New in version 2.4.

def list_templates( self, extensions: Optional[Collection[str]] = None, filter_func: Optional[Callable[[str], bool]] = None) -> List[str]:
900    def list_templates(
901        self,
902        extensions: t.Optional[t.Collection[str]] = None,
903        filter_func: t.Optional[t.Callable[[str], bool]] = None,
904    ) -> t.List[str]:
905        """Returns a list of templates for this environment.  This requires
906        that the loader supports the loader's
907        :meth:`~BaseLoader.list_templates` method.
908
909        If there are other files in the template folder besides the
910        actual templates, the returned list can be filtered.  There are two
911        ways: either `extensions` is set to a list of file extensions for
912        templates, or a `filter_func` can be provided which is a callable that
913        is passed a template name and should return `True` if it should end up
914        in the result list.
915
916        If the loader does not support that, a :exc:`TypeError` is raised.
917
918        .. versionadded:: 2.4
919        """
920        assert self.loader is not None, "No loader configured."
921        names = self.loader.list_templates()
922
923        if extensions is not None:
924            if filter_func is not None:
925                raise TypeError(
926                    "either extensions or filter_func can be passed, but not both"
927                )
928
929            def filter_func(x: str) -> bool:
930                return "." in x and x.rsplit(".", 1)[1] in extensions
931
932        if filter_func is not None:
933            names = [name for name in names if filter_func(name)]
934
935        return names

Returns a list of templates for this environment. This requires that the loader supports the loader's ~BaseLoader.list_templates() method.

If there are other files in the template folder besides the actual templates, the returned list can be filtered. There are two ways: either extensions is set to a list of file extensions for templates, or a filter_func can be provided which is a callable that is passed a template name and should return True if it should end up in the result list.

If the loader does not support that, a TypeError is raised.

New in version 2.4.

def handle_exception(self, source: Optional[str] = None) -> NoReturn:
937    def handle_exception(self, source: t.Optional[str] = None) -> "te.NoReturn":
938        """Exception handling helper.  This is used internally to either raise
939        rewritten exceptions or return a rendered traceback for the template.
940        """
941        from .debug import rewrite_traceback_stack
942
943        raise rewrite_traceback_stack(source=source)

Exception handling helper. This is used internally to either raise rewritten exceptions or return a rendered traceback for the template.

def join_path(self, template: str, parent: str) -> str:
945    def join_path(self, template: str, parent: str) -> str:
946        """Join a template with the parent.  By default all the lookups are
947        relative to the loader root so this method returns the `template`
948        parameter unchanged, but if the paths should be relative to the
949        parent template, this function can be used to calculate the real
950        template name.
951
952        Subclasses may override this method and implement template path
953        joining here.
954        """
955        return template

Join a template with the parent. By default all the lookups are relative to the loader root so this method returns the template parameter unchanged, but if the paths should be relative to the parent template, this function can be used to calculate the real template name.

Subclasses may override this method and implement template path joining here.

@internalcode
def get_template( self, name: Union[str, Template], parent: Optional[str] = None, globals: Optional[MutableMapping[str, Any]] = None) -> Template:
 982    @internalcode
 983    def get_template(
 984        self,
 985        name: t.Union[str, "Template"],
 986        parent: t.Optional[str] = None,
 987        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
 988    ) -> "Template":
 989        """Load a template by name with :attr:`loader` and return a
 990        :class:`Template`. If the template does not exist a
 991        :exc:`TemplateNotFound` exception is raised.
 992
 993        :param name: Name of the template to load. When loading
 994            templates from the filesystem, "/" is used as the path
 995            separator, even on Windows.
 996        :param parent: The name of the parent template importing this
 997            template. :meth:`join_path` can be used to implement name
 998            transformations with this.
 999        :param globals: Extend the environment :attr:`globals` with
1000            these extra variables available for all renders of this
1001            template. If the template has already been loaded and
1002            cached, its globals are updated with any new items.
1003
1004        .. versionchanged:: 3.0
1005            If a template is loaded from cache, ``globals`` will update
1006            the template's globals instead of ignoring the new values.
1007
1008        .. versionchanged:: 2.4
1009            If ``name`` is a :class:`Template` object it is returned
1010            unchanged.
1011        """
1012        if isinstance(name, Template):
1013            return name
1014        if parent is not None:
1015            name = self.join_path(name, parent)
1016
1017        return self._load_template(name, globals)

Load a template by name with loader and return a Template. If the template does not exist a TemplateNotFound exception is raised.

Parameters
  • name: Name of the template to load. When loading templates from the filesystem, "/" is used as the path separator, even on Windows.
  • parent: The name of the parent template importing this template. join_path() can be used to implement name transformations with this.
  • globals: Extend the environment globals with these extra variables available for all renders of this template. If the template has already been loaded and cached, its globals are updated with any new items.

Changed in version 3.0: If a template is loaded from cache, globals will update the template's globals instead of ignoring the new values.

Changed in version 2.4: If name is a Template object it is returned unchanged.

@internalcode
def select_template( self, names: Iterable[Union[str, Template]], parent: Optional[str] = None, globals: Optional[MutableMapping[str, Any]] = None) -> Template:
1019    @internalcode
1020    def select_template(
1021        self,
1022        names: t.Iterable[t.Union[str, "Template"]],
1023        parent: t.Optional[str] = None,
1024        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1025    ) -> "Template":
1026        """Like :meth:`get_template`, but tries loading multiple names.
1027        If none of the names can be loaded a :exc:`TemplatesNotFound`
1028        exception is raised.
1029
1030        :param names: List of template names to try loading in order.
1031        :param parent: The name of the parent template importing this
1032            template. :meth:`join_path` can be used to implement name
1033            transformations with this.
1034        :param globals: Extend the environment :attr:`globals` with
1035            these extra variables available for all renders of this
1036            template. If the template has already been loaded and
1037            cached, its globals are updated with any new items.
1038
1039        .. versionchanged:: 3.0
1040            If a template is loaded from cache, ``globals`` will update
1041            the template's globals instead of ignoring the new values.
1042
1043        .. versionchanged:: 2.11
1044            If ``names`` is :class:`Undefined`, an :exc:`UndefinedError`
1045            is raised instead. If no templates were found and ``names``
1046            contains :class:`Undefined`, the message is more helpful.
1047
1048        .. versionchanged:: 2.4
1049            If ``names`` contains a :class:`Template` object it is
1050            returned unchanged.
1051
1052        .. versionadded:: 2.3
1053        """
1054        if isinstance(names, Undefined):
1055            names._fail_with_undefined_error()
1056
1057        if not names:
1058            raise TemplatesNotFound(
1059                message="Tried to select from an empty list of templates."
1060            )
1061
1062        for name in names:
1063            if isinstance(name, Template):
1064                return name
1065            if parent is not None:
1066                name = self.join_path(name, parent)
1067            try:
1068                return self._load_template(name, globals)
1069            except (TemplateNotFound, UndefinedError):
1070                pass
1071        raise TemplatesNotFound(names)  # type: ignore

Like get_template(), but tries loading multiple names. If none of the names can be loaded a TemplatesNotFound exception is raised.

Parameters
  • names: List of template names to try loading in order.
  • parent: The name of the parent template importing this template. join_path() can be used to implement name transformations with this.
  • globals: Extend the environment globals with these extra variables available for all renders of this template. If the template has already been loaded and cached, its globals are updated with any new items.

Changed in version 3.0: If a template is loaded from cache, globals will update the template's globals instead of ignoring the new values.

Changed in version 2.11: If names is Undefined, an UndefinedError is raised instead. If no templates were found and names contains Undefined, the message is more helpful.

Changed in version 2.4: If names contains a Template object it is returned unchanged.

New in version 2.3.

@internalcode
def get_or_select_template( self, template_name_or_list: Union[str, Template, List[Union[str, Template]]], parent: Optional[str] = None, globals: Optional[MutableMapping[str, Any]] = None) -> Template:
1073    @internalcode
1074    def get_or_select_template(
1075        self,
1076        template_name_or_list: t.Union[
1077            str, "Template", t.List[t.Union[str, "Template"]]
1078        ],
1079        parent: t.Optional[str] = None,
1080        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1081    ) -> "Template":
1082        """Use :meth:`select_template` if an iterable of template names
1083        is given, or :meth:`get_template` if one name is given.
1084
1085        .. versionadded:: 2.3
1086        """
1087        if isinstance(template_name_or_list, (str, Undefined)):
1088            return self.get_template(template_name_or_list, parent, globals)
1089        elif isinstance(template_name_or_list, Template):
1090            return template_name_or_list
1091        return self.select_template(template_name_or_list, parent, globals)

Use select_template() if an iterable of template names is given, or get_template() if one name is given.

New in version 2.3.

def from_string( self, source: Union[str, jinja2.nodes.Template], globals: Optional[MutableMapping[str, Any]] = None, template_class: Optional[Type[Template]] = None) -> Template:
1093    def from_string(
1094        self,
1095        source: t.Union[str, nodes.Template],
1096        globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
1097        template_class: t.Optional[t.Type["Template"]] = None,
1098    ) -> "Template":
1099        """Load a template from a source string without using
1100        :attr:`loader`.
1101
1102        :param source: Jinja source to compile into a template.
1103        :param globals: Extend the environment :attr:`globals` with
1104            these extra variables available for all renders of this
1105            template. If the template has already been loaded and
1106            cached, its globals are updated with any new items.
1107        :param template_class: Return an instance of this
1108            :class:`Template` class.
1109        """
1110        gs = self.make_globals(globals)
1111        cls = template_class or self.template_class
1112        return cls.from_code(self, self.compile(source), gs, None)

Load a template from a source string without using loader.

Parameters
  • source: Jinja source to compile into a template.
  • globals: Extend the environment globals with these extra variables available for all renders of this template. If the template has already been loaded and cached, its globals are updated with any new items.
  • template_class: Return an instance of this Template class.
def make_globals(self, d: Optional[MutableMapping[str, Any]]) -> MutableMapping[str, Any]:
1114    def make_globals(
1115        self, d: t.Optional[t.MutableMapping[str, t.Any]]
1116    ) -> t.MutableMapping[str, t.Any]:
1117        """Make the globals map for a template. Any given template
1118        globals overlay the environment :attr:`globals`.
1119
1120        Returns a :class:`collections.ChainMap`. This allows any changes
1121        to a template's globals to only affect that template, while
1122        changes to the environment's globals are still reflected.
1123        However, avoid modifying any globals after a template is loaded.
1124
1125        :param d: Dict of template-specific globals.
1126
1127        .. versionchanged:: 3.0
1128            Use :class:`collections.ChainMap` to always prevent mutating
1129            environment globals.
1130        """
1131        if d is None:
1132            d = {}
1133
1134        return ChainMap(d, self.globals)

Make the globals map for a template. Any given template globals overlay the environment globals.

Returns a collections.ChainMap. This allows any changes to a template's globals to only affect that template, while changes to the environment's globals are still reflected. However, avoid modifying any globals after a template is loaded.

Parameters
  • d: Dict of template-specific globals.

Changed in version 3.0: Use collections.ChainMap to always prevent mutating environment globals.

class Template:
1137class Template:
1138    """A compiled template that can be rendered.
1139
1140    Use the methods on :class:`Environment` to create or load templates.
1141    The environment is used to configure how templates are compiled and
1142    behave.
1143
1144    It is also possible to create a template object directly. This is
1145    not usually recommended. The constructor takes most of the same
1146    arguments as :class:`Environment`. All templates created with the
1147    same environment arguments share the same ephemeral ``Environment``
1148    instance behind the scenes.
1149
1150    A template object should be considered immutable. Modifications on
1151    the object are not supported.
1152    """
1153
1154    #: Type of environment to create when creating a template directly
1155    #: rather than through an existing environment.
1156    environment_class: t.Type[Environment] = Environment
1157
1158    environment: Environment
1159    globals: t.MutableMapping[str, t.Any]
1160    name: t.Optional[str]
1161    filename: t.Optional[str]
1162    blocks: t.Dict[str, t.Callable[[Context], t.Iterator[str]]]
1163    root_render_func: t.Callable[[Context], t.Iterator[str]]
1164    _module: t.Optional["TemplateModule"]
1165    _debug_info: str
1166    _uptodate: t.Optional[t.Callable[[], bool]]
1167
1168    def __new__(
1169        cls,
1170        source: t.Union[str, nodes.Template],
1171        block_start_string: str = BLOCK_START_STRING,
1172        block_end_string: str = BLOCK_END_STRING,
1173        variable_start_string: str = VARIABLE_START_STRING,
1174        variable_end_string: str = VARIABLE_END_STRING,
1175        comment_start_string: str = COMMENT_START_STRING,
1176        comment_end_string: str = COMMENT_END_STRING,
1177        line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
1178        line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
1179        trim_blocks: bool = TRIM_BLOCKS,
1180        lstrip_blocks: bool = LSTRIP_BLOCKS,
1181        newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
1182        keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
1183        extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
1184        optimized: bool = True,
1185        undefined: t.Type[Undefined] = Undefined,
1186        finalize: t.Optional[t.Callable[..., t.Any]] = None,
1187        autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
1188        enable_async: bool = False,
1189    ) -> t.Any:  # it returns a `Template`, but this breaks the sphinx build...
1190        env = get_spontaneous_environment(
1191            cls.environment_class,  # type: ignore
1192            block_start_string,
1193            block_end_string,
1194            variable_start_string,
1195            variable_end_string,
1196            comment_start_string,
1197            comment_end_string,
1198            line_statement_prefix,
1199            line_comment_prefix,
1200            trim_blocks,
1201            lstrip_blocks,
1202            newline_sequence,
1203            keep_trailing_newline,
1204            frozenset(extensions),
1205            optimized,
1206            undefined,  # type: ignore
1207            finalize,
1208            autoescape,
1209            None,
1210            0,
1211            False,
1212            None,
1213            enable_async,
1214        )
1215        return env.from_string(source, template_class=cls)
1216
1217    @classmethod
1218    def from_code(
1219        cls,
1220        environment: Environment,
1221        code: CodeType,
1222        globals: t.MutableMapping[str, t.Any],
1223        uptodate: t.Optional[t.Callable[[], bool]] = None,
1224    ) -> "Template":
1225        """Creates a template object from compiled code and the globals.  This
1226        is used by the loaders and environment to create a template object.
1227        """
1228        namespace = {"environment": environment, "__file__": code.co_filename}
1229        exec(code, namespace)
1230        rv = cls._from_namespace(environment, namespace, globals)
1231        rv._uptodate = uptodate
1232        return rv
1233
1234    @classmethod
1235    def from_module_dict(
1236        cls,
1237        environment: Environment,
1238        module_dict: t.MutableMapping[str, t.Any],
1239        globals: t.MutableMapping[str, t.Any],
1240    ) -> "Template":
1241        """Creates a template object from a module.  This is used by the
1242        module loader to create a template object.
1243
1244        .. versionadded:: 2.4
1245        """
1246        return cls._from_namespace(environment, module_dict, globals)
1247
1248    @classmethod
1249    def _from_namespace(
1250        cls,
1251        environment: Environment,
1252        namespace: t.MutableMapping[str, t.Any],
1253        globals: t.MutableMapping[str, t.Any],
1254    ) -> "Template":
1255        t: Template = object.__new__(cls)
1256        t.environment = environment
1257        t.globals = globals
1258        t.name = namespace["name"]
1259        t.filename = namespace["__file__"]
1260        t.blocks = namespace["blocks"]
1261
1262        # render function and module
1263        t.root_render_func = namespace["root"]
1264        t._module = None
1265
1266        # debug and loader helpers
1267        t._debug_info = namespace["debug_info"]
1268        t._uptodate = None
1269
1270        # store the reference
1271        namespace["environment"] = environment
1272        namespace["__jinja_template__"] = t
1273
1274        return t
1275
1276    def render(self, *args: t.Any, **kwargs: t.Any) -> str:
1277        """This method accepts the same arguments as the `dict` constructor:
1278        A dict, a dict subclass or some keyword arguments.  If no arguments
1279        are given the context will be empty.  These two calls do the same::
1280
1281            template.render(knights='that say nih')
1282            template.render({'knights': 'that say nih'})
1283
1284        This will return the rendered template as a string.
1285        """
1286        if self.environment.is_async:
1287            import asyncio
1288
1289            return asyncio.run(self.render_async(*args, **kwargs))
1290
1291        ctx = self.new_context(dict(*args, **kwargs))
1292
1293        try:
1294            return self.environment.concat(self.root_render_func(ctx))  # type: ignore
1295        except Exception:
1296            self.environment.handle_exception()
1297
1298    async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:
1299        """This works similar to :meth:`render` but returns a coroutine
1300        that when awaited returns the entire rendered template string.  This
1301        requires the async feature to be enabled.
1302
1303        Example usage::
1304
1305            await template.render_async(knights='that say nih; asynchronously')
1306        """
1307        if not self.environment.is_async:
1308            raise RuntimeError(
1309                "The environment was not created with async mode enabled."
1310            )
1311
1312        ctx = self.new_context(dict(*args, **kwargs))
1313
1314        try:
1315            return self.environment.concat(  # type: ignore
1316                [n async for n in self.root_render_func(ctx)]  # type: ignore
1317            )
1318        except Exception:
1319            return self.environment.handle_exception()
1320
1321    def stream(self, *args: t.Any, **kwargs: t.Any) -> "TemplateStream":
1322        """Works exactly like :meth:`generate` but returns a
1323        :class:`TemplateStream`.
1324        """
1325        return TemplateStream(self.generate(*args, **kwargs))
1326
1327    def generate(self, *args: t.Any, **kwargs: t.Any) -> t.Iterator[str]:
1328        """For very large templates it can be useful to not render the whole
1329        template at once but evaluate each statement after another and yield
1330        piece for piece.  This method basically does exactly that and returns
1331        a generator that yields one item after another as strings.
1332
1333        It accepts the same arguments as :meth:`render`.
1334        """
1335        if self.environment.is_async:
1336            import asyncio
1337
1338            async def to_list() -> t.List[str]:
1339                return [x async for x in self.generate_async(*args, **kwargs)]
1340
1341            yield from asyncio.run(to_list())
1342            return
1343
1344        ctx = self.new_context(dict(*args, **kwargs))
1345
1346        try:
1347            yield from self.root_render_func(ctx)
1348        except Exception:
1349            yield self.environment.handle_exception()
1350
1351    async def generate_async(
1352        self, *args: t.Any, **kwargs: t.Any
1353    ) -> t.AsyncGenerator[str, object]:
1354        """An async version of :meth:`generate`.  Works very similarly but
1355        returns an async iterator instead.
1356        """
1357        if not self.environment.is_async:
1358            raise RuntimeError(
1359                "The environment was not created with async mode enabled."
1360            )
1361
1362        ctx = self.new_context(dict(*args, **kwargs))
1363
1364        try:
1365            agen = self.root_render_func(ctx)
1366            try:
1367                async for event in agen:  # type: ignore
1368                    yield event
1369            finally:
1370                # we can't use async with aclosing(...) because that's only
1371                # in 3.10+
1372                await agen.aclose()  # type: ignore
1373        except Exception:
1374            yield self.environment.handle_exception()
1375
1376    def new_context(
1377        self,
1378        vars: t.Optional[t.Dict[str, t.Any]] = None,
1379        shared: bool = False,
1380        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1381    ) -> Context:
1382        """Create a new :class:`Context` for this template.  The vars
1383        provided will be passed to the template.  Per default the globals
1384        are added to the context.  If shared is set to `True` the data
1385        is passed as is to the context without adding the globals.
1386
1387        `locals` can be a dict of local variables for internal usage.
1388        """
1389        return new_context(
1390            self.environment, self.name, self.blocks, vars, shared, self.globals, locals
1391        )
1392
1393    def make_module(
1394        self,
1395        vars: t.Optional[t.Dict[str, t.Any]] = None,
1396        shared: bool = False,
1397        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1398    ) -> "TemplateModule":
1399        """This method works like the :attr:`module` attribute when called
1400        without arguments but it will evaluate the template on every call
1401        rather than caching it.  It's also possible to provide
1402        a dict which is then used as context.  The arguments are the same
1403        as for the :meth:`new_context` method.
1404        """
1405        ctx = self.new_context(vars, shared, locals)
1406        return TemplateModule(self, ctx)
1407
1408    async def make_module_async(
1409        self,
1410        vars: t.Optional[t.Dict[str, t.Any]] = None,
1411        shared: bool = False,
1412        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1413    ) -> "TemplateModule":
1414        """As template module creation can invoke template code for
1415        asynchronous executions this method must be used instead of the
1416        normal :meth:`make_module` one.  Likewise the module attribute
1417        becomes unavailable in async mode.
1418        """
1419        ctx = self.new_context(vars, shared, locals)
1420        return TemplateModule(
1421            self,
1422            ctx,
1423            [x async for x in self.root_render_func(ctx)],  # type: ignore
1424        )
1425
1426    @internalcode
1427    def _get_default_module(self, ctx: t.Optional[Context] = None) -> "TemplateModule":
1428        """If a context is passed in, this means that the template was
1429        imported. Imported templates have access to the current
1430        template's globals by default, but they can only be accessed via
1431        the context during runtime.
1432
1433        If there are new globals, we need to create a new module because
1434        the cached module is already rendered and will not have access
1435        to globals from the current context. This new module is not
1436        cached because the template can be imported elsewhere, and it
1437        should have access to only the current template's globals.
1438        """
1439        if self.environment.is_async:
1440            raise RuntimeError("Module is not available in async mode.")
1441
1442        if ctx is not None:
1443            keys = ctx.globals_keys - self.globals.keys()
1444
1445            if keys:
1446                return self.make_module({k: ctx.parent[k] for k in keys})
1447
1448        if self._module is None:
1449            self._module = self.make_module()
1450
1451        return self._module
1452
1453    async def _get_default_module_async(
1454        self, ctx: t.Optional[Context] = None
1455    ) -> "TemplateModule":
1456        if ctx is not None:
1457            keys = ctx.globals_keys - self.globals.keys()
1458
1459            if keys:
1460                return await self.make_module_async({k: ctx.parent[k] for k in keys})
1461
1462        if self._module is None:
1463            self._module = await self.make_module_async()
1464
1465        return self._module
1466
1467    @property
1468    def module(self) -> "TemplateModule":
1469        """The template as module.  This is used for imports in the
1470        template runtime but is also useful if one wants to access
1471        exported template variables from the Python layer:
1472
1473        >>> t = Template('{% macro foo() %}42{% endmacro %}23')
1474        >>> str(t.module)
1475        '23'
1476        >>> t.module.foo() == u'42'
1477        True
1478
1479        This attribute is not available if async mode is enabled.
1480        """
1481        return self._get_default_module()
1482
1483    def get_corresponding_lineno(self, lineno: int) -> int:
1484        """Return the source line number of a line number in the
1485        generated bytecode as they are not in sync.
1486        """
1487        for template_line, code_line in reversed(self.debug_info):
1488            if code_line <= lineno:
1489                return template_line
1490        return 1
1491
1492    @property
1493    def is_up_to_date(self) -> bool:
1494        """If this variable is `False` there is a newer version available."""
1495        if self._uptodate is None:
1496            return True
1497        return self._uptodate()
1498
1499    @property
1500    def debug_info(self) -> t.List[t.Tuple[int, int]]:
1501        """The debug info mapping."""
1502        if self._debug_info:
1503            return [
1504                tuple(map(int, x.split("=")))  # type: ignore
1505                for x in self._debug_info.split("&")
1506            ]
1507
1508        return []
1509
1510    def __repr__(self) -> str:
1511        if self.name is None:
1512            name = f"memory:{id(self):x}"
1513        else:
1514            name = repr(self.name)
1515        return f"<{type(self).__name__} {name}>"

A compiled template that can be rendered.

Use the methods on Environment to create or load templates. The environment is used to configure how templates are compiled and behave.

It is also possible to create a template object directly. This is not usually recommended. The constructor takes most of the same arguments as Environment. All templates created with the same environment arguments share the same ephemeral Environment instance behind the scenes.

A template object should be considered immutable. Modifications on the object are not supported.

environment_class: Type[Environment] = <class 'Environment'>
environment: Environment
globals: MutableMapping[str, Any]
name: Optional[str]
filename: Optional[str]
blocks: Dict[str, Callable[[jinja2.runtime.Context], Iterator[str]]]
root_render_func: Callable[[jinja2.runtime.Context], Iterator[str]]
@classmethod
def from_code( cls, environment: Environment, code: code, globals: MutableMapping[str, Any], uptodate: Optional[Callable[[], bool]] = None) -> Template:
1217    @classmethod
1218    def from_code(
1219        cls,
1220        environment: Environment,
1221        code: CodeType,
1222        globals: t.MutableMapping[str, t.Any],
1223        uptodate: t.Optional[t.Callable[[], bool]] = None,
1224    ) -> "Template":
1225        """Creates a template object from compiled code and the globals.  This
1226        is used by the loaders and environment to create a template object.
1227        """
1228        namespace = {"environment": environment, "__file__": code.co_filename}
1229        exec(code, namespace)
1230        rv = cls._from_namespace(environment, namespace, globals)
1231        rv._uptodate = uptodate
1232        return rv

Creates a template object from compiled code and the globals. This is used by the loaders and environment to create a template object.

@classmethod
def from_module_dict( cls, environment: Environment, module_dict: MutableMapping[str, Any], globals: MutableMapping[str, Any]) -> Template:
1234    @classmethod
1235    def from_module_dict(
1236        cls,
1237        environment: Environment,
1238        module_dict: t.MutableMapping[str, t.Any],
1239        globals: t.MutableMapping[str, t.Any],
1240    ) -> "Template":
1241        """Creates a template object from a module.  This is used by the
1242        module loader to create a template object.
1243
1244        .. versionadded:: 2.4
1245        """
1246        return cls._from_namespace(environment, module_dict, globals)

Creates a template object from a module. This is used by the module loader to create a template object.

New in version 2.4.

def render(self, *args: Any, **kwargs: Any) -> str:
1276    def render(self, *args: t.Any, **kwargs: t.Any) -> str:
1277        """This method accepts the same arguments as the `dict` constructor:
1278        A dict, a dict subclass or some keyword arguments.  If no arguments
1279        are given the context will be empty.  These two calls do the same::
1280
1281            template.render(knights='that say nih')
1282            template.render({'knights': 'that say nih'})
1283
1284        This will return the rendered template as a string.
1285        """
1286        if self.environment.is_async:
1287            import asyncio
1288
1289            return asyncio.run(self.render_async(*args, **kwargs))
1290
1291        ctx = self.new_context(dict(*args, **kwargs))
1292
1293        try:
1294            return self.environment.concat(self.root_render_func(ctx))  # type: ignore
1295        except Exception:
1296            self.environment.handle_exception()

This method accepts the same arguments as the dict constructor: A dict, a dict subclass or some keyword arguments. If no arguments are given the context will be empty. These two calls do the same::

template.render(knights='that say nih')
template.render({'knights': 'that say nih'})

This will return the rendered template as a string.

async def render_async(self, *args: Any, **kwargs: Any) -> str:
1298    async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:
1299        """This works similar to :meth:`render` but returns a coroutine
1300        that when awaited returns the entire rendered template string.  This
1301        requires the async feature to be enabled.
1302
1303        Example usage::
1304
1305            await template.render_async(knights='that say nih; asynchronously')
1306        """
1307        if not self.environment.is_async:
1308            raise RuntimeError(
1309                "The environment was not created with async mode enabled."
1310            )
1311
1312        ctx = self.new_context(dict(*args, **kwargs))
1313
1314        try:
1315            return self.environment.concat(  # type: ignore
1316                [n async for n in self.root_render_func(ctx)]  # type: ignore
1317            )
1318        except Exception:
1319            return self.environment.handle_exception()

This works similar to render() but returns a coroutine that when awaited returns the entire rendered template string. This requires the async feature to be enabled.

Example usage::

await template.render_async(knights='that say nih; asynchronously')
def stream(self, *args: Any, **kwargs: Any) -> TemplateStream:
1321    def stream(self, *args: t.Any, **kwargs: t.Any) -> "TemplateStream":
1322        """Works exactly like :meth:`generate` but returns a
1323        :class:`TemplateStream`.
1324        """
1325        return TemplateStream(self.generate(*args, **kwargs))

Works exactly like generate() but returns a TemplateStream.

def generate(self, *args: Any, **kwargs: Any) -> Iterator[str]:
1327    def generate(self, *args: t.Any, **kwargs: t.Any) -> t.Iterator[str]:
1328        """For very large templates it can be useful to not render the whole
1329        template at once but evaluate each statement after another and yield
1330        piece for piece.  This method basically does exactly that and returns
1331        a generator that yields one item after another as strings.
1332
1333        It accepts the same arguments as :meth:`render`.
1334        """
1335        if self.environment.is_async:
1336            import asyncio
1337
1338            async def to_list() -> t.List[str]:
1339                return [x async for x in self.generate_async(*args, **kwargs)]
1340
1341            yield from asyncio.run(to_list())
1342            return
1343
1344        ctx = self.new_context(dict(*args, **kwargs))
1345
1346        try:
1347            yield from self.root_render_func(ctx)
1348        except Exception:
1349            yield self.environment.handle_exception()

For very large templates it can be useful to not render the whole template at once but evaluate each statement after another and yield piece for piece. This method basically does exactly that and returns a generator that yields one item after another as strings.

It accepts the same arguments as render().

async def generate_async(self, *args: Any, **kwargs: Any) -> AsyncGenerator[str, object]:
1351    async def generate_async(
1352        self, *args: t.Any, **kwargs: t.Any
1353    ) -> t.AsyncGenerator[str, object]:
1354        """An async version of :meth:`generate`.  Works very similarly but
1355        returns an async iterator instead.
1356        """
1357        if not self.environment.is_async:
1358            raise RuntimeError(
1359                "The environment was not created with async mode enabled."
1360            )
1361
1362        ctx = self.new_context(dict(*args, **kwargs))
1363
1364        try:
1365            agen = self.root_render_func(ctx)
1366            try:
1367                async for event in agen:  # type: ignore
1368                    yield event
1369            finally:
1370                # we can't use async with aclosing(...) because that's only
1371                # in 3.10+
1372                await agen.aclose()  # type: ignore
1373        except Exception:
1374            yield self.environment.handle_exception()

An async version of generate(). Works very similarly but returns an async iterator instead.

def new_context( self, vars: Optional[Dict[str, Any]] = None, shared: bool = False, locals: Optional[Mapping[str, Any]] = None) -> jinja2.runtime.Context:
1376    def new_context(
1377        self,
1378        vars: t.Optional[t.Dict[str, t.Any]] = None,
1379        shared: bool = False,
1380        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1381    ) -> Context:
1382        """Create a new :class:`Context` for this template.  The vars
1383        provided will be passed to the template.  Per default the globals
1384        are added to the context.  If shared is set to `True` the data
1385        is passed as is to the context without adding the globals.
1386
1387        `locals` can be a dict of local variables for internal usage.
1388        """
1389        return new_context(
1390            self.environment, self.name, self.blocks, vars, shared, self.globals, locals
1391        )

Create a new Context for this template. The vars provided will be passed to the template. Per default the globals are added to the context. If shared is set to True the data is passed as is to the context without adding the globals.

locals can be a dict of local variables for internal usage.

def make_module( self, vars: Optional[Dict[str, Any]] = None, shared: bool = False, locals: Optional[Mapping[str, Any]] = None) -> TemplateModule:
1393    def make_module(
1394        self,
1395        vars: t.Optional[t.Dict[str, t.Any]] = None,
1396        shared: bool = False,
1397        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1398    ) -> "TemplateModule":
1399        """This method works like the :attr:`module` attribute when called
1400        without arguments but it will evaluate the template on every call
1401        rather than caching it.  It's also possible to provide
1402        a dict which is then used as context.  The arguments are the same
1403        as for the :meth:`new_context` method.
1404        """
1405        ctx = self.new_context(vars, shared, locals)
1406        return TemplateModule(self, ctx)

This method works like the module attribute when called without arguments but it will evaluate the template on every call rather than caching it. It's also possible to provide a dict which is then used as context. The arguments are the same as for the new_context() method.

async def make_module_async( self, vars: Optional[Dict[str, Any]] = None, shared: bool = False, locals: Optional[Mapping[str, Any]] = None) -> TemplateModule:
1408    async def make_module_async(
1409        self,
1410        vars: t.Optional[t.Dict[str, t.Any]] = None,
1411        shared: bool = False,
1412        locals: t.Optional[t.Mapping[str, t.Any]] = None,
1413    ) -> "TemplateModule":
1414        """As template module creation can invoke template code for
1415        asynchronous executions this method must be used instead of the
1416        normal :meth:`make_module` one.  Likewise the module attribute
1417        becomes unavailable in async mode.
1418        """
1419        ctx = self.new_context(vars, shared, locals)
1420        return TemplateModule(
1421            self,
1422            ctx,
1423            [x async for x in self.root_render_func(ctx)],  # type: ignore
1424        )

As template module creation can invoke template code for asynchronous executions this method must be used instead of the normal make_module() one. Likewise the module attribute becomes unavailable in async mode.

module: TemplateModule
1467    @property
1468    def module(self) -> "TemplateModule":
1469        """The template as module.  This is used for imports in the
1470        template runtime but is also useful if one wants to access
1471        exported template variables from the Python layer:
1472
1473        >>> t = Template('{% macro foo() %}42{% endmacro %}23')
1474        >>> str(t.module)
1475        '23'
1476        >>> t.module.foo() == u'42'
1477        True
1478
1479        This attribute is not available if async mode is enabled.
1480        """
1481        return self._get_default_module()

The template as module. This is used for imports in the template runtime but is also useful if one wants to access exported template variables from the Python layer:

>>> t = Template('{% macro foo() %}42{% endmacro %}23')
>>> str(t.module)
'23'
>>> t.module.foo() == u'42'
True

This attribute is not available if async mode is enabled.

def get_corresponding_lineno(self, lineno: int) -> int:
1483    def get_corresponding_lineno(self, lineno: int) -> int:
1484        """Return the source line number of a line number in the
1485        generated bytecode as they are not in sync.
1486        """
1487        for template_line, code_line in reversed(self.debug_info):
1488            if code_line <= lineno:
1489                return template_line
1490        return 1

Return the source line number of a line number in the generated bytecode as they are not in sync.

is_up_to_date: bool
1492    @property
1493    def is_up_to_date(self) -> bool:
1494        """If this variable is `False` there is a newer version available."""
1495        if self._uptodate is None:
1496            return True
1497        return self._uptodate()

If this variable is False there is a newer version available.

debug_info: List[Tuple[int, int]]
1499    @property
1500    def debug_info(self) -> t.List[t.Tuple[int, int]]:
1501        """The debug info mapping."""
1502        if self._debug_info:
1503            return [
1504                tuple(map(int, x.split("=")))  # type: ignore
1505                for x in self._debug_info.split("&")
1506            ]
1507
1508        return []

The debug info mapping.

class TemplateModule:
1518class TemplateModule:
1519    """Represents an imported template.  All the exported names of the
1520    template are available as attributes on this object.  Additionally
1521    converting it into a string renders the contents.
1522    """
1523
1524    def __init__(
1525        self,
1526        template: Template,
1527        context: Context,
1528        body_stream: t.Optional[t.Iterable[str]] = None,
1529    ) -> None:
1530        if body_stream is None:
1531            if context.environment.is_async:
1532                raise RuntimeError(
1533                    "Async mode requires a body stream to be passed to"
1534                    " a template module. Use the async methods of the"
1535                    " API you are using."
1536                )
1537
1538            body_stream = list(template.root_render_func(context))
1539
1540        self._body_stream = body_stream
1541        self.__dict__.update(context.get_exported())
1542        self.__name__ = template.name
1543
1544    def __html__(self) -> Markup:
1545        return Markup(concat(self._body_stream))
1546
1547    def __str__(self) -> str:
1548        return concat(self._body_stream)
1549
1550    def __repr__(self) -> str:
1551        if self.__name__ is None:
1552            name = f"memory:{id(self):x}"
1553        else:
1554            name = repr(self.__name__)
1555        return f"<{type(self).__name__} {name}>"

Represents an imported template. All the exported names of the template are available as attributes on this object. Additionally converting it into a string renders the contents.

TemplateModule( template: Template, context: jinja2.runtime.Context, body_stream: Optional[Iterable[str]] = None)
1524    def __init__(
1525        self,
1526        template: Template,
1527        context: Context,
1528        body_stream: t.Optional[t.Iterable[str]] = None,
1529    ) -> None:
1530        if body_stream is None:
1531            if context.environment.is_async:
1532                raise RuntimeError(
1533                    "Async mode requires a body stream to be passed to"
1534                    " a template module. Use the async methods of the"
1535                    " API you are using."
1536                )
1537
1538            body_stream = list(template.root_render_func(context))
1539
1540        self._body_stream = body_stream
1541        self.__dict__.update(context.get_exported())
1542        self.__name__ = template.name
class TemplateExpression:
1558class TemplateExpression:
1559    """The :meth:`jinja2.Environment.compile_expression` method returns an
1560    instance of this object.  It encapsulates the expression-like access
1561    to the template with an expression it wraps.
1562    """
1563
1564    def __init__(self, template: Template, undefined_to_none: bool) -> None:
1565        self._template = template
1566        self._undefined_to_none = undefined_to_none
1567
1568    def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Optional[t.Any]:
1569        context = self._template.new_context(dict(*args, **kwargs))
1570        consume(self._template.root_render_func(context))
1571        rv = context.vars["result"]
1572        if self._undefined_to_none and isinstance(rv, Undefined):
1573            rv = None
1574        return rv

The jinja2.Environment.compile_expression() method returns an instance of this object. It encapsulates the expression-like access to the template with an expression it wraps.

TemplateExpression(template: Template, undefined_to_none: bool)
1564    def __init__(self, template: Template, undefined_to_none: bool) -> None:
1565        self._template = template
1566        self._undefined_to_none = undefined_to_none
class TemplateStream:
1577class TemplateStream:
1578    """A template stream works pretty much like an ordinary python generator
1579    but it can buffer multiple items to reduce the number of total iterations.
1580    Per default the output is unbuffered which means that for every unbuffered
1581    instruction in the template one string is yielded.
1582
1583    If buffering is enabled with a buffer size of 5, five items are combined
1584    into a new string.  This is mainly useful if you are streaming
1585    big templates to a client via WSGI which flushes after each iteration.
1586    """
1587
1588    def __init__(self, gen: t.Iterator[str]) -> None:
1589        self._gen = gen
1590        self.disable_buffering()
1591
1592    def dump(
1593        self,
1594        fp: t.Union[str, t.IO[bytes]],
1595        encoding: t.Optional[str] = None,
1596        errors: t.Optional[str] = "strict",
1597    ) -> None:
1598        """Dump the complete stream into a file or file-like object.
1599        Per default strings are written, if you want to encode
1600        before writing specify an `encoding`.
1601
1602        Example usage::
1603
1604            Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
1605        """
1606        close = False
1607
1608        if isinstance(fp, str):
1609            if encoding is None:
1610                encoding = "utf-8"
1611
1612            real_fp: t.IO[bytes] = open(fp, "wb")
1613            close = True
1614        else:
1615            real_fp = fp
1616
1617        try:
1618            if encoding is not None:
1619                iterable = (x.encode(encoding, errors) for x in self)  # type: ignore
1620            else:
1621                iterable = self  # type: ignore
1622
1623            if hasattr(real_fp, "writelines"):
1624                real_fp.writelines(iterable)
1625            else:
1626                for item in iterable:
1627                    real_fp.write(item)
1628        finally:
1629            if close:
1630                real_fp.close()
1631
1632    def disable_buffering(self) -> None:
1633        """Disable the output buffering."""
1634        self._next = partial(next, self._gen)
1635        self.buffered = False
1636
1637    def _buffered_generator(self, size: int) -> t.Iterator[str]:
1638        buf: t.List[str] = []
1639        c_size = 0
1640        push = buf.append
1641
1642        while True:
1643            try:
1644                while c_size < size:
1645                    c = next(self._gen)
1646                    push(c)
1647                    if c:
1648                        c_size += 1
1649            except StopIteration:
1650                if not c_size:
1651                    return
1652            yield concat(buf)
1653            del buf[:]
1654            c_size = 0
1655
1656    def enable_buffering(self, size: int = 5) -> None:
1657        """Enable buffering.  Buffer `size` items before yielding them."""
1658        if size <= 1:
1659            raise ValueError("buffer size too small")
1660
1661        self.buffered = True
1662        self._next = partial(next, self._buffered_generator(size))
1663
1664    def __iter__(self) -> "TemplateStream":
1665        return self
1666
1667    def __next__(self) -> str:
1668        return self._next()  # type: ignore

A template stream works pretty much like an ordinary python generator but it can buffer multiple items to reduce the number of total iterations. Per default the output is unbuffered which means that for every unbuffered instruction in the template one string is yielded.

If buffering is enabled with a buffer size of 5, five items are combined into a new string. This is mainly useful if you are streaming big templates to a client via WSGI which flushes after each iteration.

TemplateStream(gen: Iterator[str])
1588    def __init__(self, gen: t.Iterator[str]) -> None:
1589        self._gen = gen
1590        self.disable_buffering()
def dump( self, fp: Union[str, IO[bytes]], encoding: Optional[str] = None, errors: Optional[str] = 'strict') -> None:
1592    def dump(
1593        self,
1594        fp: t.Union[str, t.IO[bytes]],
1595        encoding: t.Optional[str] = None,
1596        errors: t.Optional[str] = "strict",
1597    ) -> None:
1598        """Dump the complete stream into a file or file-like object.
1599        Per default strings are written, if you want to encode
1600        before writing specify an `encoding`.
1601
1602        Example usage::
1603
1604            Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
1605        """
1606        close = False
1607
1608        if isinstance(fp, str):
1609            if encoding is None:
1610                encoding = "utf-8"
1611
1612            real_fp: t.IO[bytes] = open(fp, "wb")
1613            close = True
1614        else:
1615            real_fp = fp
1616
1617        try:
1618            if encoding is not None:
1619                iterable = (x.encode(encoding, errors) for x in self)  # type: ignore
1620            else:
1621                iterable = self  # type: ignore
1622
1623            if hasattr(real_fp, "writelines"):
1624                real_fp.writelines(iterable)
1625            else:
1626                for item in iterable:
1627                    real_fp.write(item)
1628        finally:
1629            if close:
1630                real_fp.close()

Dump the complete stream into a file or file-like object. Per default strings are written, if you want to encode before writing specify an encoding.

Example usage::

Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
def disable_buffering(self) -> None:
1632    def disable_buffering(self) -> None:
1633        """Disable the output buffering."""
1634        self._next = partial(next, self._gen)
1635        self.buffered = False

Disable the output buffering.

def enable_buffering(self, size: int = 5) -> None:
1656    def enable_buffering(self, size: int = 5) -> None:
1657        """Enable buffering.  Buffer `size` items before yielding them."""
1658        if size <= 1:
1659            raise ValueError("buffer size too small")
1660
1661        self.buffered = True
1662        self._next = partial(next, self._buffered_generator(size))

Enable buffering. Buffer size items before yielding them.