-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Alex Oliver edited this page May 25, 2026
·
5 revisions
Brace is a lightweight templating syntax for PHP that uses a familiar {{ double-brace }} syntax. It shares similarities with popular templating engines such as Mustache, Twig, Smarty, and Latte.
Designed to be simple and easy to adopt, Brace focuses on a low learning curve while remaining flexible enough for most templating needs. It provides essential functionality out of the box without requiring additional dependencies.
By default, Brace loads .tpl files from the current directory. The following example loads multiple templates, document-header.tpl, template.tpl, and document-footer.tpl, and outputs them as a single HTML file.
<?php
use Brace\Parser;
$brace = new Parser();
// Register filters
$brace->registerFilter('int', fn($content) => (int) $content);
$brace->registerFilter('decimal', fn($number) => number_format($number, 2));
$brace->registerFilter('uppercase', fn($content) => strtoupper($content));
$brace->registerFilter('titlecase', fn($content) => ucwords($content));
$brace->registerFilter('striptags', fn($content) => strip_tags($content));
// Register shortcode
$brace->regShortcode('add_to_basket_button', fn($attrs) => sprintf(
'<button id="%d">Add to basket</button>',
$attrs['product_id'],
));
$brace->parse('template, document-footer', [
'name' => 'Jane Doe',
'products' => [
0 => [
'id' => 1154,
'price' => 22.66,
'title' => 'Product Number One',
'description' => 'This is a product description',
],
1 => [
'id' => 1156,
'price' => 16,
'title' => 'Product Number Two',
'description' => 'This is another product description',
],
],
]);Template:
<!-- Simple HTML template -->
[@include document-header]
<main>
<p>Hello World. My name is {{ name|titlecase || "John Doe" }}</p>
<h1>Product List:</h1>
{{ if products }}
<p>Showing {{ COUNT(products) }} item(s)</p>
<ul>
{{ each products as product }}
<li>
<span>{{ product->title|uppercase }}</span>
<span> {{ product->currancy_symbol || "£" }}{{ product->price|decimal }} </span>
<p>{{ product->description|striptags }}</p>
[add_to_basket_button product_id="{{ product->id|int }}"]
</li>
{{ end }}
</ul>
{{ else }}
<p>No products to list</p>
{{ end }}
</main>Result:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Brace Examples</title>
</head>
<body>
<main>
<p>Hello World. My name is Jane Doe</p>
<h1>Product List:</h1>
<p>Showing 2 item(s)</p>
<ul>
<li>
<span>PRODUCT NUMBER ONE</span>
<span> £22.66 </span>
<p>This is a product description</p>
<button id="1154">Add to basket</button>
</li>
<li>
<span>PRODUCT NUMBER TWO</span>
<span> £16.00 </span>
<p>This is another product description</p>
<button id="1156">Add to basket</button>
</li>
</ul>
</main>
</body>
</html>