# template-engine **Repository Path**: wfdaj/template-engine ## Basic Information - **Project Name**: template-engine - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-05 - **Last Updated**: 2026-07-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Template engine [![Build Status](https://travis-ci.org/petk/template-engine.svg?branch=master)](https://travis-ci.org/petk/template-engine) Generic template engine written in PHP. It separates logic from the presentation and provides methods for creating nested templates and escaping strings to protect against too common XSS vulnerabilities. ## Installation ```bash composer require petk/template-engine ``` ## Usage Template engine initialization: ```php $template = new Petk\Template\Engine(__DIR__.'/../path/to/templates'); ``` Site-wide configuration parameters can be assigned before rendering so they are available in all templates: ```php $template->assign([ 'siteUrl' => 'https://example.com', // ... ]); ``` Page can be rendered in the controller: ```php echo $template->render('pages/about_us.php', [ 'mainHeading' => 'A short introduction', ]); ``` The `templates/pages/about_us.php`: ```php extends('layout.php', ['title' => 'About us']) ?> start('main_content') ?>

noHtml($mainHeading) ?>

end('main_content') ?> start('scripts') ?> end('scripts') ?> ``` The `templates/layout.php`: ```html ACME :: <?= $title ?? '' ?> block('main_content') ?>
block('scripts') ?> ``` ### Including templates To include a partial template snippet file: ```php include('forms/contact.php') ?> ``` which is equivalent to ``, except that the variable scope is not inherited by the template that included the file. To import variables into the included template snippet file: ```php include('forms/contact.php', ['formHeading' => 'value', 'foo' => 'bar']) ?> ``` ### Blocks Blocks are main building elements that contain template snippets and can be included into the parent file(s). Block is started with the `$this->start('block_name')` call and ends with `$this->end('block_name')`: ```php start('block_name') ?>

Heading

...

end('block_name') ?> ``` ### Appending blocks Block content can be appended to existing blocks by the `$this->append('block_name')`. The `templates/layout.php`: ```html block('content'); ?> block('scripts'); ?> ``` The `templates/pages/index.php`: ```php extends('layout.php'); ?> start('scripts'); ?> end('scripts'); ?> start('content') ?> include('forms/form.php') ?> end('content') ?> ``` The `templates/forms/form.php`: ```php
append('scripts'); ?> end('scripts'); ?> ``` The final rendered page: ```html
``` ### Helpers Registering additional template helpers can be useful when a custom function or class method needs to be called in the template. #### Registering function ```php $template->register('formatDate', function (int $timestamp): string { return gmdate('Y-m-d H:i e', $timestamp - date('Z', $timestamp)); }); ``` #### Registering object method ```php $template->register('doSomething', [$object, 'methodName']); ``` Using helpers in templates: ```php

Time: formatDate(time()) ?>

doSomething('arguments') ?>
``` ### Escaping When protecting against XSS there are two built-in methods provided. To replace all characters to their applicable HTML entities in the given string: ```php noHtml($var) ?> ``` To escape given string and still preserve certain characters as HTML: ```php e($var) ?> ``` ## License and contributing Contributions are most welcome by forking the Git repository over GitHub and sending a pull request.