Tempearly

Packagist Packagist GitHub tag Packagist GitHub issues GitHub pull requests GitHub last commit GitHub repo size in bytes GitHub code size in bytes Documentation Status

A tiny PHP templating engine.
Tempearly uses Regular Expressions to provide fast but still powerful features to your templates.

Installation

Composer

Require the module through Composer:

$ composer require mcstreetguy/tempearly

Tempearly registers itself and it's components in the PSR-4 namespace. Ensure you include Composer's autoloader:

include_once 'vendor/autoload.php';

Manually

If you don't use Composer you can also download a source archive from GitHub and include the files manually.

https://github.com/MCStreetguy/Tempearly/archive/master.zip

(Replace 'master' with your desired version)

Usage

First initiate a Tempearly instance by providing the path to your template folder and optionally the file extension of your templates. The extension defaults to .tpl.html but the path has to be provided.

$engine = new MCStreetguy\Tempearly('my/template/folder','.html');

Now you can invoke the rendering process of any template within your template folder as following:

$engine->render('my-template');

You may structure your templates in subdirectories. Then provide the relative path as template id:

$engine->render('login/view/registration');

The render(...) function returns the parsed template as string. Thus you may echo it directly to your page, or work on it further as you wish.

User Context

You can provide an optional array of context-variables to the rendering process as seconds argument:

$engine->render('my-template',array(
  'foo' => 'Hello World!',
  'bar' => true,
  'baz' => 27
));

You can also directly pass a Context instance. The use of that class is especially handy if you need to fill it dynamically instead of just passing static values as array.

$context = new MCStreetguy\Tempearly\Context();

$context->push('foo','Hello World!');
$context->push('bar',true);
$context->push('baz',27);

$engine->render('my-template',$context);

You may also pass an array to the Context constructor, making it use that array as context contents. Modifications are still possible afterwards.

$context = new MCStreetguy\Tempearly\Context(array(
  'foo' => 'Hello World!',
  'bar' => true
));

$context->push('baz',27);

$engine->render('my-template',$context);

There is no restriction to the context values, the parameter is defined as mixed. Nevertheless I recommend only using simple types and callables.

Minification

Tempearly provides a static minify function that removes all unnecessary whitespaces from an HTML string. Whitespaces are considered unnecessary if they occur multiple times or outside of HTML tags.

Tempearly::minify($myTemplate);