blob: 1875711df49143b38b880111ef2cbb4ae01c979f [file] [log] [blame]
Armin Ronachered98cac2008-05-07 08:42:11 +02001Integration
2===========
3
4Jinja2 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.
7
8.. _babel-integration:
9
10Babel Integration
11-----------------
12
13Jinja provides support for extracting gettext messages from templates via a
14`Babel`_ extractor entry point called `jinja2.ext.babel_extract`. The Babel
15support is implemented as part of the :ref:`i18n-extension` extension.
16
17Gettext messages extracted from both `trans` tags and code expressions.
18
19To extract gettext messages from templates, the project needs a Jinja2 section
20in its Babel extraction method `mapping file`_:
21
22.. sourcecode:: ini
23
Armin Ronacher2b228742008-05-18 20:29:32 +020024 [jinja2: **/templates/**.html]
Armin Ronachered98cac2008-05-07 08:42:11 +020025 encoding = utf-8
26
27The syntax related options of the :class:`Environment` are also available as
28configuration values in the mapping file. For example to tell the extraction
29that templates use ``%`` as `line_statement_prefix` you can use this code:
30
31.. sourcecode:: ini
32
Armin Ronacher2b228742008-05-18 20:29:32 +020033 [jinja2: **/templates/**.html]
Armin Ronachered98cac2008-05-07 08:42:11 +020034 encoding = utf-8
35 line_statement_prefix = %
36
37:ref:`jinja-extensions` may also be defined by passing a comma separated list
38of import paths as `extensions` value. The i18n extension is added
39automatically.
40
41.. _mapping file: http://babel.edgewall.org/wiki/Documentation/messages.html#extraction-method-mapping-and-configuration
42
Armin Ronachered98cac2008-05-07 08:42:11 +020043Pylons
44------
45
Armin Ronacherba808262008-05-09 14:38:09 +020046With `Pylons`_ 0.9.7 onwards it's incredible easy to integrate Jinja into a
47Pylons powered application.
Armin Ronachered98cac2008-05-07 08:42:11 +020048
Armin Ronacherba808262008-05-09 14:38:09 +020049The template engine is configured in `config/environment.py`. The configuration
50for Jinja2 looks something like that::
Armin Ronachered98cac2008-05-07 08:42:11 +020051
Armin Ronacherba808262008-05-09 14:38:09 +020052 from jinja2 import Environment, PackageLoader
53 config['pylons.app_globals'].jinja_env = Environment(
54 loader=PackageLoader('yourapplication', 'templates')
55 )
56
57After that you can render Jinja templates by using the `render_jinja` function
58from the `pylons.templating` module.
59
60Additionally 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 return
62an empty string and not an undefined object. To change this just use this
63snippet and add it into your `config/environment.py`::
64
65 config['pylons.strict_c'] = True
66
67.. _Pylons: http://www.pylonshq.com/
Armin Ronachered98cac2008-05-07 08:42:11 +020068
69TextMate
70--------
71
Armin Ronacherdecf7e32008-05-09 11:54:07 +020072Inside the `ext` folder of Jinja2 there is a bundle for TextMate that supports
73syntax highlighting for Jinja1 and Jinja2 for text based templates as well as
74HTML. It also contains a few often used snippets.
Armin Ronachered98cac2008-05-07 08:42:11 +020075
76Vim
77---
78
Armin Ronacherdecf7e32008-05-09 11:54:07 +020079A syntax plugin for `Vim`_ exists in the Vim-scripts directory as well as the
80ext 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 HTML
83templates.
84
85Copy the files into your `syntax` folder.
Armin Ronachered98cac2008-05-07 08:42:11 +020086
87.. _Babel: http://babel.edgewall.org/
Armin Ronacherdecf7e32008-05-09 11:54:07 +020088.. _Vim: http://www.vim.org/