Skip to content
Myr

Add docs for namespace functionality

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

authored by Adrian Moennich · committed by Adrian Moennich · parent e2a1869 · Browse files · 2 files+43 added−0 removed
This commit

State

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

Diff stats

2 files+43 added−0 removed

  • other:+43 added−0 removed

2 files+43 added−0 removed

modified:docs/api.rst+2 added
685 unmodified lines
686 The reason why the filename and error message are bytestrings and not687 unicode strings is that Python 2.x is not using unicode for exceptions688 and tracebacks as well as the compiler. This will change with Python 3.689690.. autoexception:: jinja2.TemplateRuntimeError691692.. autoexception:: jinja2.TemplateAssertionError693694695.. _writing-filters:
230 unmodified lines
modified:docs/templates.rst+41 added
906 unmodified lines
907 {% else %}908 did not iterate909 {% endfor %}910911 As of version 2.10 more complex use cases can be handled using namespace912 objects which allow propagating of changes across scopes::913914 {% set ns = namespace(found=false) %}915 {% for item in items %}916 {% if item.check_something() %}917 {% set ns.found = true %}918 {% endif %}919 * {{ item.title }}920 {% endfor %}921 Found item having something: {{ ns.found }}922923 Note hat the ``obj.attr`` notation in the `set` tag is only allowed for924 namespace objects; attempting to assign an attribute on any other object925 will raise an exception.926927 .. versionadded:: 2.10 Added support for namespace objects928929930Block Assignments931~~~~~~~~~~~~~~~~~932933.. versionadded:: 2.8
483 unmodified lines
1417 {% endif %}14181419 .. versionadded:: 2.114201421.. class:: namespace(...)14221423 Creates a new container that allows attribute assignment using the1424 ``{% set %}`` tag::14251426 {% set ns = namespace() %}1427 {% set ns.foo = 'bar' %}14281429 The main purpose of this is to allow carrying a value from within a loop1430 body to an outer scope. Initial values can be provided as a dict, as1431 keyword arguments, or both (same behavior as Python's `dict` constructor)::14321433 {% set ns = namespace(found=false) %}1434 {% for item in items %}1435 {% if item.check_something() %}1436 {% set ns.found = true %}1437 {% endif %}1438 * {{ item.title }}1439 {% endfor %}1440 Found item having something: {{ ns.found }}14411442 .. versionadded:: 2.10144314441445Extensions1446----------14471448The following sections cover the built-in Jinja2 extensions that may be
192 unmodified lines