Skip to content
Templex

Templates

Templates can be any form of text based file. By default, Templex looks for files with .stub or .template extensions.

Templex uses <{ and }> as opening and closing delimiters for all template directives:

<{ $variable }>
<{ include template.name }>
<{ if( condition ) }>...<{ endif }>
<{ foreach( $array as $item ) }>...<{ endforeach }>
<{ for( $i = 0; $i < 10; $i++ ) }>...<{ endfor }>
<{ switch( $variable ) }>...<{ endswitch }>

Configure which file extensions Templex should look for:

// Single extension
$templex = new Templex($templateDir, 'stub');
// Multiple extensions (comma-separated)
$templex = new Templex($templateDir, 'stub,template,tpl');

All templates are loaded from a base directory. Subdirectories can be referenced using dot notation:

$templex = new Templex(__DIR__ . '/templates/');
// Renders templates/welcome.stub
$templex->render('welcome', ['name' => 'Fred']);
// Renders templates/emails/invite.stub
$templex->render('emails.invite', ['email' => $email]);

Pass variables as an associative array when rendering:

$data = [
'title' => 'My Page',
'users' => ['Fred', 'Chris', 'Tor'],
'active' => true,
];
$output = $templex->render('page', $data);

Templex automatically caches loaded templates for improved performance. The cache can be cleared when needed:

$templex->clearTemplateCache();

Because Templex uses regex-based pattern matching rather than eval() or PHP includes, templates cannot execute arbitrary code. This makes it safe to use with user-provided template content.