bfitech/zaplate

A pseudo-template engine.

1.4.1 2020-02-05 14:44 UTC

This package is auto-updated.

Last update: 2024-05-06 00:21:44 UTC


README

PHP is an HTML template engine. This only sugars it up a bit, enough to render mostly-static HTML, which is all zap* ever needs due to it being RESTful-oriented.

Use other full-blown templating engines for rendering complex dynamic HTML.

Latest Stable Version Latest Unstable Version Build Status Codecov GitHub license

Installation

Install it from Packagist:

$ composer -vvv require bfitech/zaplate

Sample Usage

template.php

<p><?php echo $group ?></p>
<ul>
	<?php foreach ($members as $member): ?>
	<li><?php echo $member ?></li>
	<?php endforeach; ?>
</ul>

renderer.php

<?php

require __DIR__ . '/vendor/autoload.php';

class Filter {
	public function whoami($name) {
		if (is_string($name))
			return $name;
		return array_map(function($iname){
			if (stripos($iname, 'jekyll') !== false)
				return 'Mr Hyde';
			return $iname;
		}, $name);
	}
}

BFITech\ZapTemplate\Template::load('template.php', [
	'group' => "Extraordinary Gents",
	'members' => [
		'Allan Quatermain',
		'Henry Jekyll',
	],
], [
	[(new Filter), 'whoami'],
]);

Run it:

$ php renderer.php
<p>Extraordinary Gents</p>
<ul>
	<li>Allan Quatermain</li>
	<li>Mr Hyde</li>
</ul>

PROTIP: If you want to minify the HTML on production, run your template file through yourmy favorite minifier prior to rendering it, e.g.:

$ [ ! -f template.orig.php ] && cp template.{,orig.}php
$ php -w template.orig.php | \
> html-minifier \
>   --collapse-whitespace \
>   --trim-custom-fragments > \
> template.php

Aaand ... huzzah!

$ php renderer.php
<p>Extraordinary Gents</p><ul><li>Allan Quatermain</li><li>Mr Hyde</li></ul>

Documentation

Documentation available with:

$ doxygen
$ x-www-browser docs/html/index.html