Sometimes the basic features for custom template tag creation aren't enough. Don't worry, Django gives you complete access to the internals required to build a template tag from the ground up.
The template system works in a two-step process: compiling and rendering. To define a custom template tag, you specify how the compilation works and how the rendering works. When Django compiles a template, it splits the raw template text into nodes. Each node is an instance of django.template.Node
and has a render()
method. A compiled template is, simply, a list of Node
objects.
When you call render()
on a compiled template object, the template calls render()
on each Node
in its node list, with the given context. The results are all concatenated together to form the output of the template. Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node
(the compilation function), and what the node's render()
method does...