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 fileTake 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 back1Introduction2============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 formatssome documentation updatesArmin Ronacherbefore adoptioned98cac
line 16why, one step back16 with Jinja2 you can change the delimiters to something that integrates betterusing a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
lines 17–23why, one step back17 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 tracebacksome documentation updatesArmin Ronacherbefore adoptioned98cac
line 24why, one step back24 system which allows you to debug Jinja2 templates with regular pythonusing a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
lines 25–36why, one step back25 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 nofixed a bug in error reporting and some small documentation updatesArmin Ronacherbefore adoption61a5a24
lines 37–38why, one step back37C-compiler is available and you are using Python 2.4 the `ctypes`_ module38should be installed.using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
line 39why, one step back39Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
lines 40–46why, one step back40If you don't have a working C compiler and you are trying to install the source41release 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.46using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
lines 47–49why, one step back47.. _ctypes: http://python.net/crew/theller/ctypes/4849some documentation updatesArmin Ronacherbefore adoptioned98cac
lines 50–58why, one step back50Installation51------------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~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~58Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
line 59why, one step back59You can install the most recent Jinja2 version using `easy_install`_ or `pip`_::some documentation updatesArmin Ronacherbefore adoptioned98cac
lines 60–61why, one step back6061 sudo easy_install Jinja2Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
line 62why, one step back62 sudo pip install Jinja2some documentation updatesArmin Ronacherbefore adoptioned98cac
lines 63–66why, one step back6364This will install a Jinja2 egg in your Python installation's site-packages65directory.66Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
lines 67–69why, one step back67(If you are installing from the windows command line omit the `sudo` and make68sure to run the command as user with administrator rights)69some documentation updatesArmin Ronacherbefore adoptioned98cac
lines 70–86why, one step back70From the tarball release71~~~~~~~~~~~~~~~~~~~~~~~~~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 back872. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2``some documentation updatesArmin Ronacherbefore adoptioned98cac
lines 88–99why, one step back883. ``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==dev99Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
lines 100–103why, one step back100Or the new `pip`_ command::101102 sudo pip install Jinja2==dev103Applied documentation patches by Clemens Hermann.Armin Ronacherbefore adoption0aa0f58
line 104why, one step back104.. _download page: http://pypi.python.org/pypi/Jinja2some documentation updatesArmin Ronacherbefore adoptioned98cac
lines 105–106why, one step back105.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools106.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstallUpdated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
line 107why, one step back107.. _pip: http://pypi.python.org/pypi/pipsome documentation updatesArmin Ronacherbefore adoptioned98cac
line 108why, one step back108.. _mercurial: http://www.selenic.com/mercurial/Updated documentation to reflect the new speedups installation behavior.Armin Ronacherbefore adoption56d0107
lines 109–124why, one step back109110.. _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 back125126using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
lines 127–129why, one step back127Basic API Usage128---------------129fixed linking in jinja docs for the introArmin Ronacherbefore adoption4dcc237
lines 130–131why, one step back130This section gives you a brief introduction to the Python API for Jinja2131templates.using a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
lines 132–133why, one step back132133The most basic way to create a template and render it is throughfixed linking in jinja docs for the introArmin Ronacherbefore adoption4dcc237
lines 134–136why, one step back134:class:`~jinja2.Template`. This however is not the recommended way to135work 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 back137138>>> from jinja2 import Template139>>> template = Template('Hello {{ name }}!')140>>> template.render(name='John Doe')141u'Hello John Doe!'142fixed linking in jinja docs for the introArmin Ronacherbefore adoption4dcc237
lines 143–144why, one step back143By creating an instance of :class:`~jinja2.Template` you get back a new template144object that provides a method called :meth:`~jinja2.Template.render` which whenusing a table for _speedups escapeMickaël Guérinbefore adoptionc0d40d3
lines 145–147why, one step back145called with a dict or keyword arguments expands the template. The dict146or 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 back148149What 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.