Skip to content
Myr

intro.rst

This file

Path

docs/intro.rst

151 lines · 5.2 KiB

Last changed by

Applied documentation patches by Clemens Hermann.Armin Ronacherbefore adoption0aa0f58

History of this file

Take it

The walk stopped after 200 commits: lines older than c0d40d3 point at it.

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 1–15why, one step back
1Introduction
2============34This 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.78If you have any exposure to other text-based template languages, such as Smarty or9Django, you should feel right at home with Jinja2. It's both designer and10developer friendly by sticking to Python's principles and adding functionality11useful for templating environments.1213The key-features are...1415- ... **configurable syntax**. If you are generating LaTeX or other formats

some documentation updatesArmin Ronacherbefore adoptioned98cac

line 16why, one step back
16 with Jinja2 you can change the delimiters to something that integrates better

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 17–23why, one step back
17 into the LaTeX markup.
1819- ... **fast**. While performance is not the primarily target of Jinja2 it's20 surprisingly fast. The overhead compared to regular Python code was reduced21 to the very minimum.2223- ... **easy to debug**. Jinja2 integrates directly into the python traceback

some documentation updatesArmin Ronacherbefore adoptioned98cac

line 24why, one step back
24 system which allows you to debug Jinja2 templates with regular python

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 25–36why, one step back
25 debugging helpers.
2627- ... **secure**. It's possible to evaluate untrusted template code if the28 optional sandbox is enabled. This allows Jinja2 to be used as templating29 language for applications where users may modify the template design.303132Prerequisites33-------------3435Jinja2 needs at least **Python 2.4** to run. Additionally a working C-compiler36that can create python extensions should be installed for the debugger. If no

fixed a bug in error reporting and some small documentation updatesArmin Ronacherbefore adoption61a5a24

lines 37–38why, one step back
37C-compiler is available and you are using Python 2.4 the `ctypes`_ module
38should be installed.

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

line 39why, one step back
39

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

lines 40–46why, one step back
40If you don't have a working C compiler and you are trying to install the source
41release you will get a compiler error. This however can be circumvented by42passing the ``--without-speedups`` command line argument to the setup script.4344For more details about that have a look at the :ref:`disable-speedups`45section below.46

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 47–49why, one step back
47.. _ctypes: http://python.net/crew/theller/ctypes/
4849

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 50–58why, one step back
50Installation
51------------5253You have multiple ways to install Jinja2. If you are unsure what to do, go54with the Python egg or tarball.5556As a Python egg (via easy_install)57~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~58

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

line 59why, one step back
59You can install the most recent Jinja2 version using `easy_install`_ or `pip`_::

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 60–61why, one step back
60
61 sudo easy_install Jinja2

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

line 62why, one step back
62 sudo pip install Jinja2

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 63–66why, one step back
63
64This will install a Jinja2 egg in your Python installation's site-packages65directory.66

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

lines 67–69why, one step back
67(If you are installing from the windows command line omit the `sudo` and make
68sure to run the command as user with administrator rights)69

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 70–86why, one step back
70From the tarball release
71~~~~~~~~~~~~~~~~~~~~~~~~~72731. Download the most recent tarball from the `download page`_742. Unpack the tarball753. ``sudo python setup.py install``7677Note that the last command will automatically download and install78`setuptools`_ if you don't already have it installed. This requires a working79internet connection.8081This will install Jinja2 into your Python installation's site-packages directory.8283Installing the development version84~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~85861. Install `mercurial`_

fixed an embarrassing mistake in the documentationArmin Ronacherbefore adoptionbbbe062

line 87why, one step back
872. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2``

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 88–99why, one step back
883. ``cd jinja2``
894. ``ln -s jinja2 /usr/lib/python2.X/site-packages``9091As an alternative to steps 4 you can also do ``python setup.py develop``92which will install the package via setuptools in development mode. This also93has the advantage that the C extensions are compiled.9495Alternative you can use `easy_install`_ to install the current development96snapshot::9798 sudo easy_install Jinja2==dev99

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

lines 100–103why, one step back
100Or the new `pip`_ command::
101102 sudo pip install Jinja2==dev103

Applied documentation patches by Clemens Hermann.Armin Ronacherbefore adoption0aa0f58

line 104why, one step back
104.. _download page: http://pypi.python.org/pypi/Jinja2

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 105–106why, one step back
105.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools
106.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

line 107why, one step back
107.. _pip: http://pypi.python.org/pypi/pip

some documentation updatesArmin Ronacherbefore adoptioned98cac

line 108why, one step back
108.. _mercurial: http://www.selenic.com/mercurial/

Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107

lines 109–124why, one step back
109
110.. _disable-speedups:111112Disable the speedups Module113~~~~~~~~~~~~~~~~~~~~~~~~~~~114115By default Jinja2 will try to compile the speedups module. This of course116will fail if you don't have the Python headers or a working compiler. This117is often the case if you are installing Jinja2 from a windows machine.118119You can disable the speedups extension when installing using the120``--without-speedups`` flag::121122 sudo python setup.py install --without-speedups123124You can also pass this parameter to `easy_install` or `pip`.

some documentation updatesArmin Ronacherbefore adoptioned98cac

lines 125–126why, one step back
125
126

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 127–129why, one step back
127Basic API Usage
128---------------129

fixed linking in jinja docs for the introArmin Ronacherbefore adoption4dcc237

lines 130–131why, one step back
130This section gives you a brief introduction to the Python API for Jinja2
131templates.

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 132–133why, one step back
132
133The most basic way to create a template and render it is through

fixed linking in jinja docs for the introArmin Ronacherbefore adoption4dcc237

lines 134–136why, one step back
134:class:`~jinja2.Template`. This however is not the recommended way to
135work with it if your templates are not loaded from strings but the file136system or another data source:

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 137–142why, one step back
137
138>>> from jinja2 import Template139>>> template = Template('Hello {{ name }}!')140>>> template.render(name='John Doe')141u'Hello John Doe!'142

fixed linking in jinja docs for the introArmin Ronacherbefore adoption4dcc237

lines 143–144why, one step back
143By creating an instance of :class:`~jinja2.Template` you get back a new template
144object that provides a method called :meth:`~jinja2.Template.render` which when

using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3

lines 145–147why, one step back
145called with a dict or keyword arguments expands the template. The dict
146or keywords arguments passed to the template are the so-called "context"147of the template.

fixed a bug in error reporting and some small documentation updatesArmin Ronacherbefore adoption61a5a24

lines 148–151why, one step back
148
149What you can see here is that Jinja2 is using unicode internally and the150return value is an unicode string. So make sure that your application is151indeed using unicode internally.