Skip to content
Myr

intro.rst

This file

Path

docs/intro.rst

170 lines · 5.8 KiB

Last changed by

Added Python 3 section to the documentation.Armin Ronacherbefore adoptioneb43b12

History of this file

Take it

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

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 1–39why, 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 formats16 with Jinja2 you can change the delimiters to something that integrates better17 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 traceback24 system which allows you to debug Jinja2 templates with regular python25 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 no37C-compiler is available and you are using Python 2.4 the `ctypes`_ module38should be installed.39

Some tiny documentation fixes (unicode -> Unicode)Armin Ronacherbefore adoption656d5e7

line 40why, one step back
40If you don't have a working C-compiler and you are trying to install the source

Disabled speedups by default.Armin Ronacherbefore adoption9ebc457

lines 41–43why, one step back
41release with the speedups you will get a compiler error. This however can be
42circumvented by passing the ``--without-speedups`` command line argument to the43setup script::

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

line 44why, one step back
44

Fixed the install docs.Armin Ronacherbefore adoption237f01b

line 45why, one step back
45 $ python setup.py --with-speedups install

(codename Kong, released on September 12th 2009)Armin Ronacherbefore adoption86b5cb5

line 46why, one step back
46

Disabled speedups by default.Armin Ronacherbefore adoption9ebc457

lines 47–48why, one step back
47(As of Jinja 2.2, the speedups are disabled by default and can be enabled
48with ``--with-speedups``. See :ref:`enable-speedups`)

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

line 49why, one step back
49

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 50–61why, one step back
50.. _ctypes: http://python.net/crew/theller/ctypes/
515253Installation54------------5556You have multiple ways to install Jinja2. If you are unsure what to do, go57with the Python egg or tarball.5859As a Python egg (via easy_install)60~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~61

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

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

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

line 63why, one step back
63

(codename Kong, released on September 12th 2009)Armin Ronacherbefore adoption86b5cb5

lines 64–65why, one step back
64 easy_install Jinja2
65 pip install Jinja2

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 66–69why, one step back
66
67This will install a Jinja2 egg in your Python installation's site-packages68directory.69

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

lines 70–72why, one step back
70(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

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 73–79why, one step back
73From the tarball release
74~~~~~~~~~~~~~~~~~~~~~~~~~75761. Download the most recent tarball from the `download page`_772. Unpack the tarball783. ``sudo python setup.py install``79

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

lines 80–81why, one step back
80Note that you either have to have setuptools or `distribute`_ installed,
81the latter is preferred.

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 82–84why, one step back
82
83This will install Jinja2 into your Python installation's site-packages directory.84

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

lines 85–86why, one step back
85.. _distribute: http://pypi.python.org/pypi/distribute
86

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 87–95why, one step back
87Installing the development version
88~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~89901. Install `mercurial`_912. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2``923. ``cd jinja2``934. ``ln -s jinja2 /usr/lib/python2.X/site-packages``9495As an alternative to steps 4 you can also do ``python setup.py develop``

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

line 96why, one step back
96which will install the package via distribute in development mode. This also

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 97–98why, one step back
97has the advantage that the C extensions are compiled.
98

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

line 99why, one step back
99Alternative you can use `pip`_ to install the current development

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 100–101why, one step back
100snapshot::
101

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

line 102why, one step back
102 sudo pip install Jinja2==dev

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

line 103why, one step back
103

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

line 104why, one step back
104Or the `easy_install`_ command::

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

line 105why, one step back
105

Documentation recommends distribute now.Armin Ronacherbefore adoption0045216

line 106why, one step back
106 sudo easy_install Jinja2==dev

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

line 107why, one step back
107

Applied documentation patches by Clemens Hermann.Armin Ronacherbefore adoption0aa0f58

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

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

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

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

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

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

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

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

line 113why, one step back
113

Disabled speedups by default.Armin Ronacherbefore adoption9ebc457

line 114why, one step back
114.. _enable-speedups:

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

line 115why, one step back
115

Fix typo.Georg Brandlbefore adoption79cc8d1

lines 116–117why, one step back
116Enable the speedups Module
117~~~~~~~~~~~~~~~~~~~~~~~~~~

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

line 118why, one step back
118

Disabled speedups by default.Armin Ronacherbefore adoption9ebc457

line 119why, one step back
119By default Jinja2 will not compile the speedups module. Enabling this

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

lines 120–122why, one step back
120will fail if you don't have the Python headers or a working compiler. This
121is often the case if you are installing Jinja2 from a windows machine.122

Disabled speedups by default.Armin Ronacherbefore adoption9ebc457

lines 123–124why, one step back
123You can enable the speedups extension when installing using the
124``--with-speedups`` flag::

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

line 125why, one step back
125

Fixed the install docs.Armin Ronacherbefore adoption237f01b

line 126why, one step back
126 sudo python setup.py --with-speedups install

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

line 127why, one step back
127

It's now possible to use ``{{ foo.0.0 }}``Armin Ronacherbefore adoptioncb1b97f

lines 128–154why, one step back
128
129130Basic API Usage131---------------132133This section gives you a brief introduction to the Python API for Jinja2134templates.135136The most basic way to create a template and render it is through137:class:`~jinja2.Template`. This however is not the recommended way to138work with it if your templates are not loaded from strings but the file139system or another data source:140141>>> from jinja2 import Template142>>> template = Template('Hello {{ name }}!')143>>> template.render(name='John Doe')144u'Hello John Doe!'145146By creating an instance of :class:`~jinja2.Template` you get back a new template147object that provides a method called :meth:`~jinja2.Template.render` which when148called with a dict or keyword arguments expands the template. The dict149or keywords arguments passed to the template are the so-called "context"150of the template.151152What you can see here is that Jinja2 is using unicode internally and the153return value is an unicode string. So make sure that your application is154indeed using unicode internally.

Added Python 3 section to the documentation.Armin Ronacherbefore adoptioneb43b12

lines 155–170why, one step back
155
156157Experimental Python 3 Support158-----------------------------159160Jinja 2.3 brings experimental support for Python 3. It means that all161unittests pass on the new version, but there might still be small bugs in162there and behavior might be inconsistent. If you notice any bugs, please163provide feedback in the `Jinja bug tracker`_.164165Also please keep in mind that the documentation is written with Python 2166in mind, you will have to adapt the shown code examples to Python 3 syntax167for yourself.168169170.. _Jinja bug tracker: http://dev.pocoo.org/projects/jinja/