clean up faq, move technical discussions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 9de99f8 · Browse files · 3 files·+78 added−138 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+78 added−138 removed
- other:+78 added−138 removed
Provenance
- Commit
a98d482- Tree
fde0fec- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
9de99f8
3 files changed+78 added−138 removed
modified:docs/api.rst+11 added-8 removed
408 unmodified lines409 410 .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs)411 412 413-.. admonition:: Implementation413+The context is immutable, it prevents modifications, and if it is414+modified somehow despite that those changes may not show up. For415+performance, Jinja does not use the context as data storage for, only as416+a primary data source. Variables that the template does not define are417+looked up in the context, but variables the template does define are418+stored locally.419 415- Context is immutable for the same reason Python's frame locals are416- immutable inside functions. Both Jinja and Python are not using the417- context / frame locals as data storage for variables but only as primary418- data source.420+Instead of modifying the context directly, a function should return421+a value that can be assigned to a variable within the template itself.422 420- When a template accesses a variable the template does not define, Jinja421- looks up the variable in the context, after that the variable is treated422- as if it was defined in the template.423+.. code-block:: jinja424 425+ {% set comments = get_latest_comments() %}426 427+428 .. _loaders:429 430 Loaders431 -------491 unmodified linesmodified:docs/faq.rst+50 added-130 removed
1 Frequently Asked Questions2 ==========================3 4-This page answers some of the often asked questions about Jinja.4 6-.. highlight:: html+jinja7-5 Why is it called Jinja?6 -----------------------7 11-The name Jinja was chosen because it's the name of a Japanese temple and12-temple and template share a similar pronunciation. It is not named after13-the city in Uganda.8+"Jinja" is a Japanese `Shinto shrine`_, or temple, and temple and9+template share a similar English pronunciation. It is not named after10+the `city in Uganda`_.11 15-How fast is it?16----------------12+.. _Shinto shrine: https://en.wikipedia.org/wiki/Shinto_shrine13+.. _city in Uganda: https://en.wikipedia.org/wiki/Jinja%2C_Uganda14 18-We really hate benchmarks especially since they don't reflect much. The19-performance of a template depends on many factors and you would have to20-benchmark different engines in different situations. The benchmarks from the21-testsuite show that Jinja has a similar performance to `Mako`_ and is between22-10 and 20 times faster than Django's template engine or Genshi. These numbers23-should be taken with tons of salt as the benchmarks that took these numbers24-only test a few performance related situations such as looping. Generally25-speaking the performance of a template engine doesn't matter much as the26-usual bottleneck in a web application is either the database or the application27-code.15 29-.. _Mako: https://www.makotemplates.org/16+How fast is Jinja?17+------------------18 31-How Compatible is Jinja with Django?32-------------------------------------19+Jinja is relatively fast among template engines because it compiles and20+caches template code to Python code, so that the template does not need21+to be parsed and interpreted each time. Rendering a template becomes as22+close to executing a Python function as possible.23 34-The default syntax of Jinja matches Django syntax in many ways. However35-this similarity doesn't mean that you can use a Django template unmodified36-in Jinja. For example filter arguments use a function call syntax rather37-than a colon to separate filter name and arguments. Additionally the38-extension interface in Jinja is fundamentally different from the Django one39-which means that your custom tags won't work any longer.24+Jinja also makes extensive use of caching. Templates are cached by name25+after loading, so future uses of the template avoid loading. The26+template loading itself uses a bytecode cache to avoid repeated27+compiling. The caches can be external to persist across restarts.28+Templates can also be precompiled and loaded as fast Python imports.29 41-Generally speaking you will use much less custom extensions as the Jinja42-template system allows you to use a certain subset of Python expressions43-which can replace most Django extensions. For example instead of using44-something like this::30+We dislike benchmarks because they don't reflect real use. Performance31+depends on many factors. Different engines have different default32+configurations and tradeoffs that make it unclear how to set up a useful33+comparison. Often, database access, API calls, and data processing have34+a much larger effect on performance than the template engine.35 46- {% load comments %}47- {% get_latest_comments 10 as latest_comments %}48- {% for comment in latest_comments %}49- ...50- {% endfor %}36 52-You will most likely provide an object with attributes to retrieve53-comments from the database::37+Isn't it a bad idea to put logic in templates?38+----------------------------------------------39 55- {% for comment in models.comments.latest(10) %}56- ...57- {% endfor %}58-59-Or directly provide the model for quick testing::60-61- {% for comment in Comment.objects.order_by('-pub_date')[:10] %}62- ...63- {% endfor %}64-65-Please keep in mind that even though you may put such things into templates66-it still isn't a good idea. Queries should go into the view code and not67-the template!68-69-Isn't it a terrible idea to put Logic into Templates?70------------------------------------------------------71-40 Without a doubt you should try to remove as much logic from templates as73-possible. But templates without any logic mean that you have to do all74-the processing in the code which is boring and stupid. A template engine75-that does that is shipped with Python and called `string.Template`. Comes76-without loops and if conditions and is by far the fastest template engine77-you can get for Python.41+possible. With less logic, the template is easier to understand, has42+fewer potential side effects, and is faster to compile and render. But a43+template without any logic means processing must be done in code before44+rendering. A template engine that does that is shipped with Python,45+called :class:`string.Template`, and while it's definitely fast it's not46+convenient.47 79-So some amount of logic is required in templates to keep everyone happy.80-And Jinja leaves it pretty much to you how much logic you want to put into81-templates. There are some restrictions in what you can do and what not.48+Jinja's features such as blocks, statements, filters, and function calls49+make it much easier to write expressive templates, with very few50+restrictions. Jinja doesn't allow arbitrary Python code in templates, or51+every feature available in the Python language. This keeps the engine52+easier to maintain, and keeps templates more readable.53 83-Jinja neither allows you to put arbitrary Python code into templates nor84-does it allow all Python expressions. The operators are limited to the85-most common ones and more advanced expressions such as list comprehensions86-and generator expressions are not supported. This keeps the template engine87-easier to maintain and templates more readable.54+Some amount of logic is required in templates to keep everyone happy.55+Too much logic in the template can make it complex to reason about and56+maintain. It's up to you to decide how your application will work and57+balance how much logic you want to put in the template.58 89-Why is Autoescaping not the Default?90-------------------------------------59 92-There are multiple reasons why automatic escaping is not the default mode93-and also not the recommended one. While automatic escaping of variables94-means that you will less likely have an XSS problem it also causes a huge95-amount of extra processing in the template engine which can cause serious96-performance problems. As Python doesn't provide a way to mark strings as97-unsafe Jinja has to hack around that limitation by providing a custom98-string class (the :class:`~markupsafe.Markup` string) that safely interacts with safe99-and unsafe strings.100-101-With explicit escaping however the template engine doesn't have to perform102-any safety checks on variables. Also a human knows not to escape integers103-or strings that may never contain characters one has to escape or already104-HTML markup. For example when iterating over a list over a table of105-integers and floats for a table of statistics the template designer can106-omit the escaping because he knows that integers or floats don't contain107-any unsafe parameters.108-109-Additionally Jinja is a general purpose template engine and not only used110-for HTML/XML generation. For example you may generate LaTeX, emails,111-CSS, JavaScript, or configuration files.112-113-Why is the Context immutable?114------------------------------115-116-When writing a :func:`pass_context` function, you may have noticed that117-the context tries to stop you from modifying it. If you have managed to118-modify the context by using an internal context API you may have noticed119-that changes in the context don't seem to be visible in the template.120-The reason for this is that Jinja uses the context only as primary data121-source for template variables for performance reasons.122-123-If you want to modify the context write a function that returns a variable124-instead that one can assign to a variable by using set::125-126- {% set comments = get_latest_comments() %}127-128-129-My Macros are overridden by something60+Why is HTML escaping not the default?61 -------------------------------------62 132-In some situations the Jinja scoping appears arbitrary:63+Jinja provides a feature that can be enabled to escape HTML syntax in64+rendered templates. However, it is disabled by default.65 134-layout.tmpl:66+Jinja is a general purpose template engine, it is not only used for HTML67+documents. You can generate plain text, LaTeX, emails, CSS, JavaScript,68+configuration files, etc. HTML escaping wouldn't make sense for any of69+these document types.70 136-.. sourcecode:: jinja137-138- {% macro foo() %}LAYOUT{% endmacro %}139- {% block body %}{% endblock %}140-141-child.tmpl:142-143-.. sourcecode:: jinja144-145- {% extends 'layout.tmpl' %}146- {% macro foo() %}CHILD{% endmacro %}147- {% block body %}{{ foo() }}{% endblock %}148-149-This will print ``LAYOUT`` in Jinja. This is a side effect of having150-the parent template evaluated after the child one. This allows child151-templates passing information to the parent template. To avoid this152-issue rename the macro or variable in the parent template to have an153-uncommon prefix.154-155-.. _Jinja 1: https://pypi.org/project/Jinja/71+While automatic escaping means that you are less likely have an XSS72+problem, it also requires significant extra processing during compiling73+and rendering, which can reduce performance. Jinja uses MarkupSafe for74+escaping, which provides optimized C code for speed, but it still75+introduces overhead to track escaping across methods and formatting.modified:docs/templates.rst+17 added
936 unmodified lines937 938 If a macro name starts with an underscore, it's not exported and can't939 be imported.940 941+Due to how scopes work in Jinja, a macro in a child template does not942+override a macro in a parent template. The following will output943+"LAYOUT", not "CHILD".944 945+.. code-block:: jinja946+ :caption: ``layout.txt``947+948+ {% macro foo() %}LAYOUT{% endmacro %}949+ {% block body %}{% endblock %}950+951+.. code-block:: jinja952+ :caption: ``child.txt``953+954+ {% extends 'layout.txt' %}955+ {% macro foo() %}CHILD{% endmacro %}956+ {% block body %}{{ foo() }}{% endblock %}957+958+959 .. _call:960 961 Call962 ~~~~984 unmodified lines