Skip to content
Myr

integration.rst

This file

Path

docs/integration.rst

101 lines · 3.5 KiB

Last changed by

Merge branch 'master' of github.com:mitsuhiko/jinja2Armin Ronacherbefore adoption67d015e

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.78Files to help integration are available9`here. <https://github.com/pallets/jinja/tree/master/ext>`_1011.. _babel-integration:1213Babel Integration14-----------------1516Jinja provides support for extracting gettext messages from templates via a17`Babel`_ extractor entry point called `jinja2.ext.babel_extract`.  The Babel18support is implemented as part of the :ref:`i18n-extension` extension.1920Gettext messages extracted from both `trans` tags and code expressions.2122To extract gettext messages from templates, the project needs a Jinja2 section23in its Babel extraction method `mapping file`_:2425.. sourcecode:: ini2627    [jinja2: **/templates/**.html]28    encoding = utf-82930The syntax related options of the :class:`Environment` are also available as31configuration values in the mapping file.  For example to tell the extraction32that templates use ``%`` as `line_statement_prefix` you can use this code:3334.. sourcecode:: ini3536    [jinja2: **/templates/**.html]37    encoding = utf-838    line_statement_prefix = %3940:ref:`jinja-extensions` may also be defined by passing a comma separated list41of import paths as `extensions` value.  The i18n extension is added42automatically.4344.. versionchanged:: 2.74546   Until 2.7 template syntax errors were always ignored.  This was done47   since many people are dropping non template html files into the48   templates folder and it would randomly fail.  The assumption was that49   testsuites will catch syntax errors in templates anyways.  If you don't50   want that behavior you can add ``silent=false`` to the settings and51   exceptions are propagated.5253.. _mapping file: http://babel.pocoo.org/en/latest/messages.html#extraction-method-mapping-and-configuration5455Pylons56------5758With `Pylons`_ 0.9.7 onwards it's incredible easy to integrate Jinja into a59Pylons powered application.6061The template engine is configured in `config/environment.py`.  The configuration62for Jinja2 looks something like that::6364    from jinja2 import Environment, PackageLoader65    config['pylons.app_globals'].jinja_env = Environment(66        loader=PackageLoader('yourapplication', 'templates')67    )6869After that you can render Jinja templates by using the `render_jinja` function70from the `pylons.templating` module.7172Additionally it's a good idea to set the Pylons' `c` object into strict mode.73Per default any attribute to not existing attributes on the `c` object return74an empty string and not an undefined object.  To change this just use this75snippet and add it into your `config/environment.py`::7677    config['pylons.strict_c'] = True7879.. _Pylons: http://www.pylonshq.com/8081TextMate82--------8384There is a `bundle for TextMate`_ that supports syntax highlighting for Jinja1 and Jinja2 for text based85templates as well as HTML.  It also contains a few often used snippets.8687.. _bundle for TextMate: https://github.com/mitsuhiko/jinja2-tmbundle8889Vim90---9192A syntax plugin for `Vim`_ exists in the Vim-scripts directory as well as the93`ext` folder at the root of the Jinja2 project.  `The script94<http://www.vim.org/scripts/script.php?script_id=1856>`_ supports Jinja1 and95Jinja2.  Once installed two file types are available `jinja` and `htmljinja`.96The first one for text based templates, the latter for HTML templates.9798Copy the files into your `syntax` folder.99100.. _Babel: http://babel.pocoo.org/101.. _Vim: http://www.vim.org/