faq.rst
This file
Path
docs/faq.rst
77 lines · 3.3 KiB
Last changed by
Merge branch 'stable'David Lordbefore adoption6aeab5d
History of this fileTake it
1Frequently Asked Questions2==========================345Why is it called Jinja?6-----------------------78"Jinja" is a Japanese `Shinto shrine`_, or temple, and temple and9template share a similar English pronunciation. It is not named after10the `city in Uganda`_.1112.. _Shinto shrine: https://en.wikipedia.org/wiki/Shinto_shrine13.. _city in Uganda: https://en.wikipedia.org/wiki/Jinja%2C_Uganda141516How fast is Jinja?17------------------1819Jinja is relatively fast among template engines because it compiles and20caches template code to Python code, so that the template does not need21to be parsed and interpreted each time. Rendering a template becomes as22close to executing a Python function as possible.2324Jinja also makes extensive use of caching. Templates are cached by name25after loading, so future uses of the template avoid loading. The26template loading itself uses a bytecode cache to avoid repeated27compiling. The caches can be external to persist across restarts.28Templates can also be precompiled and loaded as fast Python imports.2930We dislike benchmarks because they don't reflect real use. Performance31depends on many factors. Different engines have different default32configurations and tradeoffs that make it unclear how to set up a useful33comparison. Often, database access, API calls, and data processing have34a much larger effect on performance than the template engine.353637Isn't it a bad idea to put logic in templates?38----------------------------------------------3940Without a doubt you should try to remove as much logic from templates as41possible. With less logic, the template is easier to understand, has42fewer potential side effects, and is faster to compile and render. But a43template without any logic means processing must be done in code before44rendering. A template engine that does that is shipped with Python,45called :class:`string.Template`, and while it's definitely fast it's not46convenient.4748Jinja's features such as blocks, statements, filters, and function calls49make it much easier to write expressive templates, with very few50restrictions. Jinja doesn't allow arbitrary Python code in templates, or51every feature available in the Python language. This keeps the engine52easier to maintain, and keeps templates more readable.5354Some amount of logic is required in templates to keep everyone happy.55Too much logic in the template can make it complex to reason about and56maintain. It's up to you to decide how your application will work and57balance how much logic you want to put in the template.585960Why is HTML escaping not the default?61-------------------------------------6263Jinja provides a feature that can be enabled to escape HTML syntax in64rendered templates. However, it is disabled by default.6566Jinja is a general purpose template engine, it is not only used for HTML67documents. You can generate plain text, LaTeX, emails, CSS, JavaScript,68configuration files, etc. HTML escaping wouldn't make sense for any of69these document types.7071While automatic escaping means that you are less likely have an XSS72problem, it also requires significant extra processing during compiling73and rendering, which can reduce performance. Jinja uses `MarkupSafe`_ for74escaping, which provides optimized C code for speed, but it still75introduces overhead to track escaping across methods and formatting.7677.. _MarkupSafe: https://markupsafe.palletsprojects.com/