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
1Introduction2============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.3940If 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.4647.. _ctypes: http://python.net/crew/theller/ctypes/484950Installation51------------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~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5859You can install the most recent Jinja2 version using `easy_install`_ or `pip`_::6061 sudo easy_install Jinja262 sudo pip install Jinja26364This will install a Jinja2 egg in your Python installation's site-packages65directory.6667(If you are installing from the windows command line omit the `sudo` and make68sure to run the command as user with administrator rights)6970From 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`_872. ``hg clone http://dev.pocoo.org/hg/jinja2-main jinja2``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==dev99100Or the new `pip`_ command::101102 sudo pip install Jinja2==dev103104.. _download page: http://pypi.python.org/pypi/Jinja2105.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools106.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall107.. _pip: http://pypi.python.org/pypi/pip108.. _mercurial: http://www.selenic.com/mercurial/109110.. _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`.125126127Basic API Usage128---------------129130This section gives you a brief introduction to the Python API for Jinja2131templates.132133The most basic way to create a template and render it is through134: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:137138>>> from jinja2 import Template139>>> template = Template('Hello {{ name }}!')140>>> template.render(name='John Doe')141u'Hello John Doe!'142143By creating an instance of :class:`~jinja2.Template` you get back a new template144object that provides a method called :meth:`~jinja2.Template.render` which when145called 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.148149What 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.