API reference¶
mstache module.
This module alone implements the entire mstache 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 implementations and future major mstache revisions.
- mstache.tokenize(template, *, tags=(b'{{', b'}}'), comments=False, cache={}, cache_make_key=<class 'tuple'>)[source]¶
Compile mustache template as a tuple of token tuples.
- Parameters:
template (
bytes) – template as utf-8 encoded bytestags (
tuple[bytes,bytes]) – mustache tag tuple (open, close)comments (
bool) – whether yield comment tokens or not (ignore comments)cache (
CompiledTemplateCache) – mutable mapping for compiled template cachecache_make_key (
Callable[[tuple[bytes,bytes,bytes,bool]],Hashable]) – key function for compiled template cache
- Return type:
- Returns:
tuple of token tuples
- Raises:
UnclosedTokenException – if token is left unclosed
ClosingTokenException – if block closing token does not match
DelimiterTokenException – if delimiter token syntax is invalid
- mstache.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={}, cache_make_key=<class 'tuple'>, recursion_limit=1024)[source]¶
Generate rendered mustache template chunks.
- Parameters:
template (
TypeVar(TString,str,bytes)) – mustache template (str or bytes)scope (
Any) – current rendering scope (data object)scopes (
Iterable) – list of precedent scopesresolver (
Callable[[AnyStr],AnyStr]) – callable will be used to resolve partials (bytes)getter (
Callable[[Any,Sequence,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 (
CompiledTemplateCache) – mutable mapping for compiled template cachecache_make_key (
Callable[[tuple[bytes,bytes,bytes,bool]],Hashable]) – key function for compiled template cacherecursion_limit (
int) – maximum number of nested render operations
- 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
RenderingRecursionError – if rendering recursion limit is exceeded
- mstache.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={}, cache_make_key=<class 'tuple'>, recursion_limit=1024)[source]¶
Render mustache template.
- Parameters:
scope (
Any) – current rendering scope (data object)scopes (
Iterable) – list of precedent scopesresolver (
Callable[[AnyStr],AnyStr]) – callable will be used to resolve partials (bytes)getter (
Callable[[Any,Sequence,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 (
CompiledTemplateCache) – mutable mapping for compiled template cachecache_make_key (
Callable[[tuple[bytes,bytes,bytes,bool]],Hashable]) – key function for compiled template cacherecursion_limit (
int) – maximum number of nested render operations
- Return type:
- 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
TemplateRecursionError – if rendering recursion limit is exceeded
- mstache.cli(argv=None)[source]¶
Render template from command line.
Use python -m mstache –help to check available options.
- exception mstache.TokenException[source]¶
Bases:
SyntaxErrorInvalid token found during tokenization.
- exception mstache.ClosingTokenException[source]¶
Bases:
TokenExceptionNon-matching closing token found during tokenization.
- exception mstache.UnclosedTokenException[source]¶
Bases:
ClosingTokenExceptionUnclosed token found during tokenization.
- exception mstache.DelimiterTokenException[source]¶
Bases:
TokenExceptionInvalid delimiters token found during tokenization.
Added in version 0.1.1.
- exception mstache.TemplateRecursionError[source]¶
Bases:
RecursionErrorTemplate rendering exceeded maximum render recursion.
- mstache.default_recursion_limit = 1024¶
Default number of maximum nested renders.
A nested render happens whenener the lambda render parameter is used or when a partial template is injected.
Meant to avoid recursion bombs on templates.
- mstache.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
Added 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.
- mstache.default_escape(data)[source]¶
Convert bytes conflicting with HTML to their escape sequences.
- mstache.default_lambda_render(scope, **kwargs)[source]¶
Generate a template-only render function with fixed parameters.
- mstache.default_tags = (b'{{', b'}}')¶
Tuple of default mustache tags as in tuple[bytes, bytes].
- mstache.default_cache: CompiledTemplateCache = {}¶
Default template cache mapping, keeping the 1024 most recently used compiled templates (LRU expiration).
- mstache.default_cache_make_key¶
Defaut template cache key function,
tuple, so keys would be as in tuple[bytes, bytes, bytes, bool] containing the relevantmstache.tokenizer()parameters.
- mstache.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).
- class mstache.TString¶
String/bytes generic.
alias of TypeVar(‘TString’, str, bytes)
- mstache.PropertyGetter¶
Template property getter function interface.
- mstache.StringifyFunction¶
Template variable general stringification function interface.
- mstache.EscapeFunction¶
Template variable value escape function interface.
- mstache.LambdaRenderFunctionConstructor¶
Lambda render function constructor interface.
- mstache.VirtualPropertyFunction¶
Virtual property implementation callable interface.
- mstache.VirtualPropertyMapping¶
Virtual property mapping interface.
- mstache.CompiledToken¶
Compiled template token.
Tokens are tuples containing a renderer decision path, key, content and flags.
type: boolDecision for rendering path node a.
type: boolDecision for rendering path node b.
type: boolDecision for rendering path node c
key: bytesTemplate substring with token scope key.
- ``content: bytes`
Template substring with token content data.
flags: intToken flags.
Unused:
-1(default)- Variable flags:
0- escaped1- unescaped
- Block start flags:
0- falsy1- truthy
Block end value: block content index.
- mstache.CompiledTemplate¶
Compiled template interface.
See also
mstache.CompiledTokenItem type.
mstache.CompiledTemplateCacheInterface exposing this type.
alias of
tuple[tuple[bool,bool,bool,bytes,bytes,int], …]
- class mstache.CompiledTemplateCache(*args, **kwargs)[source]¶
Bases:
ProtocolCache object protocol.
See also
mstache.CompiledTemplateItem type.
mstache.CacheMakeKeyFunctionCache key function interface.