template

Mustache-style template engine with HTML escaping, raw output, conditionals and loops.

v1.0.0Text & stringsbundledimport "template";
Overview Installation Documentation Examples

Overview

A zero-dependency Mustache-style string template engine for HolyPHP.

Supports HTML-escaped interpolation ({{var}}), raw unescaped output ({{{raw}}}), dot-path lookup ({{user.profile.name}}), conditionals ({{#if cond}}...{{else}}...{{/if}}), loops ({{#each items}}...{{else}}...{{/each}}), comments ({{! ... }}), and clear compile-time/runtime error messages with line numbers.

bundled — works offline zero dependencies mustache render

Quick start

import "template";

$html = template_render(
    "<h1>Hello, {{user.name}}!</h1>"
    . "{{#if items}}<ul>{{#each items}}<li>{{.}}</li>{{/each}}</ul>"
    . "{{else}}<p>No items.</p>{{/if}}",
    [
        "user" => ["name" => "Ada <admin>"],
        "items" => ["Compilers", "Templates", "HolyPHP"]
    ]
);

println($html);
// <h1>Hello, Ada &lt;admin&gt;!</h1><ul><li>Compilers</li><li>Templates</li><li>HolyPHP</li></ul>

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install template

This adds the library to hphp.yaml, installs it into .hphp/packages/template/ and pins the exact version in hphp.lock:

require:
  template: "^1.0"

Use it

import "template";

template is bundled with the compiler, so import "template"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install template          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install template
Packagetemplate
Version1.0.0 (follows the compiler)
Entry filetemplate.hphp
Sourcepackages/lib/template ↗

Documentation

Template Syntax

SyntaxDescription
{{var}}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 Class

Compile once and render multiple times with different data:

Error Handling

All errors throw Exception with a template: prefix and line number where applicable:

Examples

01Quick start

Bundled with the compiler — import "template"; works offline in any file, or add it to a project with hphp install template.

import "template";

$html = template_render(
    "<h1>Hello, {{user.name}}!</h1>"
    . "{{#if items}}<ul>{{#each items}}<li>{{.}}</li>{{/each}}</ul>"
    . "{{else}}<p>No items.</p>{{/if}}",
    [
        "user" => ["name" => "Ada <admin>"],
        "items" => ["Compilers", "Templates", "HolyPHP"]
    ]
);

println($html);
// <h1>Hello, Ada &lt;admin&gt;!</h1><ul><li>Compilers</li><li>Templates</li><li>HolyPHP</li></ul>
Try it
hphp install template
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_template.hphp.