faq.rst 7.8 KB

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