A simple, easy to follow PHP templating engine

v0.1.0 2015-10-27 13:32 UTC


Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Code Climate Total Downloads License

A simple, easy to follow PHP templating engine. Designed to be forked, modified, extended and hacked.

Example Usage

This templating engine supports inheritance through blocks. Child templates declare blocks which can be overridden, extended and displayed by parent templates.

Child templates can declare a single parent template at any point using the parent() method which also provides the opportunity to modify the variables that are in scope.

All templates must follow the namespace::path/to/template format.

<?php $this->parent('app::layout', ['title' => 'Blog Post: '.$title]); ?>

<?php $this->block('content', function ($params) { extract($params); ?>
            <?php foreach($paragraphs as $paragraph): ?>
            <?php endforeach; ?>
<?php }); ?>

Namespaces and function callbacks are registered with the templating engine when it is constructed. Function callbacks are available as methods within the template context and must be callable.

The default template extension is phtml, but this can be overridden.

use SitePoint\TemplatingEngine\TemplatingEngine;

$engine = new TemplatingEngine(
    ['app'  => '/path/to/templates/app'], // The namespaces to register
    ['caps' => 'strtoupper'],             // Function callbacks to register inside the template context
    'phtml'                               // The extension of the templates (defaults to phtml)

$params = [
    'title' => 'My Blog Post',
    'paragraphs' => [
        'My first paragraph.',
        'My second paragraph.',

echo $engine->render('app::post', $params);

Template Context Methods

The following methods are available by default within the template context.

 * Define a parent template.
 * @param string $template The name of the parent template.
 * @param array  $params   Parameters to add to the parent template context
 * @throws EngineException If a parent template has already been defined.
public function parent($template, array $params = []);
 * Insert a template.
 * @param string $template The name of the template.
 * @param array  $params   Parameters to add to the template context
public function insert($template, array $params = []);
 * Render a block.
 * @param string $name The name of the block.
public function block($name, callable $callback = null);
 * Escape a string for safe output as HTML.
 * @param string $raw The unescaped string.
 * @return string The escaped HTML output.
public function escape($raw);


Change Log

This project maintains a change log file


The MIT License (MIT). Please see LICENSE for more information.