-
Notifications
You must be signed in to change notification settings - Fork 2
TemplateRenderer
Pair\Html\TemplateRenderer is the low-level renderer for Pair template files.
It reads an HTML/PHP template, replaces {{placeholder}} tokens, renders widget placeholders, and prints the final HTML.
This class is most relevant when you work on the application shell rather than on a module layout.
This is the only public method of the class and the main reason it exists.
Current behavior:
- reads the template file content
- scans available widgets through
Widget::availableWidgets() - replaces matching widget placeholders with
Widget::render() - replaces built-in placeholders mapped from
Application - replaces common placeholders such as
baseHref,styles, andscripts - replaces dynamic variables from
Application::getVars() - prints the final HTML
Important implementation detail: parse() prints the result directly, it does not return a string.
Mapped from Application properties:
-
{{langCode}}->Application->langCode -
{{title}}->Application->pageTitle -
{{heading}}->Application->pageHeading -
{{content}}->Application->pageContent -
{{logBar}}->Application->logBar
Common placeholders:
{{baseHref}}{{styles}}{{scripts}}
Dynamic placeholders:
- any key stored in
Application::var(...) - any unknown property assigned through
Application::__set(...)
Unknown placeholders are replaced with an empty string.
If a widget named footerWidget is available, a placeholder like this:
{{footerWidget}}is replaced with the widget output.
Current implementation detail: each widget placeholder name is replaced only once per render pass because the internal preg_replace(...) call uses a limit of 1.
<!doctype html>
<html lang="{{langCode}}">
<head>
<!-- Base path for relative assets and links. -->
<base href="{{baseHref}}">
<!-- Page title from Application->pageTitle. -->
<title>{{title}}</title>
<!-- CSS tags generated by Application::styles(). -->
{{styles}}
</head>
<body>
<!-- Main application content generated by the MVC flow. -->
<main>{{content}}</main>
<!-- Widget placeholder rendered through Widget::render(). -->
{{footerWidget}}
<!-- JS tags generated by Application::scripts(). -->
{{scripts}}
</body>
</html>use Pair\Core\Application;
use Pair\Html\TemplateRenderer;
$app = Application::getInstance();
// Exposes one custom placeholder available as {{footerNote}}.
$app->var('footerNote', 'Generated at ' . date('c'));
// Parses the template and prints the final HTML.
TemplateRenderer::parse(APPLICATION_PATH . '/templates/default.php');use Pair\Core\Application;
$app = Application::getInstance();
// Assigns multiple template variables in one call.
$app->var([
'sectionClass' => 'orders-shell',
'footerNote' => 'Orders dashboard',
]);Those values become available as {{sectionClass}} and {{footerNote}} inside the template.
Because TemplateRenderer only exposes one public method, most of the useful details are implementation details:
- widget placeholders are resolved before generic placeholders
- only scalar values and objects with
__toString()are rendered fromApplication::getVars() - built-in placeholders such as
{{styles}}and{{scripts}}come fromApplication, not from template vars
-
parse()expects the template file to exist and be readable. - Unknown placeholders are removed, not preserved for a second pass.
- Repeating the same widget placeholder twice in one template currently replaces only the first occurrence.
See also: Widget, Application, View, Template.