Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 1 | Introduction |
| 2 | ============ |
| 3 | |
| 4 | This is the documentation for the Jinja2 general purpose templating language. |
| 5 | Jinja2 is a library for Python 2.4 and onwards that is designed to be flexible, |
| 6 | fast and secure. |
| 7 | |
| 8 | If you have any exposure to other text-based template languages, such as Smarty or |
| 9 | Django, you should feel right at home with Jinja2. It's both designer and |
| 10 | developer friendly by sticking to Python's principles and adding functionality |
| 11 | useful for templating environments. |
| 12 | |
| 13 | The key-features are... |
| 14 | |
| 15 | - ... **configurable syntax**. If you are generating LaTeX or other formats |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 16 | with Jinja2 you can change the delimiters to something that integrates better |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 17 | 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 Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 24 | system which allows you to debug Jinja2 templates with regular python |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 25 | 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 | |
| 32 | Prerequisites |
| 33 | ------------- |
| 34 | |
| 35 | Jinja2 needs at least **Python 2.4** to run. Additionally a working C-compiler |
| 36 | that can create python extensions should be installed for the debugger. If no |
Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 37 | C-compiler is available and you are using Python 2.4 the `ctypes`_ module |
| 38 | should be installed. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 39 | |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 40 | If you don't have a working C compiler and you are trying to install the source |
| 41 | release you will get a compiler error. This however can be circumvented by |
Armin Ronacher | 86b5cb5 | 2009-09-13 00:23:27 -0700 | [diff] [blame] | 42 | passing the ``--without-speedups`` command line argument to the setup script:: |
| 43 | |
| 44 | $ python setup.py install --without-speedups |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 45 | |
| 46 | For more details about that have a look at the :ref:`disable-speedups` |
| 47 | section below. |
| 48 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 49 | .. _ctypes: http://python.net/crew/theller/ctypes/ |
| 50 | |
| 51 | |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 52 | Installation |
| 53 | ------------ |
| 54 | |
| 55 | You have multiple ways to install Jinja2. If you are unsure what to do, go |
| 56 | with the Python egg or tarball. |
| 57 | |
| 58 | As a Python egg (via easy_install) |
| 59 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 60 | |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 61 | You can install the most recent Jinja2 version using `easy_install`_ or `pip`_:: |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 62 | |
Armin Ronacher | 86b5cb5 | 2009-09-13 00:23:27 -0700 | [diff] [blame] | 63 | easy_install Jinja2 |
| 64 | pip install Jinja2 |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 65 | |
| 66 | This will install a Jinja2 egg in your Python installation's site-packages |
| 67 | directory. |
| 68 | |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 69 | (If you are installing from the windows command line omit the `sudo` and make |
| 70 | sure to run the command as user with administrator rights) |
| 71 | |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 72 | From the tarball release |
| 73 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | |
| 75 | 1. Download the most recent tarball from the `download page`_ |
| 76 | 2. Unpack the tarball |
| 77 | 3. ``sudo python setup.py install`` |
| 78 | |
| 79 | Note that the last command will automatically download and install |
| 80 | `setuptools`_ if you don't already have it installed. This requires a working |
| 81 | internet connection. |
| 82 | |
| 83 | This will install Jinja2 into your Python installation's site-packages directory. |
| 84 | |
| 85 | Installing the development version |
| 86 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 87 | |
| 88 | 1. Install `mercurial`_ |
Armin Ronacher | bbbe062 | 2008-05-19 00:23:37 +0200 | [diff] [blame] | 89 | 2. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2`` |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 90 | 3. ``cd jinja2`` |
| 91 | 4. ``ln -s jinja2 /usr/lib/python2.X/site-packages`` |
| 92 | |
| 93 | As an alternative to steps 4 you can also do ``python setup.py develop`` |
| 94 | which will install the package via setuptools in development mode. This also |
| 95 | has the advantage that the C extensions are compiled. |
| 96 | |
| 97 | Alternative you can use `easy_install`_ to install the current development |
| 98 | snapshot:: |
| 99 | |
| 100 | sudo easy_install Jinja2==dev |
| 101 | |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 102 | Or the new `pip`_ command:: |
| 103 | |
| 104 | sudo pip install Jinja2==dev |
| 105 | |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 106 | .. _download page: http://pypi.python.org/pypi/Jinja2 |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 107 | .. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools |
| 108 | .. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 109 | .. _pip: http://pypi.python.org/pypi/pip |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 110 | .. _mercurial: http://www.selenic.com/mercurial/ |
| 111 | |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 112 | .. _disable-speedups: |
| 113 | |
| 114 | Disable the speedups Module |
| 115 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 116 | |
| 117 | By default Jinja2 will try to compile the speedups module. This of course |
| 118 | will fail if you don't have the Python headers or a working compiler. This |
| 119 | is often the case if you are installing Jinja2 from a windows machine. |
| 120 | |
| 121 | You can disable the speedups extension when installing using the |
| 122 | ``--without-speedups`` flag:: |
| 123 | |
| 124 | sudo python setup.py install --without-speedups |
| 125 | |
Armin Ronacher | 86b5cb5 | 2009-09-13 00:23:27 -0700 | [diff] [blame] | 126 | You can also pass this parameter to `pip`:: |
| 127 | |
| 128 | $ pip install --install-option='--without-speedups' Jinja2 |
Armin Ronacher | 56d0107 | 2008-11-23 13:25:51 +0100 | [diff] [blame] | 129 | |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 130 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 131 | Basic API Usage |
| 132 | --------------- |
| 133 | |
Armin Ronacher | 4dcc237 | 2008-05-26 13:59:26 +0200 | [diff] [blame] | 134 | This section gives you a brief introduction to the Python API for Jinja2 |
| 135 | templates. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 136 | |
| 137 | The most basic way to create a template and render it is through |
Armin Ronacher | 4dcc237 | 2008-05-26 13:59:26 +0200 | [diff] [blame] | 138 | :class:`~jinja2.Template`. This however is not the recommended way to |
| 139 | work with it if your templates are not loaded from strings but the file |
| 140 | system or another data source: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 141 | |
| 142 | >>> from jinja2 import Template |
| 143 | >>> template = Template('Hello {{ name }}!') |
| 144 | >>> template.render(name='John Doe') |
| 145 | u'Hello John Doe!' |
| 146 | |
Armin Ronacher | 4dcc237 | 2008-05-26 13:59:26 +0200 | [diff] [blame] | 147 | By creating an instance of :class:`~jinja2.Template` you get back a new template |
| 148 | object that provides a method called :meth:`~jinja2.Template.render` which when |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 149 | called with a dict or keyword arguments expands the template. The dict |
| 150 | or keywords arguments passed to the template are the so-called "context" |
| 151 | of the template. |
Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 152 | |
| 153 | What you can see here is that Jinja2 is using unicode internally and the |
| 154 | return value is an unicode string. So make sure that your application is |
| 155 | indeed using unicode internally. |