Skip to content
Myr

integration.rst

This file

Path

docs/integration.rst

106 lines · 3.7 KiB

Last changed by

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

History of this file

Take it

1Integration2===========34Jinja 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 Jinja 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 Jinja 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: https://pylonshq.com/8081TextMate82--------8384There is a `bundle for TextMate`_ that supports syntax highlighting for Jinja 185and Jinja 2 for text based templates as well as HTML. It also contains a few86often used snippets.8788.. _bundle for TextMate: https://github.com/mitsuhiko/jinja2-tmbundle8990Vim91---9293A syntax plugin for `Vim`_ is available `from the jinja repository94<https://github.com/pallets/jinja/blob/master/ext/Vim/jinja.vim>`_. The script95supports Jinja 1 and Jinja 2. Once installed, two file types are available96(``jinja`` and ``htmljinja``). The first one is for text-based templates and the97second is for HTML templates. For HTML documents, the plugin attempts to98automatically detect Jinja syntax inside of existing HTML documents.99100If you are using a plugin manager like `Pathogen`_, see the `vim-jinja101<https://github.com/mitsuhiko/vim-jinja>`_ repository for installing in the102``bundle/`` directory.103104.. _Babel: http://babel.pocoo.org/105.. _Vim: https://www.vim.org/106.. _Pathogen: https://github.com/tpope/vim-pathogen