Skip to content
Myr

Documented autoescaping behavior and eval contexts.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent c57959d · Browse files · 6 files+104 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+104 added−7 removed

  • other:+104 added−7 removed

6 files+104 added−7 removed

modified:docs/api.rst+63 added-4 removed
351 unmodified lines
352 in this dict are the names of the blocks, and the values a list of353 blocks registered. The last item in each list is the current active354 block (latest in the inheritance chain).355356 .. attribute:: eval_ctx357358 The current :ref:`eval-context`.359360 .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs)361362363.. admonition:: Implementation
89 unmodified lines
453.. autofunction:: jinja2.environmentfilter454455.. autofunction:: jinja2.contextfilter456457.. autofunction:: jinja2.evalcontextfilter458459.. autofunction:: jinja2.environmentfunction460461.. autofunction:: jinja2.contextfunction462463.. autofunction:: jinja2.evalcontextfunction464465.. function:: escape(s)466467 Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s`468 to HTML-safe sequences. Use this if you need to display text that might
83 unmodified lines
552553Filters can also be passed the current template context or environment. This554is useful if a filter wants to return an undefined value or check the current555:attr:`~Environment.autoescape` setting. For this purpose two decorators548exist: :func:`environmentfilter` and :func:`contextfilter`.556exist: :func:`environmentfilter`, :func:`contextfilter` and557:func:`evalcontextfilter`.558559Here a small example filter that breaks a text into HTML line breaks and560paragraphs and marks the return value as safe HTML string if autoescaping is561enabled::
2 unmodified lines
564 from jinja2 import environmentfilter, Markup, escape565566 _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')567559 @environmentfilter560 def nl2br(environment, value):568 @evalcontextfilter569 def nl2br(eval_ctx, value):570 result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n')571 for p in _paragraph_re.split(escape(value)))563 if environment.autoescape:572 if eval_ctx.autoescape:573 result = Markup(result)574 return result575576Context filters work the same just that the first argument is the current577active :class:`Context` rather then the environment.578579580.. _eval-context:581582Evaluation Context583------------------584585The evaluation context (short eval context or eval ctx) is a new object586introducted in Jinja 2.4 that makes it possible to activate and deactivate587compiled features at runtime.588589Currently it is only used to enable and disable the automatic escaping but590can be used for extensions as well.591592In previous Jinja versions filters and functions were marked as593environment callables in order to check for the autoescape status from the594environment. In new versions it's encouraged to check the setting from the595evaluation context instead.596597Previous versions::598599 @environmentfilter600 def filter(env, value):601 result = do_something(value)602 if env.autoescape:603 result = Markup(result)604 return result605606In new versions you can either use a :func:`contextfilter` and access the607evaluation context from the actual context, or use a608:func:`evalcontextfilter` which directly passes the evaluation context to609the function::610611 @contextfilter612 def filter(context, value):613 result = do_something(value)614 if context.eval_ctx.autoescape:615 result = Markup(result)616 return result617618 @evalcontextfilter619 def filter(eval_ctx, value):620 result = do_something(value)621 if eval_ctx.autoescape:622 result = Markup(result)623 return result624625The evaluation context must not be modified at runtime. Modifications626must only happen with a :class:`nodes.EvalContextModifier` and627:class:`nodes.ScopedEvalContextModifier` from an extension, not on the628eval context object itself.629630.. _writing-tests:631632Custom Tests633------------
110 unmodified lines
modified:docs/extensions.rst+14 added
124 unmodified lines
125is possible to enforce a nested scope in a template. Variables can be126declared directly in the opening block of the with statement or using a127standard `set` statement directly within.128129.. _autoescape-extension:130131Autoescape Extension132--------------------133134**Import name:** `jinja2.ext.autoescape`135136.. versionadded:: 2.4137138The autoescape extension allows you to toggle the autoescape feature from139within the template. If the environment's :attr:`~Environment.autoescape`140setting is set to `False` it can be activated, if it's `True` it can be141activated. The setting overriding is scoped.142143144.. _writing-extensions:145146Writing Extensions147------------------
135 unmodified lines
modified:docs/templates.rst+21 added
1306 unmodified lines
1307 {% with %}1308 {% set foo = 42 %}1309 {{ foo }}1310 {% endwith %}131113121313Autoescape Extension1314--------------------13151316.. versionadded:: 2.413171318If the application enables the :ref:`autoescape-extension` one can1319activate and deactivate the autoescaping from within the templates.13201321Example::13221323 {% autoescape true %}1324 Autoescaping is active within this block1325 {% endautoescape %}13261327 {% autoescape false %}1328 Autoescaping is inactive within this block1329 {% endautoescape %}13301331After the `endautoescape` the behavior is reverted to what it was before.
modified:jinja2/filters.py+2 added-1 removed
29 unmodified lines
303132def evalcontextfilter(f):33 """Decorator for marking eval-context dependent filters. An eval34 context object is passed as first argument.34 context object is passed as first argument. For more information35 about the eval context, see :ref:`eval-context`.3637 .. versionadded:: 2.438 """39 f.evalcontextfilter = True
691 unmodified lines
modified:jinja2/utils.py+2 added-1 removed
129 unmodified lines
130def evalcontextfunction(f):131 """This decoraotr can be used to mark a function or method as an eval132 context callable. This is similar to the :func:`contextfunction`133 but instead of passing the context, an evaluation context object is134 passed.134 passed. For more information about the eval context, see135 :ref:`eval-context`.136137 .. versionadded:: 2.4138 """139 f.evalcontextfunction = True
663 unmodified lines
modified:CHANGES+2 added-1 removed
11 unmodified lines
12- added a :class:`ModuleLoader` that can load templates from13 precompiled sources. The environment now features a method14 to compile the templates from a configured loader into a zip15 file or folder.1616- the _speedups C extension now supports Python 3.17- added support for autoescaping toggling sections and support18 for evaluation contexts (:ref:`eval-context`).1920Version 2.3.121-------------22(bugfix release, released on February 19th 2010)
142 unmodified lines