Skip to content
Myr

faq.rst

This file

Path

docs/faq.rst

155 lines · 6.7 KiB

Last changed by

updated documentationArmin Ronacherbefore adoption3ef2043

History of this file

Take it

all calls are proxied by context.call now so that we can inject environment and context as first arguments. This slows calls down a bit but is a lot more user friendly. Added first draft of FAQArmin Ronacherbefore adoptionfd31049

lines 1–20why, one step back
1Frequently Asked Questions
2==========================34This page answers some of the often asked questions about Jinja.56.. highlight:: html+jinja78Why is it called Jinja?9-----------------------1011The name Jinja was chosen because it's the name of a Japanese temple and12temple and template share a similar pronunciation. It is not named after13the capital city of Uganda.1415How fast is it?16---------------1718We really hate benchmarks especially since they don't reflect much. The19performance of a template depends on many factors and you would have to20benchmark different engines in different situations. The benchmarks from the

updated FAQ for performance numbers. I guess I should remove that point anyways.Armin Ronacherbefore adoption5dfbfc1

lines 21–22why, one step back
21testsuite show that Jinja2 has a similar performance to `Mako`_ and is between
2210 and 20 times faster than Django's template engine or Genshi. These numbers

even more tests, fixed severe bug with autoescaping.Armin Ronacherbefore adoption5411ce7

line 23why, one step back
23should be taken with tons of salt as the benchmarks that took these numbers

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

lines 24–27why, one step back
24only test a few performance related situations such as looping. Generally
25speaking the performance of a template engine doesn't matter much as the26usual bottleneck in a web application is either the database or the application27code.

all calls are proxied by context.call now so that we can inject environment and context as first arguments. This slows calls down a bit but is a lot more user friendly. Added first draft of FAQArmin Ronacherbefore adoptionfd31049

lines 28–126why, one step back
28
29.. _Mako: http://www.makotemplates.org/3031How Compatible is Jinja2 with Django?32-------------------------------------3334The default syntax of Jinja2 matches Django syntax in many ways. However35this similarity doesn't mean that you can use a Django template unmodified36in Jinja2. For example filter arguments use a function call syntax rather37than a colon to separate filter name and arguments. Additionally the38extension interface in Jinja is fundamentally different from the Django one39which means that your custom tags won't work any longer.4041Generally speaking you will use much less custom extensions as the Jinja42template system allows you to use a certain subset of Python expressions43which can replace most Django extensions. For example instead of using44something like this::4546 {% load comments %}47 {% get_latest_comments 10 as latest_comments %}48 {% for comment in latest_comments %}49 ...50 {% endfor %}5152You will most likely provide an object with attributes to retrieve53comments from the database::5455 {% for comment in models.comments.latest(10) %}56 ...57 {% endfor %}5859Or directly provide the model for quick testing::6061 {% for comment in Comment.objects.order_by('-pub_date')[:10] %}62 ...63 {% endfor %}6465Please keep in mind that even though you may put such things into templates66it still isn't a good idea. Queries should go into the view code and now67the template!6869Isn't it a terrible idea to put Logic into Templates?70-----------------------------------------------------7172Without a doubt you should try to remove as much logic from templates as73possible. But templates without any logic mean that you have to do all74the processing in the code which is boring and stupid. A template engine75that does that is shipped with Python and called `string.Template`. Comes76without loops and if conditions and is by far the fastest template engine77you can get for Python.7879So some amount of logic is required in templates to keep everyone happy.80And Jinja leaves it pretty much to you how much logic you want to put into81templates. There are some restrictions in what you can do and what not.8283Jinja2 neither allows you to put arbitrary Python code into templates nor84does it allow all Python expressions. The operators are limited to the85most common ones and more advanced expressions such as list comprehensions86and generator expressions are not supported. This keeps the template engine87easier to maintain and templates more readable.8889Why is Autoescaping not the Default?90------------------------------------9192There are multiple reasons why automatic escaping is not the default mode93and also not the recommended one. While automatic escaping of variables94means that you will less likely have an XSS problem it also causes a huge95amount of extra processing in the template engine which can cause serious96performance problems. As Python doesn't provide a way to mark strings as97unsafe Jinja has to hack around that limitation by providing a custom98string class (the :class:`Markup` string) that safely interacts with safe99and unsafe strings.100101With explicit escaping however the template engine doesn't have to perform102any safety checks on variables. Also a human knows not to escape integers103or strings that may never contain characters one has to escape or already104HTML markup. For example when iterating over a list over a table of105integers and floats for a table of statistics the template designer can106omit the escaping because he knows that integers or floats don't contain107any unsafe parameters.108109Additionally Jinja2 is a general purpose template engine and not only used110for HTML/XML generation. For example you may generate LaTeX, emails,111CSS, JavaScript, or configuration files.112113Why is the Context immutable?114-----------------------------115116When writing a :func:`contextfunction` or something similar you may have117noticed that the context tries to stop you from modifying it. If you have118managed to modify the context by using an internal context API you may119have noticed that changes in the context don't seem to be visible in the120template. The reason for this is that Jinja uses the context only as121primary data source for template variables for performance reasons.122123If you want to modify the context write a function that returns a variable124instead that one can assign to a variable by using set::125126 {% set comments = get_latest_comments() %}

even more tests, fixed severe bug with autoescaping.Armin Ronacherbefore adoption5411ce7

lines 127–130why, one step back
127
128I don't have the _speedups Module. Is Jinja slower now?129--------------------------------------------------------130

updated documentationArmin Ronacherbefore adoption3ef2043

lines 131–134why, one step back
131To achieve a good performance with automatic escaping enabled, the escaping
132function is also implemented in pure C and used if Jinja2 was installed with133the speedups module. This happens automatically if a C compiler is available134on the system during installation.

even more tests, fixed severe bug with autoescaping.Armin Ronacherbefore adoption5411ce7

lines 135–144why, one step back
135
136My tracebacks look weird. What's happening?137--------------------------------------------138139If the speedups module is not compiled and you are using a Python installation140without ctypes (Python 2.4 without ctypes, Jython or Google's AppEngine)141Jinja2 is unable to provide correct debugging information and the traceback142may be incomplete. There is currently no good workaround for Jython or143the AppEngine as ctypes is unavailable there and it's not possible to use144the speedups extension.

updated documentationArmin Ronacherbefore adoption3ef2043

lines 145–155why, one step back
145
146Why is there no Python 2.3 support?147-----------------------------------148149Python 2.3 is missing a lot of features that are used heavily in Jinja2. This150decision was made as with the upcoming Python 2.6 and 3.0 versions it becomes151harder to maintain the code for older Python versions. If you really need152Python 2.3 support you either have to use `Jinja 1`_ or other templating153engines that still support 2.3.154155.. _Jinja 1: http://jinja.pocoo.org/1/