blob: 89186b1e284674d512afd6680570dde058fc9653 [file] [log] [blame]
Armin Ronacherfd310492008-05-25 00:16:51 +02001Frequently Asked Questions
2==========================
3
4This page answers some of the often asked questions about Jinja.
5
6.. highlight:: html+jinja
7
Armin Ronacherfd310492008-05-25 00:16:51 +02008Why is it called Jinja?
9-----------------------
10
11The name Jinja was chosen because it's the name of a Japanese temple and
12temple and template share a similar pronunciation. It is not named after
13the capital city of Uganda.
14
15How fast is it?
16---------------
17
18We really hate benchmarks especially since they don't reflect much. The
19performance of a template depends on many factors and you would have to
20benchmark different engines in different situations. The benchmarks from the
Armin Ronacher5dfbfc12008-05-25 18:10:12 +020021testsuite 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
Armin Ronacher5411ce72008-05-25 11:36:22 +020023should be taken with tons of salt as the benchmarks that took these numbers
Armin Ronacher61a5a242008-05-26 12:07:44 +020024only test a few performance related situations such as looping. Generally
25speaking the performance of a template engine doesn't matter much as the
26usual bottleneck in a web application is either the database or the application
27code.
Armin Ronacherfd310492008-05-25 00:16:51 +020028
29.. _Mako: http://www.makotemplates.org/
30
31How Compatible is Jinja2 with Django?
32-------------------------------------
33
34The default syntax of Jinja2 matches Django syntax in many ways. However
35this similarity doesn't mean that you can use a Django template unmodified
36in Jinja2. For example filter arguments use a function call syntax rather
37than a colon to separate filter name and arguments. Additionally the
38extension interface in Jinja is fundamentally different from the Django one
39which means that your custom tags won't work any longer.
40
41Generally speaking you will use much less custom extensions as the Jinja
42template system allows you to use a certain subset of Python expressions
43which can replace most Django extensions. For example instead of using
44something like this::
45
46 {% load comments %}
47 {% get_latest_comments 10 as latest_comments %}
48 {% for comment in latest_comments %}
49 ...
50 {% endfor %}
51
52You will most likely provide an object with attributes to retrieve
53comments from the database::
54
55 {% for comment in models.comments.latest(10) %}
56 ...
57 {% endfor %}
58
59Or directly provide the model for quick testing::
60
61 {% for comment in Comment.objects.order_by('-pub_date')[:10] %}
62 ...
63 {% endfor %}
64
65Please keep in mind that even though you may put such things into templates
Christopher Denter604b8462009-04-28 01:50:32 +020066it still isn't a good idea. Queries should go into the view code and not
Armin Ronacherfd310492008-05-25 00:16:51 +020067the template!
68
69Isn't it a terrible idea to put Logic into Templates?
70-----------------------------------------------------
71
72Without a doubt you should try to remove as much logic from templates as
73possible. But templates without any logic mean that you have to do all
74the processing in the code which is boring and stupid. A template engine
75that does that is shipped with Python and called `string.Template`. Comes
76without loops and if conditions and is by far the fastest template engine
77you can get for Python.
78
79So 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 into
81templates. There are some restrictions in what you can do and what not.
82
83Jinja2 neither allows you to put arbitrary Python code into templates nor
84does it allow all Python expressions. The operators are limited to the
85most common ones and more advanced expressions such as list comprehensions
86and generator expressions are not supported. This keeps the template engine
87easier to maintain and templates more readable.
88
89Why is Autoescaping not the Default?
90------------------------------------
91
92There are multiple reasons why automatic escaping is not the default mode
93and also not the recommended one. While automatic escaping of variables
94means that you will less likely have an XSS problem it also causes a huge
95amount of extra processing in the template engine which can cause serious
96performance problems. As Python doesn't provide a way to mark strings as
97unsafe Jinja has to hack around that limitation by providing a custom
98string class (the :class:`Markup` string) that safely interacts with safe
99and unsafe strings.
100
101With explicit escaping however the template engine doesn't have to perform
102any safety checks on variables. Also a human knows not to escape integers
103or strings that may never contain characters one has to escape or already
104HTML markup. For example when iterating over a list over a table of
105integers and floats for a table of statistics the template designer can
106omit the escaping because he knows that integers or floats don't contain
107any unsafe parameters.
108
109Additionally Jinja2 is a general purpose template engine and not only used
110for HTML/XML generation. For example you may generate LaTeX, emails,
111CSS, JavaScript, or configuration files.
112
113Why is the Context immutable?
114-----------------------------
115
116When writing a :func:`contextfunction` or something similar you may have
117noticed that the context tries to stop you from modifying it. If you have
118managed to modify the context by using an internal context API you may
119have noticed that changes in the context don't seem to be visible in the
120template. The reason for this is that Jinja uses the context only as
121primary data source for template variables for performance reasons.
122
123If you want to modify the context write a function that returns a variable
124instead that one can assign to a variable by using set::
125
126 {% set comments = get_latest_comments() %}
Armin Ronacher5411ce72008-05-25 11:36:22 +0200127
Armin Ronacher10c34da2010-08-17 12:10:27 +0200128What is the speedups module and why is it missing?
129--------------------------------------------------
Armin Ronacher5411ce72008-05-25 11:36:22 +0200130
Armin Ronacher3ef20432008-06-09 18:27:19 +0200131To achieve a good performance with automatic escaping enabled, the escaping
Armin Ronacher10c34da2010-08-17 12:10:27 +0200132function was also implemented in pure C in older Jinja2 releases and used if
133Jinja2 was installed with the speedups module.
134
135Because this feature itself is very useful for non-template engines as
136well it was moved into a separate project on PyPI called `MarkupSafe`_.
137
138Jinja2 no longer ships with a C implementation of it but only the pure
139Python implementation. It will however check if MarkupSafe is available
140and installed, and if it is, use the Markup class from MarkupSafe.
141
142So if you want the speedups, just import MarkupSafe.
143
144.. _MarkupSafe: http://pypi.python.org/pypi/MarkupSafe
Armin Ronacher5411ce72008-05-25 11:36:22 +0200145
146My tracebacks look weird. What's happening?
147--------------------------------------------
148
Armin Ronacher10c34da2010-08-17 12:10:27 +0200149If the debugsupport module is not compiled and you are using a Python
150installation without ctypes (Python 2.4 without ctypes, Jython or Google's
151AppEngine) Jinja2 is unable to provide correct debugging information and
152the traceback may be incomplete. There is currently no good workaround
153for Jython or the AppEngine as ctypes is unavailable there and it's not
154possible to use the debugsupport extension.
Armin Ronacher3ef20432008-06-09 18:27:19 +0200155
156Why is there no Python 2.3 support?
157-----------------------------------
158
159Python 2.3 is missing a lot of features that are used heavily in Jinja2. This
160decision was made as with the upcoming Python 2.6 and 3.0 versions it becomes
161harder to maintain the code for older Python versions. If you really need
162Python 2.3 support you either have to use `Jinja 1`_ or other templating
163engines that still support 2.3.
164
Armin Ronacher2f0d6592009-10-26 11:53:27 +0100165My Macros are overriden by something
166------------------------------------
Armin Ronacher2b488392009-09-18 19:32:46 +0200167
Armin Ronacher2f0d6592009-10-26 11:53:27 +0100168In some situations the Jinja scoping appears arbitrary:
Armin Ronacher2b488392009-09-18 19:32:46 +0200169
170layout.tmpl:
171
172.. sourcecode:: jinja
173
174 {% macro foo() %}LAYOUT{% endmacro %}
175 {% block body %}{% endblock %}
176
177child.tmpl:
178
179.. sourcecode:: jinja
180
181 {% extends 'layout.tmpl' %}
182 {% macro foo() %}CHILD{% endmacro %}
183 {% block body %}{{ foo() }}{% endblock %}
184
Armin Ronacher2f0d6592009-10-26 11:53:27 +0100185This will print ``LAYOUT`` in Jinja2. This is a side effect of having
186the parent template evaluated after the child one. This allows child
187templates passing information to the parent template. To avoid this
188issue rename the macro or variable in the parent template to have an
189uncommon prefix.
Armin Ronacher2b488392009-09-18 19:32:46 +0200190
Armin Ronacher3ef20432008-06-09 18:27:19 +0200191.. _Jinja 1: http://jinja.pocoo.org/1/