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:

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:

  1. Under scope a, variable b has no c key.

  2. Under root scope, deep reference to b.c has a value.

Example: strict mode

import mstache
mstache.render(
    '{{#a}}{{b.c}}{{/a}}',
    {'a': {'b': {}}, 'b': {'c': 1}},
    strict=False,
    )
# '' (empty string)

Explained as:

  1. Under scope a, variable b has no c key.

  2. Since b was partially matched, b.c is considered missing.