faq.rst
This file
Path
docs/faq.rst
195 lines · 8.2 KiB
Last changed by
Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7
History of this fileTake it
The walk stopped after 200 commits: lines older than 352334c point at it.
2.10-devArmin Ronacherbefore adoption352334c
lines 1–20why, one step back1Frequently Asked Questions2==========================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 city in 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 theMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 21why, one step back21testsuite show that Jinja has a similar performance to `Mako`_ and is between2.10-devArmin Ronacherbefore adoption352334c
lines 22–28why, one step back2210 and 20 times faster than Django's template engine or Genshi. These numbers23should be taken with tons of salt as the benchmarks that took these numbers24only test a few performance related situations such as looping. Generally25speaking the performance of a template engine doesn't matter much as the26usual bottleneck in a web application is either the database or the application27code.28Merge branch '2.10.x'David Lordbefore adoption9550dc8
line 29why, one step back29.. _Mako: https://www.makotemplates.org/2.10-devArmin Ronacherbefore adoption352334c
line 30why, one step back30Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 31–32why, one step back31How Compatible is Jinja with Django?32------------------------------------2.10-devArmin Ronacherbefore adoption352334c
line 33why, one step back33Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 34why, one step back34The default syntax of Jinja matches Django syntax in many ways. However2.10-devArmin Ronacherbefore adoption352334c
line 35why, one step back35this similarity doesn't mean that you can use a Django template unmodifiedMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 36why, one step back36in Jinja. For example filter arguments use a function call syntax rather2.10-devArmin Ronacherbefore adoption352334c
lines 37–82why, one step back37than 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 not67the 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.82Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 83why, one step back83Jinja neither allows you to put arbitrary Python code into templates nor2.10-devArmin Ronacherbefore adoption352334c
lines 84–108why, one step back84does 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.108Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 109why, one step back109Additionally Jinja is a general purpose template engine and not only used2.10-devArmin Ronacherbefore adoption352334c
lines 110–132why, one step back110for 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() %}127128My tracebacks look weird. What's happening?129--------------------------------------------130131If the debugsupport module is not compiled and you are using a Python132installation without ctypes (Python 2.4 without ctypes, Jython or Google'sMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 133why, one step back133AppEngine) Jinja is unable to provide correct debugging information and2.10-devArmin Ronacherbefore adoption352334c
lines 134–147why, one step back134the traceback may be incomplete. There is currently no good workaround135for Jython or the AppEngine as ctypes is unavailable there and it's not136possible to use the debugsupport extension.137138If you are working in the Google AppEngine development server you can139whitelist the ctypes module to restore the tracebacks. This however won't140work in production environments::141142 import os143 if os.environ.get('SERVER_SOFTWARE', '').startswith('Dev'):144 from google.appengine.tools.devappserver2.python import sandbox145 sandbox._WHITE_LIST_C_MODULES += ['_ctypes', 'gestalt']146147Credit for this snippet goes to `Thomas JohanssonMerge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7
line 148why, one step back148<https://stackoverflow.com/questions/3086091/debug-jinja2-in-google-app-engine/3694434#3694434>`_2.10-devArmin Ronacherbefore adoption352334c
line 149why, one step back149Merge pull request #868 from The-Compiler/py33David Lordbefore adoptionabf6089
lines 150–151why, one step back150Why is there no Python 2.3/2.4/2.5/2.6/3.1/3.2/3.3 support?151-----------------------------------------------------------2.10-devArmin Ronacherbefore adoption352334c
line 152why, one step back152Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 153why, one step back153Python 2.3 is missing a lot of features that are used heavily in Jinja. This2.10-devArmin Ronacherbefore adoption352334c
lines 154–155why, one step back154decision was made as with the upcoming Python 2.6 and 3.0 versions it becomes155harder to maintain the code for older Python versions. If you really needMerge branch '2.10.x'David Lordbefore adoption13cc9f4
line 156why, one step back156Python 2.3 support you either have to use Jinja 1 or other templating2.10-devArmin Ronacherbefore adoption352334c
lines 157–162why, one step back157engines that still support 2.3.158159Python 2.4/2.5/3.1/3.2 support was removed when we switched to supporting160Python 2 and 3 by the same sourcecode (without using 2to3). It was required to161drop support because only Python 2.6/2.7 and >=3.3 support byte and unicode162literals in a way compatible to each other version. If you really need supportMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 163why, one step back163for older Python 2 (or 3) versions, you can just use Jinja 2.6.Merge pull request #868 from The-Compiler/py33David Lordbefore adoptionabf6089
lines 164–166why, one step back164165Python 2.6/3.3 support was dropped because it got dropped in various upstream166projects (such as wheel or pytest), which would make it difficult to continueMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 167why, one step back167supporting it. Jinja 2.10 was the last version supporting Python 2.6/3.3.2.10-devArmin Ronacherbefore adoption352334c
lines 168–188why, one step back168169My Macros are overridden by something170-------------------------------------171172In some situations the Jinja scoping appears arbitrary:173174layout.tmpl:175176.. sourcecode:: jinja177178 {% macro foo() %}LAYOUT{% endmacro %}179 {% block body %}{% endblock %}180181child.tmpl:182183.. sourcecode:: jinja184185 {% extends 'layout.tmpl' %}186 {% macro foo() %}CHILD{% endmacro %}187 {% block body %}{{ foo() }}{% endblock %}188Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 189why, one step back189This will print ``LAYOUT`` in Jinja. This is a side effect of having2.10-devArmin Ronacherbefore adoption352334c
lines 190–194why, one step back190the parent template evaluated after the child one. This allows child191templates passing information to the parent template. To avoid this192issue rename the macro or variable in the parent template to have an193uncommon prefix.194Merge branch '2.10.x'David Lordbefore adoption9550dc8
line 195why, one step back195.. _Jinja 1: https://pypi.org/project/Jinja/