blob: 25f4abae5e51efab3c4d43d2f9291ebe310982b9 [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
Ron DuPlaine5763b32012-03-13 14:53:15 -07008Files to help integration are available
José Carlos Garcíac5a860c2016-04-04 01:21:39 +02009`here. <https://github.com/pallets/jinja/tree/master/ext>`_
Ron DuPlaine5763b32012-03-13 14:53:15 -070010
Armin Ronachered98cac2008-05-07 08:42:11 +020011.. _babel-integration:
12
13Babel Integration
14-----------------
15
16Jinja provides support for extracting gettext messages from templates via a
17`Babel`_ extractor entry point called `jinja2.ext.babel_extract`. The Babel
18support is implemented as part of the :ref:`i18n-extension` extension.
19
20Gettext messages extracted from both `trans` tags and code expressions.
21
22To extract gettext messages from templates, the project needs a Jinja2 section
23in its Babel extraction method `mapping file`_:
24
25.. sourcecode:: ini
26
Armin Ronacher2b228742008-05-18 20:29:32 +020027 [jinja2: **/templates/**.html]
Armin Ronachered98cac2008-05-07 08:42:11 +020028 encoding = utf-8
29
30The syntax related options of the :class:`Environment` are also available as
31configuration values in the mapping file. For example to tell the extraction
32that templates use ``%`` as `line_statement_prefix` you can use this code:
33
34.. sourcecode:: ini
35
Armin Ronacher2b228742008-05-18 20:29:32 +020036 [jinja2: **/templates/**.html]
Armin Ronachered98cac2008-05-07 08:42:11 +020037 encoding = utf-8
38 line_statement_prefix = %
39
40:ref:`jinja-extensions` may also be defined by passing a comma separated list
41of import paths as `extensions` value. The i18n extension is added
42automatically.
43
Armin Ronacher11619152011-12-15 11:50:27 +010044.. versionchanged:: 2.7
45
46 Until 2.7 template syntax errors were always ignored. This was done
47 since many people are dropping non template html files into the
48 templates folder and it would randomly fail. The assumption was that
49 testsuites will catch syntax errors in templates anyways. If you don't
50 want that behavior you can add ``silent=false`` to the settings and
51 exceptions are propagated.
52
Armin Ronachered98cac2008-05-07 08:42:11 +020053.. _mapping file: http://babel.edgewall.org/wiki/Documentation/messages.html#extraction-method-mapping-and-configuration
54
Armin Ronachered98cac2008-05-07 08:42:11 +020055Pylons
56------
57
Armin Ronacherba808262008-05-09 14:38:09 +020058With `Pylons`_ 0.9.7 onwards it's incredible easy to integrate Jinja into a
59Pylons powered application.
Armin Ronachered98cac2008-05-07 08:42:11 +020060
Armin Ronacherba808262008-05-09 14:38:09 +020061The template engine is configured in `config/environment.py`. The configuration
62for Jinja2 looks something like that::
Armin Ronachered98cac2008-05-07 08:42:11 +020063
Armin Ronacherba808262008-05-09 14:38:09 +020064 from jinja2 import Environment, PackageLoader
65 config['pylons.app_globals'].jinja_env = Environment(
66 loader=PackageLoader('yourapplication', 'templates')
67 )
68
69After that you can render Jinja templates by using the `render_jinja` function
70from the `pylons.templating` module.
71
72Additionally 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 return
74an empty string and not an undefined object. To change this just use this
75snippet and add it into your `config/environment.py`::
76
77 config['pylons.strict_c'] = True
78
79.. _Pylons: http://www.pylonshq.com/
Armin Ronachered98cac2008-05-07 08:42:11 +020080
81TextMate
82--------
83
Abdullah678b4882016-01-04 20:13:07 +030084There is a bundle for TextMate that supports syntax highlighting for Jinja1 and Jinja2 for text based
Ron DuPlaine5763b32012-03-13 14:53:15 -070085templates as well as HTML. It also contains a few often used snippets.
Armin Ronachered98cac2008-05-07 08:42:11 +020086
Abdullah678b4882016-01-04 20:13:07 +030087.. _TextMate Bundle: https://github.com/mitsuhiko/jinja2-tmbundle
88
Armin Ronachered98cac2008-05-07 08:42:11 +020089Vim
90---
91
Armin Ronacherdecf7e32008-05-09 11:54:07 +020092A syntax plugin for `Vim`_ exists in the Vim-scripts directory as well as the
Ron DuPlaine5763b32012-03-13 14:53:15 -070093`ext` folder at the root of the Jinja2 project. `The script
94<http://www.vim.org/scripts/script.php?script_id=1856>`_ supports Jinja1 and
95Jinja2. Once installed two file types are available `jinja` and `htmljinja`.
96The first one for text based templates, the latter for HTML templates.
Armin Ronacherdecf7e32008-05-09 11:54:07 +020097
98Copy the files into your `syntax` folder.
Armin Ronachered98cac2008-05-07 08:42:11 +020099
100.. _Babel: http://babel.edgewall.org/
Armin Ronacherdecf7e32008-05-09 11:54:07 +0200101.. _Vim: http://www.vim.org/