Skip to content
Myr

faq.rst

This file

Path

docs/faq.rst

75 lines · 3.2 KiB

Last changed by

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

History of this file

Take it

The walk stopped after 200 commits: lines older than 5d33f67 point at it.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

lines 1–7why, one step back
1Frequently Asked Questions
2==========================345Why is it called Jinja?6-----------------------7

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 8–10why, one step back
8"Jinja" is a Japanese `Shinto shrine`_, or temple, and temple and
9template share a similar English pronunciation. It is not named after10the `city in Uganda`_.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 11why, one step back
11

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 12–13why, one step back
12.. _Shinto shrine: https://en.wikipedia.org/wiki/Shinto_shrine
13.. _city in Uganda: https://en.wikipedia.org/wiki/Jinja%2C_Uganda

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

lines 14–15why, one step back
14
15

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 16–17why, one step back
16How fast is Jinja?
17------------------

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 18why, one step back
18

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 19–22why, one step back
19Jinja is relatively fast among template engines because it compiles and
20caches 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.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 23why, one step back
23

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 24–28why, one step back
24Jinja also makes extensive use of caching. Templates are cached by name
25after 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.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 29why, one step back
29

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 30–34why, one step back
30We dislike benchmarks because they don't reflect real use. Performance
31depends 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.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

lines 35–36why, one step back
35
36

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 37–38why, one step back
37Isn't it a bad idea to put logic in templates?
38----------------------------------------------

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

lines 39–40why, one step back
39
40Without a doubt you should try to remove as much logic from templates as

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 41–46why, one step back
41possible. With less logic, the template is easier to understand, has
42fewer 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.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 47why, one step back
47

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 48–52why, one step back
48Jinja's features such as blocks, statements, filters, and function calls
49make 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.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 53why, one step back
53

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 54–57why, one step back
54Some 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.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

lines 58–59why, one step back
58
59

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 60why, one step back
60Why is HTML escaping not the default?

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

lines 61–62why, one step back
61-------------------------------------
62

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 63–64why, one step back
63Jinja provides a feature that can be enabled to escape HTML syntax in
64rendered templates. However, it is disabled by default.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 65why, one step back
65

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 66–69why, one step back
66Jinja is a general purpose template engine, it is not only used for HTML
67documents. You can generate plain text, LaTeX, emails, CSS, JavaScript,68configuration files, etc. HTML escaping wouldn't make sense for any of69these document types.

Merge pull request #1105 from areebbeigh/issue-1102David Lordbefore adoption5d33f67

line 70why, one step back
70

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 71–75why, one step back
71While automatic escaping means that you are less likely have an XSS
72problem, 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.