Templating Engine
, (*1)
A simple, easy to follow PHP templating engine. Designed to be forked, modified, extended and hacked., (*2)
Example Usage
This templating engine supports inheritance through blocks. Child templates declare blocks which can be overridden, extended and displayed by parent templates., (*3)
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., (*4)
All templates must follow the namespace::path/to/template
format., (*5)
parent('app::layout', ['title' => 'Blog Post: '.$title]); ?>
block('content', function ($params) { extract($params); ?>
<article>
<header>
<h1><?=$this->escape($this->caps($title));?></h1>
</header>
<main>
<?php foreach($paragraphs as $paragraph): ?>
<p>
<?=$this->escape($paragraph);?>
</p>
<?php endforeach; ?>
</main>
</article>
<html>
<head>
<title><?=$this->escape($title);?></title>
</head>
<body>
<?=$this->block('content');?>
</body>
</html>
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
., (*6)
The default template extension is phtml
, but this can be overridden., (*7)
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., (*8)
/**
* 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);
Authors
Change Log
This project maintains a change log file, (*9)
License
The MIT License (MIT). Please see LICENSE for more information., (*10)