This is the Django template system.
How it works:
The Lexer.tokenize() method converts a template string (i.e., a string containing markup with custom template tags) to tokens, which can be either plain text (TokenType.TEXT), variables (TokenType.VAR), or block statements (TokenType.BLOCK).
The Parser() class takes a list of tokens in its constructor, and its parse() method returns a compiled template -- which is, under the hood, a list of Node objects.
Each Node is responsible for creating some sort of output -- e.g. simple text (TextNode), variable values in a given context (VariableNode), results of basic logic (IfNode), results of looping (ForNode), or anything else. The core Node types are TextNode, VariableNode, IfNode and ForNode, but plugin modules can define their own custom node types.
Each Node has a render() method, which takes a Context and returns a string of the rendered node. For example, the render() method of a Variable Node returns the variable's value as a string. The render() method of a ForNode returns the rendered output of whatever was inside the loop, recursively.
The Template class is a convenient wrapper that takes care of template compilation and rendering.
Usage:
The only thing you should ever use directly in this file is the Template class. Create a compiled template object with a template_string, then call render() with a context. In the compilation stage, the TemplateSyntaxError exception will be raised if the template doesn't have proper syntax.
Sample code:
>>> from django import template >>> s = '<html>{% if test %}<h1>{{ varvalue }}</h1>{% endif %}</html>' >>> t = template.Template(s)
(t is now a compiled template, and its render() method can be called multiple times with multiple contexts)
>>> c = template.Context({'test':True, 'varvalue': 'Hello'}) >>> t.render(c) '<html><h1>Hello</h1></html>' >>> c = template.Context({'test':False, 'varvalue': 'Hello'}) >>> t.render(c) '<html></html>'
Class | Origin |
Undocumented |
Constant | BLOCK_TAG_END |
Undocumented |
Constant | BLOCK_TAG_START |
Undocumented |
Constant | COMMENT_TAG_END |
Undocumented |
Constant | COMMENT_TAG_START |
Undocumented |
Constant | FILTER_ARGUMENT_SEPARATOR |
Undocumented |
Constant | FILTER_SEPARATOR |
Undocumented |
Constant | SINGLE_BRACE_END |
Undocumented |
Constant | SINGLE_BRACE_START |
Undocumented |
Constant | UNKNOWN_SOURCE |
Undocumented |
Constant | VARIABLE_ATTRIBUTE_SEPARATOR |
Undocumented |
Constant | VARIABLE_TAG_END |
Undocumented |
Constant | VARIABLE_TAG_START |
Undocumented |
Variable | constant_string |
Undocumented |
Variable | filter_raw_string |
Undocumented |
Variable | filter_re |
Undocumented |
Variable | kwarg_re |
Undocumented |
Variable | logger |
Undocumented |
Variable | tag_re |
Undocumented |
Class | DebugLexer |
No class docstring; 1/3 method documented |
Class | FilterExpression |
Parse a variable token and its optional filters (all as a single string), and return a list of tuples of the filter name and arguments. Sample: |
Class | Lexer |
No class docstring; 0/2 instance variable, 2/4 methods documented |
Class | Node |
No class docstring; 0/3 class variable, 3/4 methods documented |
Class | NodeList |
No class docstring; 0/1 class variable, 1/2 method documented |
Class | Parser |
No class docstring; 0/6 instance variable, 3/14 methods documented |
Class | Template |
No class docstring; 0/5 instance variable, 3/7 methods documented |
Class | TextNode |
No class docstring; 0/1 instance variable, 0/1 class variable, 1/4 method documented |
Class | Token |
No class docstring; 0/4 instance variable, 1/3 method documented |
Class | TokenType |
Undocumented |
Class | Variable |
A template variable, resolvable against a given context. The variable may be a hard-coded string (if it begins and ends with single or double quote marks): |
Class | VariableDoesNotExist |
Undocumented |
Class | VariableNode |
Undocumented |
Function | linebreak_iter |
Undocumented |
Function | render_value_in_context |
Convert any value to a string to become part of a rendered template. This means escaping, if required, and conversion to a string. If value is a string, it's expected to already be translated. |
Function | token_kwargs |
Parse token keyword arguments and return a dictionary of the arguments retrieved from the bits token list. |
Parse token keyword arguments and return a dictionary of the arguments retrieved from the bits token list.
bits
is a list containing the remainder of the token (split by spaces)
that is to be checked for arguments. Valid arguments are removed from this
list.
support_legacy
- if True, the legacy format 1 as foo is accepted.
Otherwise, only the standard foo=1 format is allowed.
There is no requirement for all remaining token bits to be keyword arguments, so return the dictionary as soon as an invalid argument format is reached.