• 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 pages from the same template and the same data. Each tab is a different RENDERING SHAPE — what a render actually costs depends on it, so the tabs answer "how does this engine behave on the page I am writing" rather than "who wins one synthetic page". 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.

Two properties, a nested object, a nullable property behind a default, and an array inside an object — the shape most real templates are, and the only page where property access itself is on the clock.

Time per render steady state

Render time for the same page, median of every render, fastest first: Clarity 0.080 ms median, Native 0.100 ms median, Plates 0.109 ms median, Stempler 0.115 ms median, Latte 0.237 ms median, Blade 0.258 ms median, Twig 0.405 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.252 ms, Plates 3.763 ms, Clarity 30.155 ms, Blade 37.986 ms, Twig 63.930 ms, Stempler 66.368 ms, Latte 94.079 ms.

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

The same content in the same order with the same values held as nested arrays and read by key instead of by property — so the difference from the tab beside it is the access shape and nothing else.

Time per render steady state

Render time for the same page, median of every render, fastest first: Clarity 0.081 ms median, Native 0.103 ms median, Plates 0.109 ms median, Stempler 0.118 ms median, Latte 0.241 ms median, Blade 0.263 ms median, Twig 0.347 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.260 ms, Plates 3.806 ms, Clarity 31.524 ms, Blade 39.877 ms, Stempler 65.744 ms, Twig 66.170 ms, Latte 95.980 ms.

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

The heavy page: a block of flat values each emitted twice — once as text, once in an attribute — and a table of rows with six fields each, every value HTML-special so the escaping path does real work in both contexts.

Time per render steady state

Render time for the same page, median of every render, fastest first: Clarity 0.271 ms median, Native 0.306 ms median, Stempler 0.324 ms median, Plates 0.343 ms median, Latte 0.440 ms median, Blade 0.557 ms median, Twig 0.614 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.724 ms, Plates 5.160 ms, Blade 41.218 ms, Clarity 45.687 ms, Stempler 80.991 ms, Twig 93.340 ms, Latte 114.322 ms.

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

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.