HTML-escaped interpolation (&, <, >, ", '). Supports dot paths like {{user.name}}. Missing keys render as "".
{{{raw}}}
Unescaped raw interpolation.
{{#if cond}}...{{/if}}
Renders body if cond is truthy (non-null, non-false, non-zero, non-empty string/array).
{{#if cond}}...{{else}}...{{/if}}
Conditional with fallback branch.
{{#each items}}...{{/each}}
Iterates over an array/map. Inside the loop: . and this refer to the current item, @index is the 0-based index, @key is the key, @first and @last are booleans. If the item is a map, its keys are accessible directly, and outer scope variables remain accessible.
{{#each items}}...{{else}}...{{/each}}
Loop with fallback branch when items is empty or null.
{{! comment }}
Comment (stripped from output).
API Reference
Functions
template_render(string $tpl, $data = []): string Compiles and renders $tpl with $data in one step.
template_escape(string $value): string Escapes &, <, >, ", and ' for safe HTML output.
Template Class
Compile once and render multiple times with different data:
new Template(string $source) Parses and compiles $source into an AST. Throws Exception immediately on syntax errors.
Template::compile(string $source): Template Static factory equivalent to new Template($source).
$tpl->render($data = []): string Renders the compiled template against $data.
$tpl->getSource(): string Returns the original template source string.
Error Handling
All errors throw Exception with a template: prefix and line number where applicable:
Unclosed tags: template: unclosed tag '{{' starting at line 1, template: unclosed raw tag '{{{' starting at line 1
Empty tags: template: empty tag '{{}}' at line 1, template: empty raw tag '{{{}}}' at line 1
Unclosed blocks: template: unclosed '{{#if}}' block opened at line 1, template: unclosed '{{#each}}' block opened at line 1
Mismatched/unexpected tags: template: mismatched closing tag '{{/each}}' at line 1, expected '{{/if}}' for block opened at line 1, template: unexpected '{{/if}}' without matching '{{#if}}' at line 1, template: unexpected '{{else}}' without matching block at line 1
Invalid loop target: template: '{{#each items}}' at line 1 expects an array
Examples
01Quick start
Bundled with the compiler — import "template"; works offline in any file, or add it to a project with hphp install template.