Strict tags - bobthecow/mustache.php GitHub Wiki
Strict tags
By default, Mustache ignores missing tags. A missing variable renders as an empty string, a missing section renders nothing, and a missing partial is logged but otherwise skipped.
Output coercion is strict by default, matching previous Mustache.php behavior. Non-scalar, non-stringable output values throw a clear RuntimeException instead of relying on PHP string coercion failures.
Set the strict_tags engine option to make selected missing tags fail instead:
$mustache = new \Mustache\Engine([
'strict_tags' => \Mustache\Engine::STRICT_INTERPOLATION | \Mustache\Engine::STRICT_PARTIALS,
]);
Set strict_tags to true to enable all strict checks, or false to disable them all, including strict output coercion.
Available checks
STRICT_INTERPOLATION: missing interpolation tags like{{ name }}throwUnknownVariableException.STRICT_SECTIONS: missing section and inverted section tags like{{# items }}and{{^ items }}throwUnknownVariableException.STRICT_PARTIALS: missing partial templates like{{> sidebar }}throwUnknownTemplateException.STRICT_PARENTS: missing parent templates like{{< layout }}throwUnknownTemplateException.STRICT_EXTRA_BLOCKS: extra inheritance block overrides throwUnknownBlockExceptionwhen they cannot be matched to a rendered parent template.STRICT_COERCION: non-scalar, non-stringable output values throwRuntimeException. This is enabled by default.STRICT_ALL: enables every strict check available in this release.
To relax output coercion while keeping other checks strict, pass a bitmask that excludes STRICT_COERCION.
Extra block overrides
STRICT_EXTRA_BLOCKS validates block overrides as parent templates are resolved for rendering.
An override is accepted when the resolved parent declares that block, even if the block itself is inside a false section and does not render:
{{! layout }}
{{# show_title }}
{{$ title }}Default title{{/ title }}
{{/ show_title }}
{{! page }}
{{< layout }}
{{$ title }}Page title{{/ title }}
{{/ layout }}
If show_title is false, this renders without throwing because layout still declares the title block.
If a parent tag is skipped by a section or another runtime condition, Mustache does not validate block overrides against templates on that skipped path:
{{! wrapper }}
{{# use_layout }}
{{< layout }}{{/ layout }}
{{/ use_layout }}
{{! page }}
{{< wrapper }}
{{$ typo }}Extra block{{/ typo }}
{{/ wrapper }}
If use_layout is false, layout is never resolved, so STRICT_EXTRA_BLOCKS does not throw for typo.
Dynamic names
Dynamic partial and parent names are resolved at render time. With the relevant strict check enabled, a missing dynamic name variable throws UnknownVariableException; a resolved template name that cannot be loaded throws UnknownTemplateException.
For extra block checks, dynamic parents follow the same rule as static parents: validation happens after the concrete parent template is resolved for rendering.