blob: ffb683906d8889138deb068d18a42f1ba9af6e9e [file] [log] [blame]
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02001Introduction
2============
3
4This is the documentation for the Jinja2 general purpose templating language.
5Jinja2 is a library for Python 2.4 and onwards that is designed to be flexible,
6fast and secure.
7
8If you have any exposure to other text-based template languages, such as Smarty or
9Django, you should feel right at home with Jinja2. It's both designer and
10developer friendly by sticking to Python's principles and adding functionality
11useful for templating environments.
12
13The key-features are...
14
15- ... **configurable syntax**. If you are generating LaTeX or other formats
Armin Ronachered98cac2008-05-07 08:42:11 +020016 with Jinja2 you can change the delimiters to something that integrates better
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020017 into the LaTeX markup.
18
19- ... **fast**. While performance is not the primarily target of Jinja2 it's
20 surprisingly fast. The overhead compared to regular Python code was reduced
21 to the very minimum.
22
23- ... **easy to debug**. Jinja2 integrates directly into the python traceback
Armin Ronachered98cac2008-05-07 08:42:11 +020024 system which allows you to debug Jinja2 templates with regular python
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020025 debugging helpers.
26
27- ... **secure**. It's possible to evaluate untrusted template code if the
28 optional sandbox is enabled. This allows Jinja2 to be used as templating
29 language for applications where users may modify the template design.
30
31
32Prerequisites
33-------------
34
35Jinja2 needs at least **Python 2.4** to run. Additionally a working C-compiler
36that can create python extensions should be installed for the debugger. If no
37C-compiler is available the `ctypes`_ module should be installed.
38
39.. _ctypes: http://python.net/crew/theller/ctypes/
40
41
Armin Ronachered98cac2008-05-07 08:42:11 +020042Installation
43------------
44
45You have multiple ways to install Jinja2. If you are unsure what to do, go
46with the Python egg or tarball.
47
48As a Python egg (via easy_install)
49~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50
51You can install the most recent Jinja2 version using `easy_install`_::
52
53 sudo easy_install Jinja2
54
55This will install a Jinja2 egg in your Python installation's site-packages
56directory.
57
58From the tarball release
59~~~~~~~~~~~~~~~~~~~~~~~~~
60
611. Download the most recent tarball from the `download page`_
622. Unpack the tarball
633. ``sudo python setup.py install``
64
65Note that the last command will automatically download and install
66`setuptools`_ if you don't already have it installed. This requires a working
67internet connection.
68
69This will install Jinja2 into your Python installation's site-packages directory.
70
71Installing the development version
72~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
741. Install `mercurial`_
Armin Ronacherbbbe0622008-05-19 00:23:37 +0200752. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2``
Armin Ronachered98cac2008-05-07 08:42:11 +0200763. ``cd jinja2``
774. ``ln -s jinja2 /usr/lib/python2.X/site-packages``
78
79As an alternative to steps 4 you can also do ``python setup.py develop``
80which will install the package via setuptools in development mode. This also
81has the advantage that the C extensions are compiled.
82
83Alternative you can use `easy_install`_ to install the current development
84snapshot::
85
86 sudo easy_install Jinja2==dev
87
88.. _download page: http://jinja.pocoo.org/2/download
89.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools
90.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall
91.. _mercurial: http://www.selenic.com/mercurial/
92
93
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020094Basic API Usage
95---------------
96
Armin Ronachered98cac2008-05-07 08:42:11 +020097This section gives you a brief introduction to the Python API for Jinja2 templates.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020098
99The most basic way to create a template and render it is through
100:class:`Template`. This however is not the recommended way to work with it,
101but the easiest
102
103>>> from jinja2 import Template
104>>> template = Template('Hello {{ name }}!')
105>>> template.render(name='John Doe')
106u'Hello John Doe!'
107
108By creating an instance of :class:`Template` you get back a new template
109object that provides a method called :meth:`~Template.render` which when
110called with a dict or keyword arguments expands the template. The dict
111or keywords arguments passed to the template are the so-called "context"
112of the template.