Strict mode¶
Mustache v1.4.2 spec defines couple of rules often ignored by specific implementations like mustache.js for which we aim maximum compatibility with.
To match both use-cases, mstache follows mustache.js implementation
quirks while also providing an strict toggle to enable the strict behavior.
The strict behavior, enabled by passing strict=True to mstache.render(),
mstache.stream(), mstache.tokenize(), implies:
Setting invalid delimiters containing spaces will raise
mstache.DelimiterTokenExceptioninstead of undefined behavior.If getter parameter is an
mstache.PropertyGetterTuple(default), its second item,mstache.default_strict_getter(), will be used.
Caution
Strict-mode is expected to implement more syntax-related exceptions over time, either because of spec updates or more quirks being discovered.
Use strict mode with caution for when strictness is desirable (like validation).
Strict getter¶
The stricter getter from mstache.PropertyGetterTuple second element
considers a nested reference (nested.value) to be missing on partial matches,
that is, when it does not completely resolve but the parent component of
the reference is in the scope.
This is the intended behavior defined by Mustache v1.4.2 spec.
Example: non-strict mode¶
import mstache
mstache.render(
'{{#a}}{{b.c}}{{/a}}',
{'a': {'b': {}}, 'b': {'c': 1}},
)
# '1'
Explained as:
Under scope
a, variablebhas nockey.Under root scope, deep reference to
b.chas a value.
Example: strict mode¶
import mstache
mstache.render(
'{{#a}}{{b.c}}{{/a}}',
{'a': {'b': {}}, 'b': {'c': 1}},
strict=False,
)
# '' (empty string)
Explained as:
Under scope
a, variablebhas nockey.Since
bwas partially matched,b.cis considered missing.