Debian Codegraph @

docs/faq.rst

raw
1 Frequently Asked Questions
2 ==========================
3
4
5 Why is it called Jinja?
6 -----------------------
7
8 "Jinja" is a Japanese `Shinto shrine`_, or temple, and temple and
9 template share a similar English pronunciation. It is not named after
10 the `city in Uganda`_.
11
12 .. _Shinto shrine: https://en.wikipedia.org/wiki/Shinto_shrine
13 .. _city in Uganda: https://en.wikipedia.org/wiki/Jinja%2C_Uganda
14
15
16 How fast is Jinja?
17 ------------------
18
19 Jinja is relatively fast among template engines because it compiles and
20 caches template code to Python code, so that the template does not need
21 to be parsed and interpreted each time. Rendering a template becomes as
22 close to executing a Python function as possible.
23
24 Jinja also makes extensive use of caching. Templates are cached by name
25 after loading, so future uses of the template avoid loading. The
26 template loading itself uses a bytecode cache to avoid repeated
27 compiling. The caches can be external to persist across restarts.
28 Templates can also be precompiled and loaded as fast Python imports.
29
30 We dislike benchmarks because they don't reflect real use. Performance
31 depends on many factors. Different engines have different default
32 configurations and tradeoffs that make it unclear how to set up a useful
33 comparison. Often, database access, API calls, and data processing have
34 a much larger effect on performance than the template engine.
35
36
37 Isn't it a bad idea to put logic in templates?
38 ----------------------------------------------
39
40 Without a doubt you should try to remove as much logic from templates as
41 possible. With less logic, the template is easier to understand, has
42 fewer potential side effects, and is faster to compile and render. But a
43 template without any logic means processing must be done in code before
44 rendering. A template engine that does that is shipped with Python,
45 called :class:`string.Template`, and while it's definitely fast it's not
46 convenient.
47
48 Jinja's features such as blocks, statements, filters, and function calls
49 make it much easier to write expressive templates, with very few
50 restrictions. Jinja doesn't allow arbitrary Python code in templates, or
51 every feature available in the Python language. This keeps the engine
52 easier to maintain, and keeps templates more readable.
53
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 and
56 maintain. It's up to you to decide how your application will work and
57 balance how much logic you want to put in the template.
58
59
60 Why is HTML escaping not the default?
61 -------------------------------------
62
63 Jinja provides a feature that can be enabled to escape HTML syntax in
64 rendered templates. However, it is disabled by default.
65
66 Jinja is a general purpose template engine, it is not only used for HTML
67 documents. You can generate plain text, LaTeX, emails, CSS, JavaScript,
68 configuration files, etc. HTML escaping wouldn't make sense for any of
69 these document types.
70
71 While automatic escaping means that you are less likely have an XSS
72 problem, it also requires significant extra processing during compiling
73 and rendering, which can reduce performance. Jinja uses `MarkupSafe`_ for
74 escaping, which provides optimized C code for speed, but it still
75 introduces overhead to track escaping across methods and formatting.
76
77 .. _MarkupSafe: https://markupsafe.palletsprojects.com/