A tiny PHP templating engine.
Tempearly uses Regular Expressions to provide fast but still powerful features to your templates.
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:
If you don't use Composer you can also download a source archive from GitHub and include the files manually.
(Replace 'master' with your desired version)
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
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:
You may structure your templates in subdirectories. Then provide the relative path as template id:
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.
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
Nevertheless I recommend only using simple types and callables.
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.