blob: fc68e68c6204148628c2cd801eef9f12bc252baa [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
Armin Ronacher61a5a242008-05-26 12:07:44 +020037C-compiler is available and you are using Python 2.4 the `ctypes`_ module
38should be installed.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020039
Armin Ronacher56d01072008-11-23 13:25:51 +010040If you don't have a working C compiler and you are trying to install the source
Armin Ronacher9ebc4572009-09-13 00:30:06 -070041release with the speedups you will get a compiler error. This however can be
42circumvented by passing the ``--without-speedups`` command line argument to the
43setup script::
Armin Ronacher86b5cb52009-09-13 00:23:27 -070044
Armin Ronacher237f01b2009-09-13 10:35:54 -070045 $ python setup.py --with-speedups install
Armin Ronacher56d01072008-11-23 13:25:51 +010046
Armin Ronacher9ebc4572009-09-13 00:30:06 -070047(As of Jinja 2.2, the speedups are disabled by default and can be enabled
48with ``--with-speedups``. See :ref:`enable-speedups`)
Armin Ronacher56d01072008-11-23 13:25:51 +010049
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020050.. _ctypes: http://python.net/crew/theller/ctypes/
51
52
Armin Ronachered98cac2008-05-07 08:42:11 +020053Installation
54------------
55
56You have multiple ways to install Jinja2. If you are unsure what to do, go
57with the Python egg or tarball.
58
59As a Python egg (via easy_install)
60~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61
Armin Ronacher56d01072008-11-23 13:25:51 +010062You can install the most recent Jinja2 version using `easy_install`_ or `pip`_::
Armin Ronachered98cac2008-05-07 08:42:11 +020063
Armin Ronacher86b5cb52009-09-13 00:23:27 -070064 easy_install Jinja2
65 pip install Jinja2
Armin Ronachered98cac2008-05-07 08:42:11 +020066
67This will install a Jinja2 egg in your Python installation's site-packages
68directory.
69
Armin Ronacher56d01072008-11-23 13:25:51 +010070(If you are installing from the windows command line omit the `sudo` and make
71sure to run the command as user with administrator rights)
72
Armin Ronachered98cac2008-05-07 08:42:11 +020073From the tarball release
74~~~~~~~~~~~~~~~~~~~~~~~~~
75
761. Download the most recent tarball from the `download page`_
772. Unpack the tarball
783. ``sudo python setup.py install``
79
80Note that the last command will automatically download and install
81`setuptools`_ if you don't already have it installed. This requires a working
82internet connection.
83
84This will install Jinja2 into your Python installation's site-packages directory.
85
86Installing the development version
87~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
891. Install `mercurial`_
Armin Ronacherbbbe0622008-05-19 00:23:37 +0200902. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2``
Armin Ronachered98cac2008-05-07 08:42:11 +0200913. ``cd jinja2``
924. ``ln -s jinja2 /usr/lib/python2.X/site-packages``
93
94As an alternative to steps 4 you can also do ``python setup.py develop``
95which will install the package via setuptools in development mode. This also
96has the advantage that the C extensions are compiled.
97
98Alternative you can use `easy_install`_ to install the current development
99snapshot::
100
101 sudo easy_install Jinja2==dev
102
Armin Ronacher56d01072008-11-23 13:25:51 +0100103Or the new `pip`_ command::
104
105 sudo pip install Jinja2==dev
106
Armin Ronacher0aa0f582009-03-18 01:01:36 +0100107.. _download page: http://pypi.python.org/pypi/Jinja2
Armin Ronachered98cac2008-05-07 08:42:11 +0200108.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools
109.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall
Armin Ronacher56d01072008-11-23 13:25:51 +0100110.. _pip: http://pypi.python.org/pypi/pip
Armin Ronachered98cac2008-05-07 08:42:11 +0200111.. _mercurial: http://www.selenic.com/mercurial/
112
Armin Ronacher9ebc4572009-09-13 00:30:06 -0700113.. _enable-speedups:
Armin Ronacher56d01072008-11-23 13:25:51 +0100114
Armin Ronacher9ebc4572009-09-13 00:30:06 -0700115Enaable the speedups Module
Armin Ronacher56d01072008-11-23 13:25:51 +0100116~~~~~~~~~~~~~~~~~~~~~~~~~~~
117
Armin Ronacher9ebc4572009-09-13 00:30:06 -0700118By default Jinja2 will not compile the speedups module. Enabling this
Armin Ronacher56d01072008-11-23 13:25:51 +0100119will fail if you don't have the Python headers or a working compiler. This
120is often the case if you are installing Jinja2 from a windows machine.
121
Armin Ronacher9ebc4572009-09-13 00:30:06 -0700122You can enable the speedups extension when installing using the
123``--with-speedups`` flag::
Armin Ronacher56d01072008-11-23 13:25:51 +0100124
Armin Ronacher237f01b2009-09-13 10:35:54 -0700125 sudo python setup.py --with-speedups install
Armin Ronacher56d01072008-11-23 13:25:51 +0100126
Armin Ronacher56d01072008-11-23 13:25:51 +0100127
Armin Ronachered98cac2008-05-07 08:42:11 +0200128
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200129Basic API Usage
130---------------
131
Armin Ronacher4dcc2372008-05-26 13:59:26 +0200132This section gives you a brief introduction to the Python API for Jinja2
133templates.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200134
135The most basic way to create a template and render it is through
Armin Ronacher4dcc2372008-05-26 13:59:26 +0200136:class:`~jinja2.Template`. This however is not the recommended way to
137work with it if your templates are not loaded from strings but the file
138system or another data source:
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200139
140>>> from jinja2 import Template
141>>> template = Template('Hello {{ name }}!')
142>>> template.render(name='John Doe')
143u'Hello John Doe!'
144
Armin Ronacher4dcc2372008-05-26 13:59:26 +0200145By creating an instance of :class:`~jinja2.Template` you get back a new template
146object that provides a method called :meth:`~jinja2.Template.render` which when
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200147called with a dict or keyword arguments expands the template. The dict
148or keywords arguments passed to the template are the so-called "context"
149of the template.
Armin Ronacher61a5a242008-05-26 12:07:44 +0200150
151What you can see here is that Jinja2 is using unicode internally and the
152return value is an unicode string. So make sure that your application is
153indeed using unicode internally.