API reference¶
ustache module.
This module alone implements the entire ustache library, including its minimal command line interface.
The main functions are considered to be cli(), render() and
stream(), and they expose different approaches to template rendering:
command line interface, buffered and streaming rendering API, respectively.
Other functionality will be considered advanced, exposing some implementation-specific complexity and potentially non-standard behavior which could reduce compatibility with other mustache implentations and future major ustache versions.
- ustache.tokenize(template, *, tags=(b'{{', b'}}'), comments=False, cache={})[source]¶
Generate token tuples from mustache template.
This generator accepts sending back a token index, which will result on rewinding it back and repeat everything from there.
- Parameters
template (
bytes) – template as utf-8 encoded bytestags (
Tuple[AnyStr,AnyStr]) – mustache tag tuple (open, close)comments (
bool) – whether yield comment tokens or not (ignore comments)cache (
MutableMapping[Tuple[Union[bytes,int],bytes,bytes,bool],Tuple[Tuple[bool,bool,bool,slice,slice,int], …]]) – mutable mapping for compiled template cache
- Return type
Generator[Tuple[bool,bool,bool,slice,slice,int],int,None]- Returns
token tuple generator (type, name slice, content slice, option)
- Raises
UnclosedTokenException – if token is left unclosed
ClosingTokenException – if block closing token does not match
DelimiterTokenException – if delimiter token syntax is invalid
- ustache.stream(template, scope, *, scopes=(), resolver=<function default_resolver>, getter=<function default_getter>, stringify=<function default_stringify>, escape=<function default_escape>, lambda_render=<function default_lambda_render>, tags=(b'{{', b'}}'), cache={})[source]¶
Generate rendered mustache template chunks.
- Parameters
template (~TString) – mustache template (str or bytes)
scope (
Any) – current rendering scope (data object)resolver (
Callable[[AnyStr],AnyStr]) – callable will be used to resolve partials (bytes)getter (
Callable[[Any,Sequence[Any],AnyStr,Any],Any]) – callable will be used to pick variables from scopestringify (
Callable[[bytes,bool],bytes]) – callable will be used to render python types (bytes)escape (
Callable[[bytes],bytes]) – callable will be used to escape template (bytes)lambda_render (
Callable[…,Callable[…,AnyStr]]) – lambda render function constructortags (
Tuple[AnyStr,AnyStr]) – tuple (start, end) specifying the initial mustache delimiterscache (
MutableMapping[Tuple[Union[bytes,int],bytes,bytes,bool],Tuple[Tuple[bool,bool,bool,slice,slice,int], …]]) – mutable mapping for compiled template cache
- Return type
- Returns
generator of bytes/str chunks (same type as template)
- Raises
UnclosedTokenException – if token is left unclosed
ClosingTokenException – if block closing token does not match
DelimiterTokenException – if delimiter token syntax is invalid
- ustache.render(template, scope, *, scopes=(), resolver=<function default_resolver>, getter=<function default_getter>, stringify=<function default_stringify>, escape=<function default_escape>, lambda_render=<function default_lambda_render>, tags=(b'{{', b'}}'), cache={})[source]¶
Render mustache template.
- Parameters
template (~TString) – mustache template
scope (
Any) – current rendering scope (data object)resolver (
Callable[[AnyStr],AnyStr]) – callable will be used to resolve partials (bytes)getter (
Callable[[Any,Sequence[Any],AnyStr,Any],Any]) – callable will be used to pick variables from scopestringify (
Callable[[bytes,bool],bytes]) – callable will be used to render python types (bytes)escape (
Callable[[bytes],bytes]) – callable will be used to escape template (bytes)lambda_render (
Callable[…,Callable[…,AnyStr]]) – lambda render function constructortags (
Tuple[AnyStr,AnyStr]) – tuple (start, end) specifying the initial mustache delimiterscache (
MutableMapping[Tuple[Union[bytes,int],bytes,bytes,bool],Tuple[Tuple[bool,bool,bool,slice,slice,int], …]]) – mutable mapping for compiled template cache
- Return type
~TString
- Returns
rendered bytes/str (type depends on template)
- Raises
UnclosedTokenException – if token is left unclosed
ClosingTokenException – if block closing token does not match
DelimiterTokenException – if delimiter token syntax is invalid
- ustache.cli(argv=None)[source]¶
Render template from command line.
Use python -m ustache –help to check available options.
- exception ustache.TokenException[source]¶
Bases:
SyntaxErrorInvalid token found during tokenization.
- message = 'Invalid tag {tag} at line {row} column {column}'¶
- exception ustache.ClosingTokenException[source]¶
Bases:
ustache.TokenExceptionNon-matching closing token found during tokenization.
- message = 'Non-matching tag {tag} at line {row} column {column}'¶
- exception ustache.UnclosedTokenException[source]¶
Bases:
ustache.ClosingTokenExceptionUnclosed token found during tokenization.
- message = 'Unclosed tag {tag} at line {row} column {column}'¶
- exception ustache.DelimiterTokenException[source]¶
Bases:
ustache.TokenExceptionInvalid delimiters token found during tokenization.
New in version 0.1.1.
- message = 'Invalid delimiters {tag} at line {row} column {column}'¶
- ustache.default_getter(scope, scopes, key, default=None, *, virtuals=mappingproxy({'length': <function virtual_length>}))[source]¶
Extract property value from scope hierarchy.
- Parameters
- Return type
- Returns
value from scope or default
New in version 0.1.3: virtuals parameter.
Both
AttributeErrorandTypeErrorexceptions raised by virtual property implementations will be handled as if that property doesn’t exist, which can be useful to filter out incompatible types.
- ustache.default_escape(data)[source]¶
Convert bytes conflicting with HTML to their escape sequences.
- ustache.default_lambda_render(scope, **kwargs)[source]¶
Generate a template-only render function with fixed parameters.
- ustache.default_tags = (b'{{', b'}}')¶
Tuple of default mustache tags (in bytes).
- ustache.default_cache: MutableMapping[Tuple[Union[bytes, int], bytes, bytes, bool], Tuple[Tuple[bool, bool, bool, slice, slice, int], ...]] = {}¶
Default template cache mapping, keeping the 1024 most recently used compiled templates (LRU expiration).
If xxhash is available, template data won’t be included in cache.
- ustache.default_virtuals: Mapping[str, Callable[[Any], Any]] = mappingproxy({'length': <function virtual_length>})¶
Immutable mapping with default virtual properties.
The following virtual properties are implemented:
length, for non-mapping sized objects, returning
len(ref).
- ustache.TString¶
String/bytes generic.
alias of TypeVar(‘TString’, str, bytes)
- ustache.PartialResolver¶
Template partial tag resolver function interface.
alias of
Callable
- ustache.PropertyGetter¶
Template property getter function interface.
- ustache.StringifyFunction¶
Template variable general stringification function interface.
- ustache.EscapeFunction¶
Template variable value escape function interface.
- ustache.LambdaRenderFunctionFactory¶
Deprecated since version 0.1.3: Use
LambdaRenderFunctionConstructorinstead.alias of
Callable[[…],Callable[[…],AnyStr]]
- ustache.LambdaRenderFunctionConstructor¶
Lambda render function constructor interface.
alias of
Callable[[…],Callable[[…],AnyStr]]
- ustache.CompiledTemplate¶
Compiled template interface.
See also
ustache.CompiledTokenItem type.
ustache.CompiledTemplateCacheInterface exposing this type.
alias of
Tuple[Tuple[bool,bool,bool,slice,slice,int], …]
- ustache.CompiledToken¶
Compiled template token.
Tokens are tuples containing a renderer decission path, key, content and flags.
type: boolDecission for rendering path node a.
type: boolDecission for rendering path node b.
type: boolDecission for rendering path node c
key: Optional[slice]Template slice for token scope key, if any.
content: Optional[slice]Template slice for token content data, if any.
flags: intToken flags.
Unused:
-1(default)- Variable flags:
0- escaped1- unescaped
- Block start flags:
0- falsy1- truthy
Block end value: block content index.
- ustache.CompiledTemplateCache¶
Cache mapping interface.
See also
ustache.CompiledTemplateCacheItem type.
alias of
MutableMapping[Tuple[Union[bytes,int],bytes,bytes,bool],Tuple[Tuple[bool,bool,bool,slice,slice,int], …]]
- ustache.TagsTuple¶
Mustache tag tuple interface.
alias of
Tuple