module documentation

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 ​Debug​Lexer No class docstring; 1/3 method documented
Class ​Filter​Expression 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 ​Node​List 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 ​Text​Node 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 ​Token​Type 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 ​Variable​Does​Not​Exist Undocumented
Class ​Variable​Node 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.
BLOCK_TAG_END: str =

Undocumented

Value
'%}'
BLOCK_TAG_START: str =

Undocumented

Value
'{%'
COMMENT_TAG_END: str =

Undocumented

Value
'#}'
COMMENT_TAG_START: str =

Undocumented

Value
'{#'
FILTER_ARGUMENT_SEPARATOR: str =

Undocumented

Value
':'
FILTER_SEPARATOR: str =

Undocumented

Value
'|'
SINGLE_BRACE_END: str =

Undocumented

Value
'}'
SINGLE_BRACE_START: str =

Undocumented

Value
'{'
UNKNOWN_SOURCE: str =

Undocumented

Value
'<unknown source>'
VARIABLE_ATTRIBUTE_SEPARATOR: str =

Undocumented

Value
'.'
VARIABLE_TAG_END: str =

Undocumented

Value
'}}'
VARIABLE_TAG_START: str =

Undocumented

Value
'{{'
constant_string =

Undocumented

filter_raw_string =

Undocumented

filter_re =

Undocumented

kwarg_re =

Undocumented

logger =

Undocumented

tag_re =

Undocumented

def linebreak_iter(template_source):

Undocumented

def render_value_in_context(value, 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.
def token_kwargs(bits, parser, support_legacy=False):

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.