Skip to content
Myr

Merge pull request #284 from LucianU/patch-1

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

Spelling, grammar, and punctuation fixes to extensions docs.

authored by Armin Ronacher · committed by Armin Ronacher · parents 6a04a8b, 07497dc · Browse files · 1 file+12 added−12 removed
This commit

State

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

Diff stats

1 file+12 added−12 removed

  • other:+12 added−12 removed

1 file+12 added−12 removed

modified:docs/extensions.rst+12 added-12 removed
2 unmodified lines
3Extensions4==========56Jinja2 supports extensions that can add extra filters, tests, globals or even7extend the parser. The main motivation of extensions is it to move often used7extend the parser. The main motivation of extensions is to move often used8code into a reusable class like adding support for internationalization.91011Adding Extensions
1 unmodified line
1314Extensions are added to the Jinja2 environment at creation time. Once the15environment is created additional extensions cannot be added. To add an16extension pass a list of extension classes or import paths to the17`environment` parameter of the :class:`Environment` constructor. The following17`extensions` parameter of the :class:`Environment` constructor. The following18example creates a Jinja2 environment with the i18n extension loaded::1920 jinja_env = Environment(extensions=['jinja2.ext.i18n'])21
9 unmodified lines
31used in combination with `gettext`_ or `babel`_. If the i18n extension is32enabled Jinja2 provides a `trans` statement that marks the wrapped string as33translatable and calls `gettext`.3435After enabling dummy `_` function that forwards calls to `gettext` is added35After enabling, dummy `_` function that forwards calls to `gettext` is added36to the environment globals. An internationalized application then has to37provide at least an `gettext` and optionally a `ngettext` function into the38namespace. Either globally or for each rendering.37provide a `gettext` function and optionally an `ngettext` function into the38namespace, either globally or for each rendering.3940Environment Methods41~~~~~~~~~~~~~~~~~~~4243After enabling of the extension the environment provides the following43After enabling the extension, the environment provides the following44additional methods:4546.. method:: jinja2.Environment.install_gettext_translations(translations, newstyle=False)4748 Installs a translation globally for that environment. The tranlations48 Installs a translation globally for that environment. The translations49 object provided must implement at least `ugettext` and `ungettext`.50 The `gettext.NullTranslations` and `gettext.GNUTranslations` classes51 as well as `Babel`_\s `Translations` class are supported.52
35 unmodified lines
88 string was extracted from embedded Python code), and89 * `message` is the string itself (a `unicode` object, or a tuple90 of `unicode` objects for functions with multiple string arguments).9192 If `Babel`_ is installed :ref:`the babel integration <babel-integration>`92 If `Babel`_ is installed, :ref:`the babel integration <babel-integration>`93 can be used to extract strings for babel.9495For a web application that is available in multiple languages but gives all96the users the same language (for example a multilingual forum software
27 unmodified lines
124125What's the big difference between standard and newstyle gettext calls? In126general they are less to type and less error prone. Also if they are used127in an autoescaping environment they better support automatic escaping.128Here some common differences between old and new calls:128Here are some common differences between old and new calls:129130standard gettext:131132.. sourcecode:: html+jinja
11 unmodified lines
144 {{ gettext('Hello World!') }}145 {{ gettext('Hello %(name)s!', name='World') }}146 {{ ngettext('%(num)d apple', '%(num)d apples', apples|count) }}147148The advantages of newstyle gettext is that you have less to type and that148The advantages of newstyle gettext are that you have less to type and that149named placeholders become mandatory. The latter sounds like a150disadvantage but solves a lot of troubles translators are often facing151when they are unable to switch the positions of two placeholder. With152newstyle gettext, all format strings look the same.
20 unmodified lines
173174**Import name:** `jinja2.ext.loopcontrols`175176This extension adds support for `break` and `continue` in loops. After177enabling Jinja2 provides those two keywords which work exactly like in177enabling, Jinja2 provides those two keywords which work exactly like in178Python.179180.. _with-extension:181
33 unmodified lines
215216By writing extensions you can add custom tags to Jinja2. This is a non trival217task and usually not needed as the default tags and expressions cover all218common use cases. The i18n extension is a good example of why extensions are219useful, another one would be fragment caching.219useful. Another one would be fragment caching.220221When writing extensions you have to keep in mind that you are working with the222Jinja2 template compiler which does not validate the node tree you are passing223to it. If the AST is malformed you will get all kinds of compiler or runtime
124 unmodified lines