• PHP 8.2+
  • Minimal deps
  • Sandboxed
  • OPcache

Clarity Engine

A fast, secure, and expressive PHP template engine – Maximum performance, whether sandboxed and secure or with the full power of PHP.

Why Clarity

Compiled, cached, contained

Compiled & Cached

Templates compile to PHP classes and ride the OPcache for blazing-fast rendering. First render compiles, every render after is straight PHP.

Secure Sandbox

No arbitrary PHP execution. Templates can only access variables passed to render() and registered filters — nothing else gets through.

Expressive Syntax

Clean, readable template language with macros, template inheritance, the filter pipeline and context-aware auto escaping. Familiar, fast, and easy to learn.

Modules & Extensions

Bundle filters, functions and directives into self-registering plug-ins. Register custom filters, functions, inline filters and block directives without touching the engine.

Unicode Aware

Full multibyte string handling with transparent normalization, auto-escaping that understands context, and clean integration with PHP's intl extension.

Minimal Dependencies

Standalone engine with no external dependencies beyond PHP 8.2+ and mbstring. Just composer require and start rendering.

From zero to rendered

Build your first Clarity template in under a minute. Templates extend a layout, render variables, and call the filter pipeline — all without leaving the sandbox.

  • One-liner composer require
  • Compiled to PHP classes for OPcache-friendly rendering
  • Auto-escaping with {{ "…" |> raw }} opt-out
  • Built-in macros, inheritance, includes and filter chains
<?php
use Clarity\ClarityEngine;

$engine = new ClarityEngine([
    'viewPath'   => __DIR__ . '/views',
    'namespaces' => [
        'admin'  => __DIR__ . '/views/admin',
        'emails' => __DIR__ . '/views/emails',
    ],
]);

echo $engine->render('welcome', [
    'title' => 'Welcome to Clarity',
    'user'  => ['name' => 'Developer'],
]);

Syntax

Reads like HTML, renders like PHP

Clarity's syntax is intentionally close to modern template engines so existing skills transfer cleanly. Variables, filters, control flow, macros and layout inheritance — all available, all sandboxed.

welcome.clarity.html Clarity
{% extends "layouts/base" %}

{% block content %}
  <h1>Hello, {{ user:name }}!</h1>
  <p>{{ post:date |> date("M j") }}</p>

  {% macro card(title, body) %}
    <div class="card">
      <h3>{{ title }}</h3>
      <p>{{ body }}</p>
    </div>
  {% endmacro %}

  {% call card("Welcome back", intro) %}
{% endblock %}
Rendered HTML
<h1>Hello, Developer!</h1>
<p>Nov 8</p>

<div class="card">
  <h3>Welcome back</h3>
  <p>The sandbox is warm.</p>
</div>
  • Filter chains
  • Macros & calls
  • Loops & conditions
  • Spread literals

Measured, not claimed

Benchmark results

Seven engines render the same page from the same template and the same data: a layout, an included partial, a block of flat values each emitted twice — once as text, once in an attribute — and a table of rows with six fields each, with every value HTML-special so the escaping path does real work in both contexts. Each engine renders in-process, so a difference here is the engine rather than the server, and inside every chart each row is relative to the fastest.

Time per render steady state

Render time for the same page, median of every render, fastest first: Native 0.304 ms median, Clarity 0.304 ms median, Stempler 0.324 ms median, Plates 0.340 ms median, Latte 0.438 ms median, Blade 0.552 ms median, Twig 0.609 ms median.

Every render of the same page is measured: the dot is the median, the caps the fastest observation and the 95th percentile.

First render cold template cache

First render in a fresh process with a cold template cache, fastest first: Native 1.735 ms, Plates 4.852 ms, Clarity 34.071 ms, Blade 39.354 ms, Stempler 80.558 ms, Twig 93.544 ms, Latte 116.467 ms.

The first request after a deploy: engine classes load, the template compiles, the cache is written and the page renders once.

Memory per run retained heap

Heap after a whole run, lightest retained first: Native 1.73 MB retained, 1.73 MB floor, 2.79 MB peak, Plates 1.80 MB retained, 1.73 MB floor, 2.65 MB peak, Clarity 2.43 MB retained, 1.88 MB floor, 3.19 MB peak, Stempler 2.59 MB retained, 1.93 MB floor, 3.26 MB peak, Latte 3.02 MB retained, 1.73 MB floor, 11.15 MB peak, Twig 3.09 MB retained, 1.73 MB floor, 4.75 MB peak, Blade 3.13 MB retained, 1.73 MB floor, 3.46 MB peak.

The heap a whole run still holds after a garbage collection, and the run's high-water mark, each read in its own fresh process.

Median of 10,000 renders per run over 30 runs, 200 items per render, rendered in-process on the PHP CLI. PHP 8.5.11 · OPcache (opcache.enable_cli): yes · JIT: tracing (buffer 64M) · Clarity template revalidate: 60 s.

Install

One command, no dependencies

Requires PHP 8.2+ with the mbstring extension. The intl extension enables locale-aware formatting.

composer require sailantis/clarity-engine

Or read the docs first.