Skip to content
Myr

integration.rst

This file

Path

docs/integration.rst

88 lines · 2.9 KiB

Last changed by

some small documentation fixesArmin Ronacherbefore adoption2b22874

History of this file

Take it

1Integration2===========34Jinja2 provides some code for integration into other tools such as frameworks,5the `Babel`_ library or your favourite editor for fancy code highlighting.6This is a brief description of whats included.78.. _babel-integration:910Babel Integration11-----------------1213Jinja provides support for extracting gettext messages from templates via a14`Babel`_ extractor entry point called `jinja2.ext.babel_extract`.  The Babel15support is implemented as part of the :ref:`i18n-extension` extension.1617Gettext messages extracted from both `trans` tags and code expressions.1819To extract gettext messages from templates, the project needs a Jinja2 section20in its Babel extraction method `mapping file`_:2122.. sourcecode:: ini2324    [jinja2: **/templates/**.html]25    encoding = utf-82627The syntax related options of the :class:`Environment` are also available as28configuration values in the mapping file.  For example to tell the extraction29that templates use ``%`` as `line_statement_prefix` you can use this code:3031.. sourcecode:: ini3233    [jinja2: **/templates/**.html]34    encoding = utf-835    line_statement_prefix = %3637:ref:`jinja-extensions` may also be defined by passing a comma separated list38of import paths as `extensions` value.  The i18n extension is added39automatically.4041.. _mapping file: http://babel.edgewall.org/wiki/Documentation/messages.html#extraction-method-mapping-and-configuration4243Pylons44------4546With `Pylons`_ 0.9.7 onwards it's incredible easy to integrate Jinja into a47Pylons powered application.4849The template engine is configured in `config/environment.py`.  The configuration50for Jinja2 looks something like that::5152    from jinja2 import Environment, PackageLoader53    config['pylons.app_globals'].jinja_env = Environment(54        loader=PackageLoader('yourapplication', 'templates')55    )5657After that you can render Jinja templates by using the `render_jinja` function58from the `pylons.templating` module.5960Additionally it's a good idea to set the Pylons' `c` object into strict mode.61Per default any attribute to not existing attributes on the `c` object return62an empty string and not an undefined object.  To change this just use this63snippet and add it into your `config/environment.py`::6465    config['pylons.strict_c'] = True6667.. _Pylons: http://www.pylonshq.com/6869TextMate70--------7172Inside the `ext` folder of Jinja2 there is a bundle for TextMate that supports73syntax highlighting for Jinja1 and Jinja2 for text based templates as well as74HTML.  It also contains a few often used snippets.7576Vim77---7879A syntax plugin for `Vim`_ exists in the Vim-scripts directory as well as the80ext folder of Jinja2.  `The script <http://www.vim.org/scripts/script.php?script_id=1856>`_81supports Jinja1 and Jinja2.  Once installed two file types are available `jinja`82and `htmljinja`.  The first one for text based templates, the latter for HTML83templates.8485Copy the files into your `syntax` folder.8687.. _Babel: http://babel.edgewall.org/88.. _Vim: http://www.vim.org/