Django template parsing order

Is the parsing order for Django templates specified somewhere in the Django documentation?

Based on the documentation for writing custom template tags and APIs, it seems Django uses depth traversal to build a node tree. However, I cannot find any expression in the Django documentation that guarantees this. In particular, I would like to know if the node tree is fully assembled before rendering is done.

Also, how does this affect template inheritance? Are the tags {% block ... %}in the derived template parsed at their location in the base template, or are they parsed by the tag {% extends "..." %}before the base template?

+3
source share
1 answer

The answer is yes, all nodes are created when the Template object is created. But magic happens when you call the rendering method of this object. This Template object has a NodeList with it, which is displayed in context. This rendering is, as you said earlier, first depth, and it just gets the intern (html) child strings attached to Node's father. Here is the NodeList class where the node is rendered. So, the root node is the one that adds all the nodes attached to it in order to finally generate the file. And if you remember, the extends template tag should be the first in the template, so it becomes the root node, which receives all the nodes already processed in it.

, ? node? , , ExtendsNode, extends. node ( ) , (, ). , node , , node, . , , , , .

, , .

, , Token Parser Django.

, .

+2

Source: https://habr.com/ru/post/1794826/


All Articles