sitepoint / templating-engine
A simple, easy to follow PHP templating engine
Requires (Dev)
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-12-21 20:02:31 UTC
README
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); ?> <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> <?php }); ?>
<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
.
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);
Authors
Change Log
This project maintains a change log file
License
The MIT License (MIT). Please see LICENSE for more information.