blob: e8c4678975b9ffbc7fc2e170b3e63a0942c27392 [file] [log] [blame]
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02001Introduction
2============
3
4This is the documentation for the Jinja2 general purpose templating language.
Thomas Waldmann50f69652013-05-18 01:07:52 +02005Jinja2 is a library for Python that is designed to be flexible, fast and secure.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02006
7If you have any exposure to other text-based template languages, such as Smarty or
8Django, you should feel right at home with Jinja2. It's both designer and
9developer friendly by sticking to Python's principles and adding functionality
10useful for templating environments.
11
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020012Prerequisites
13-------------
14
Armin Ronacherfe63b442013-05-19 12:29:55 +010015Jinja2 works with Python 2.6.x, 2.7.x and >= 3.3. If you are using Python
Armin Ronacherd76b8da2013-05-20 12:15:50 +0100163.2 you can use an older release of Jinja2 (2.6) as support for Python 3.2
17was dropped in Jinja2 version 2.7.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020018
Armin Ronacherfe63b442013-05-19 12:29:55 +010019If you wish to use the :class:`~jinja2.PackageLoader` class, you will also
Carl A Dunhamd5463582014-01-18 15:26:10 -060020need `setuptools`_ or `distribute`_ installed at runtime.
Thomas Waldmann50f69652013-05-18 01:07:52 +020021
Armin Ronachered98cac2008-05-07 08:42:11 +020022Installation
23------------
24
25You have multiple ways to install Jinja2. If you are unsure what to do, go
26with the Python egg or tarball.
27
Carl A Dunhamd5463582014-01-18 15:26:10 -060028As a Python egg (via `easy_install`)
29~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Armin Ronachered98cac2008-05-07 08:42:11 +020030
Armin Ronacher56d01072008-11-23 13:25:51 +010031You can install the most recent Jinja2 version using `easy_install`_ or `pip`_::
Armin Ronachered98cac2008-05-07 08:42:11 +020032
Armin Ronacher86b5cb52009-09-13 00:23:27 -070033 easy_install Jinja2
34 pip install Jinja2
Armin Ronachered98cac2008-05-07 08:42:11 +020035
36This will install a Jinja2 egg in your Python installation's site-packages
37directory.
38
Mike Chesnutd329a3e2012-12-18 13:52:07 -080039(If you are installing from the Windows command line omit the `sudo` and make
Armin Ronacher56d01072008-11-23 13:25:51 +010040sure to run the command as user with administrator rights)
41
Armin Ronachered98cac2008-05-07 08:42:11 +020042From the tarball release
43~~~~~~~~~~~~~~~~~~~~~~~~~
44
451. Download the most recent tarball from the `download page`_
462. Unpack the tarball
473. ``sudo python setup.py install``
48
Carl A Dunhamd5463582014-01-18 15:26:10 -060049Note that you either have to have `setuptools` or `distribute` installed;
Armin Ronacher00452162010-02-10 02:06:06 +010050the latter is preferred.
Armin Ronachered98cac2008-05-07 08:42:11 +020051
52This will install Jinja2 into your Python installation's site-packages directory.
53
54Installing the development version
55~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
Armin Ronacherd811ab52010-10-17 16:07:08 +0200571. Install `git`_
José Carlos Garcíac5a860c2016-04-04 01:21:39 +0200582. ``git clone git://github.com/pallets/jinja.git``
Armin Ronachered98cac2008-05-07 08:42:11 +0200593. ``cd jinja2``
604. ``ln -s jinja2 /usr/lib/python2.X/site-packages``
61
62As an alternative to steps 4 you can also do ``python setup.py develop``
Carl A Dunhamd5463582014-01-18 15:26:10 -060063which will install the package via `distribute` in development mode. This also
Armin Ronachered98cac2008-05-07 08:42:11 +020064has the advantage that the C extensions are compiled.
65
Armin Ronacher0aa0f582009-03-18 01:01:36 +010066.. _download page: http://pypi.python.org/pypi/Jinja2
Carl A Dunhamd5463582014-01-18 15:26:10 -060067.. _distribute: http://pypi.python.org/pypi/distribute
Armin Ronachered98cac2008-05-07 08:42:11 +020068.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools
69.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall
Armin Ronacher56d01072008-11-23 13:25:51 +010070.. _pip: http://pypi.python.org/pypi/pip
Armin Ronacher54a13bc2010-10-17 16:07:38 +020071.. _git: http://git-scm.org/
Armin Ronachered98cac2008-05-07 08:42:11 +020072
Armin Ronacher56d01072008-11-23 13:25:51 +010073
Armin Ronachercd7bf5b2013-05-19 13:51:47 +010074MarkupSafe Dependency
75~~~~~~~~~~~~~~~~~~~~~
Armin Ronacher56d01072008-11-23 13:25:51 +010076
Armin Ronachercd7bf5b2013-05-19 13:51:47 +010077As of version 2.7 Jinja2 depends on the `MarkupSafe`_ module. If you
78install Jinja2 via `pip` or `easy_install` it will be installed
79automatically for you.
Armin Ronacher10c34da2010-08-17 12:10:27 +020080
81.. _MarkupSafe: http://pypi.python.org/pypi/MarkupSafe
82
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020083Basic API Usage
84---------------
85
Armin Ronacher4dcc2372008-05-26 13:59:26 +020086This section gives you a brief introduction to the Python API for Jinja2
87templates.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020088
89The most basic way to create a template and render it is through
Armin Ronacher4dcc2372008-05-26 13:59:26 +020090:class:`~jinja2.Template`. This however is not the recommended way to
91work with it if your templates are not loaded from strings but the file
92system or another data source:
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020093
94>>> from jinja2 import Template
95>>> template = Template('Hello {{ name }}!')
96>>> template.render(name='John Doe')
97u'Hello John Doe!'
98
Armin Ronacher4dcc2372008-05-26 13:59:26 +020099By creating an instance of :class:`~jinja2.Template` you get back a new template
100object that provides a method called :meth:`~jinja2.Template.render` which when
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200101called with a dict or keyword arguments expands the template. The dict
102or keywords arguments passed to the template are the so-called "context"
103of the template.
Armin Ronacher61a5a242008-05-26 12:07:44 +0200104
105What you can see here is that Jinja2 is using unicode internally and the
106return value is an unicode string. So make sure that your application is
107indeed using unicode internally.
Armin Ronachereb43b122010-02-10 02:10:48 +0100108
109
110Experimental Python 3 Support
111-----------------------------
112
Thomas Waldmann50f69652013-05-18 01:07:52 +0200113Jinja 2.7 brings experimental support for Python >=3.3. It means that all
Armin Ronachereb43b122010-02-10 02:10:48 +0100114unittests pass on the new version, but there might still be small bugs in
115there and behavior might be inconsistent. If you notice any bugs, please
116provide feedback in the `Jinja bug tracker`_.
117
118Also please keep in mind that the documentation is written with Python 2
Carl A Dunhamd5463582014-01-18 15:26:10 -0600119in mind, so you will have to adapt the shown code examples to Python 3 syntax
Armin Ronachereb43b122010-02-10 02:10:48 +0100120for yourself.
121
122
José Carlos Garcíac5a860c2016-04-04 01:21:39 +0200123.. _Jinja bug tracker: http://github.com/pallets/jinja/issues