Zing. compile-to-PHP template engine GitHub ↗

Custom Directives

You can register your own directives to keep repetitive template logic out of templates:

$engine->directive('checked', new CheckedDirective());

Then use it like any built-in:

<input type="checkbox" @checked($user->is_admin) />

Writing a directive

A directive is a class that implements DirectiveCompiler:

use Zing\DirectiveCompiler;

class CheckedDirective implements DirectiveCompiler
{
    public function compile(string $expression): string
    {
        return "<?php echo {$expression} ? 'checked' : ''; ?>";
    }
}

The $expression is the raw text inside the parentheses. Return the PHP that replaces the directive in the compiled template.

That's it. No special setup, no registration beyond calling directive() on the engine.