Documentation

Guide & Quick Start

Installation, first steps and a compact language overview. The full 17-part guide covering every feature in detail ships as GUIDE.md in the download package.

Installation

Two equally valid paths, depending on whether the project already uses Composer:

With Composer

composer require uiengine/uiengine

Without Composer

require 'UIEngine/src/Autoload.php';

Quick start

  1. 1

    Include it

    Load the Composer autoloader, or include UIEngine/src/Autoload.php directly.

    require 'vendor/autoload.php';
    use UIEngine\UIEngine;
    
    $ui = new UIEngine();
  2. 2

    Set directories

    Set the template, cache and log directories (or use the defaults from Config.php).

    $ui->getPathManager()->setTemplate(__DIR__.'/templates');
    $ui->getPathManager()->setCache(__DIR__.'/cache');
  3. 3

    Assign variables

    Provide any PHP value (scalars, arrays, objects) as a template variable.

    $ui->assign('title', 'Hello world');
    $ui->assign('items', ['Coffee', 'Tea', 'Cocoa']);
  4. 4

    Display the template

    display() compiles (cached) and outputs the rendered result.

    $ui->display('page.tpl');

Language overview

The most important tags at a glance – all of them run through the same safe expression grammar.

TagMeaning
{$var}Output a variable, HTML-escaped.
{$var|escape:'url'}Apply a modifier, here context-aware escaping.
{if $expr}...{/if}Condition with comparison and logical operators.
{foreach $arr as $i}Loop over an array, including $i@first/@last/@index.
{extends 'a.tpl'}Inheritance: the current template defines a parent layout.
{block name}...{/block}A swappable section within the inheritance chain.
{include 'a.tpl'}Include a partial template, its own cache entry.
{function name=x}Define a reusable template macro.
{eval var=$expr}Evaluate a string as a mini-template at runtime.
{nocache}...{/nocache}Stays dynamic within an otherwise cached page.

Inheritance & layouts

A parent layout defines swappable blocks, child templates fill or extend them:

layout.tpl

<!DOCTYPE html>
<html>
<body>
  {block content}Standard{/block}
</body>
</html>

seite.tpl

{extends 'layout.tpl'}
{block content}
  Eigener Inhalt hier.
{/block}

Custom plugins

Three levels of extension, from simple to complete:

$ui->register('kuerzel', fn($text, $len = 80) =>
    mb_strlen($text) > $len ? mb_substr($text, 0, $len) . '…' : $text
);
{plugin name='kuerzel' args='$artikel->text, 120'}

Security-policy sandbox

Optional, for environments with multiple, differently trusted template authors:

$policy = new SecurityPolicy();
$policy->allowModifiers(['upper', 'lower', 'date_format']);
$policy->allowConstants(['PHP_VERSION']);
$policy->disallowEval();
$policy->allowFetch('/var/www/shared/');

$ui->setSecurityPolicy($policy);

A policy only ever narrows things further – never beyond the limits the compiler already enforces.

Caching

Compiled templates are cached automatically and only recompiled on change. For entire pages:

$ui->display('seite.tpl', $cacheId, 3600); // 1 Stunde

Sections inside {nocache}...{/nocache} stay unaffected and are re-evaluated on every request – handy for cart widgets or timestamps on an otherwise cached page.

Still have questions?

The most common questions are answered in the FAQ – from security to system requirements.