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
Provenance
- Commit
fe150f3- Tree
559e352- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c57959d
6 files changed+104 added−7 removed
modified:docs/api.rst+63 added-4 removed
351 unmodified lines352 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).355 356+ .. attribute:: eval_ctx357+358+ The current :ref:`eval-context`.359+360 .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs)361 362 363 .. admonition:: Implementation89 unmodified lines453 .. autofunction:: jinja2.environmentfilter454 455 .. autofunction:: jinja2.contextfilter456 457+.. autofunction:: jinja2.evalcontextfilter458+459 .. autofunction:: jinja2.environmentfunction460 461 .. autofunction:: jinja2.contextfunction462 463+.. autofunction:: jinja2.evalcontextfunction464+465 .. function:: escape(s)466 467 Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s`468 to HTML-safe sequences. Use this if you need to display text that might83 unmodified lines552 553 Filters can also be passed the current template context or environment. This554 is useful if a filter wants to return an undefined value or check the current555 :attr:`~Environment.autoescape` setting. For this purpose two decorators548-exist: :func:`environmentfilter` and :func:`contextfilter`.556+exist: :func:`environmentfilter`, :func:`contextfilter` and557+:func:`evalcontextfilter`.558 559 Here a small example filter that breaks a text into HTML line breaks and560 paragraphs and marks the return value as safe HTML string if autoescaping is561 enabled::2 unmodified lines564 from jinja2 import environmentfilter, Markup, escape565 566 _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')567 559- @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 result575 576 Context filters work the same just that the first argument is the current577 active :class:`Context` rather then the environment.578 579 580+.. _eval-context:581+582+Evaluation Context583+------------------584+585+The evaluation context (short eval context or eval ctx) is a new object586+introducted in Jinja 2.4 that makes it possible to activate and deactivate587+compiled features at runtime.588+589+Currently it is only used to enable and disable the automatic escaping but590+can be used for extensions as well.591+592+In previous Jinja versions filters and functions were marked as593+environment callables in order to check for the autoescape status from the594+environment. In new versions it's encouraged to check the setting from the595+evaluation context instead.596+597+Previous versions::598+599+ @environmentfilter600+ def filter(env, value):601+ result = do_something(value)602+ if env.autoescape:603+ result = Markup(result)604+ return result605+606+In new versions you can either use a :func:`contextfilter` and access the607+evaluation context from the actual context, or use a608+:func:`evalcontextfilter` which directly passes the evaluation context to609+the function::610+611+ @contextfilter612+ def filter(context, value):613+ result = do_something(value)614+ if context.eval_ctx.autoescape:615+ result = Markup(result)616+ return result617+618+ @evalcontextfilter619+ def filter(eval_ctx, value):620+ result = do_something(value)621+ if eval_ctx.autoescape:622+ result = Markup(result)623+ return result624+625+The evaluation context must not be modified at runtime. Modifications626+must only happen with a :class:`nodes.EvalContextModifier` and627+:class:`nodes.ScopedEvalContextModifier` from an extension, not on the628+eval context object itself.629+630 .. _writing-tests:631 632 Custom Tests633 ------------110 unmodified linesmodified:docs/extensions.rst+14 added
124 unmodified lines125 is possible to enforce a nested scope in a template. Variables can be126 declared directly in the opening block of the with statement or using a127 standard `set` statement directly within.128 129+.. _autoescape-extension:130 131+Autoescape Extension132+--------------------133+134+**Import name:** `jinja2.ext.autoescape`135+136+.. versionadded:: 2.4137+138+The autoescape extension allows you to toggle the autoescape feature from139+within the template. If the environment's :attr:`~Environment.autoescape`140+setting is set to `False` it can be activated, if it's `True` it can be141+activated. The setting overriding is scoped.142+143+144 .. _writing-extensions:145 146 Writing Extensions147 ------------------135 unmodified linesmodified:docs/templates.rst+21 added
1306 unmodified lines1307 {% with %}1308 {% set foo = 42 %}1309 {{ foo }}1310 {% endwith %}1311+1312+1313+Autoescape Extension1314+--------------------1315+1316+.. versionadded:: 2.41317+1318+If the application enables the :ref:`autoescape-extension` one can1319+activate and deactivate the autoescaping from within the templates.1320+1321+Example::1322+1323+ {% autoescape true %}1324+ Autoescaping is active within this block1325+ {% endautoescape %}1326+1327+ {% autoescape false %}1328+ Autoescaping is inactive within this block1329+ {% endautoescape %}1330+1331+After the `endautoescape` the behavior is reverted to what it was before.modified:jinja2/filters.py+2 added-1 removed
29 unmodified lines30 31 32 def 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`.36 37 .. versionadded:: 2.438 """39 f.evalcontextfilter = True691 unmodified linesmodified:jinja2/utils.py+2 added-1 removed
129 unmodified lines130 def 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`.136 137 .. versionadded:: 2.4138 """139 f.evalcontextfunction = True663 unmodified linesmodified:CHANGES+2 added-1 removed
11 unmodified lines12 - 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.16-16 - the _speedups C extension now supports Python 3.17+- added support for autoescaping toggling sections and support18+ for evaluation contexts (:ref:`eval-context`).19 20 Version 2.3.121 -------------22 (bugfix release, released on February 19th 2010)142 unmodified lines