Wallogit.com
2017 © Pedro Peláez
Simple template utility
Zenplate is a simple template engine for PHP. As a matter of fact, PHP is by design a template engine already, so what this library really does is simply enabling you to load a template file and pass a bunch of variables to it. Such variables will only be visible inside the template itself (template scoped variables)., (*1)
Furthermore, the library lets you load/register a bunch of helpers that can be then used inside your templates to make things easier at the markup generation level., (*2)
To use Zenplate it is mandatory that you know how to use native php inside a template using alternative notation. Here's an article on the matter that you should read before moving on to the remainder of this tutorial., (*3)
Here's an example of how a template can be loaded using Zenplate., (*4)
use zenplate\Zenplate;
$html = Zenplate::load("path-to-your-templates/template-1.php");
echo $html; // this will simply print the content of your `template-1.php` template.
Now, let's say we want to pass data to the template. We can do so like that:, (*5)
use zenplate\Zenplate;
$html = Zenplate::load("path-to-your-templates/template-1.php", [
"name" => "Zenplate",
"version" => 1
]);
Assuming the template is defined like so:, (*6)
// file: template-1.phpHello, this is the library = $name ?> which is currently at version = $version ?>, (*7)
We will then eventually obtain this markup:, (*8)
<p>Hello, this is the library Zemplate which is currently at version 1</p>
If you want to spice up Zenplate, you can do so by loading custom helpers that you are going to develop youself. For instance, let's say we want to add a trivial helper which perform some simple text transformation. We can do it like so:, (*9)
use zenplate\helper\Helper;
class StringHelper extends Helper
{
public function upperc($str)
{
return strtoupper($str);
}
}
// and then, elsewhere, where we bootstrap our app we go like:
Zenplate::addHelper("my_string_helper", new StringHelper());
Once that's done, in a template loaded using Zenplate, we will be in the position to do the following:, (*10)
<p><?= $helpers["my_string_helper"]->upperc($a_variable) ?></p>
Zenplate comes with some pre-loaded helpers. Have a good look at this folder to see what they are like and what they are for., (*11)
They are registered as time, shortener and mprinter if you ever wanted to use them in your templates., (*12)
composer install composer run-script test