php-templating
PHP Templating
Template Engine And Escaping
Respect the template engine already established by the project. Do not replace Twig, Blade, or another existing template engine merely because a different engine would be preferred for a new project. Treat the slim/php-view guidance below as the default only when the project has not already selected a template engine.
For Slim-based projects, prefer slim/php-view with native PHP templates, using its layouts and partials for reuse. Templates must support direct step-by-step debugging with Xdebug without template compilation.
When using slim/php-view:
- Configure the layout on the renderer with
$renderer->setLayout('layout.php'). - Render reusable template parts with
$this->fetch('partials/contact.php', ['locale' => $locale, 'contact' => $contact])so their typed input and required rendering context are explicit. - Do not use APIs from other template engines such as
$this->layout(), or bypass PHP-View's partial support with rawincludecalls.
Escape HTML output explicitly through a shared html(string $value): string helper using htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'). Do not apply HTML escaping to plain-text output.
Prefer built-in PHP functionality over additional dependencies when it meets the actual requirements. Introduce another template engine or escaping library only when a concrete requirement justifies it. Handle JavaScript, CSS, and URL contexts separately if they arise.
Start with the framework's simplest suitable option, and justify additional features against actual requirements.