Package template
source code
This is the Django template system.
How it works:
The Lexer.tokenize() function converts a template string (i.e., a
string containing markup with custom template tags) to tokens, which can
be either plain text (TOKEN_TEXT), variables (TOKEN_VAR) or block
statements (TOKEN_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 an IfNode 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 = u'<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)
u'<html><h1>Hello</h1></html>'
>>> c = template.Context({'test':False, 'varvalue': 'Hello'})
>>> t.render(c)
u'<html></html>'
|
|
compile_string(template_string,
origin)
Compiles template_string into NodeList ready for rendering |
source code
|
|
|
|
resolve_variable(path,
context)
Returns the resolved variable, which may contain attribute syntax,
within the given context. |
source code
|
|
|
|
generic_tag_compiler(params,
defaults,
name,
node_class,
parser,
token)
Returns a template.Node subclass. |
source code
|
|
|
|
|
|
|
|
|
|
TOKEN_TEXT = 0
|
|
|
TOKEN_VAR = 1
|
|
|
TOKEN_BLOCK = 2
|
|
|
TOKEN_COMMENT = 3
|
|
|
FILTER_SEPARATOR = '|'
|
|
|
FILTER_ARGUMENT_SEPARATOR = ':'
|
|
|
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
|
|
|
BLOCK_TAG_START = '{%'
|
|
|
BLOCK_TAG_END = '%}'
|
|
|
VARIABLE_TAG_START = '{{'
|
|
|
VARIABLE_TAG_END = '}}'
|
|
|
COMMENT_TAG_START = '{#'
|
|
|
COMMENT_TAG_END = '#}'
|
|
|
SINGLE_BRACE_START = '{'
|
|
|
SINGLE_BRACE_END = '}'
|
|
|
ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJ...
|
|
|
UNKNOWN_SOURCE = '<unknown source>'
|
|
|
tag_re = re.compile(r'(\{%.*?%\}|\{.*?\}\}|#.*?#\})')
|
|
|
libraries = {}
|
|
|
builtins = []
|
|
|
invalid_var_format_string = None
|
|
|
filter_raw_string = '^\\_\\("(?P<i18n_constant>[^"\\\\]*(?:\\\...
|
|
|
filter_re = re.compile(r'(?u)^_\("(?P<i18n_constant>[^"\\]*(?:...
|
|
Returns the resolved variable, which may contain attribute syntax,
within the given context.
Deprecated; use the Variable class instead.
|
ALLOWED_VARIABLE_CHARS
- Value:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.'
|
|
filter_raw_string
- Value:
'^\\_\\("(?P<i18n_constant>[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"\\)|^"(?P<con
stant>[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|^(?P<var>[\\w\\.]+)|(?:\\|(?P<fil
ter_name>\\w+)(?:\\:(?:\\_\\("(?P<i18n_arg>[^"\\\\]*(?:\\\\.[^"\\\\]*)
*)"\\)|"(?P<constant_arg>[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|(?P<var_arg>[\
\w\\.]+)))?)'
|
|
filter_re
- Value:
re.compile(r'(?u)^_\("(?P<i18n_constant>[^"\\]*(?:\\.[^"\\]*)*)"\)|^"(
?P<constant>[^"\\]*(?:\\.[^"\\]*)*)"|^(?P<var>[\w\.]+)|(?:\|(?P<filter
_name>\w+)(?::(?:_\("(?P<i18n_arg>[^"\\]*(?:\\.[^"\\]*)*)"\)|"(?P<cons
tant_arg>[^"\\]*(?:\\.[^"\\]*)*)"|(?P<var_arg>[\w\.]+)))?)')
|
|