A library which provides adapters to different PHP templating engines
MetaTemplate follows the PSR-0 standard for class loading, so its
classes can be loaded with every compliant autoloader, such as
Zend_Loader or the Symfony Classloader.
Installation is easy, just register the
MetaTemplate namespace with
your autoloader and point it to the directory where you copied the
contents of the
lib directory to.
Most of the time you will be consuming engines via the
It provides some methods, which take a filename and return a template
MetaTemplate ships with these adapters/engines by default:
PHPTemplate, mapped to
PhpSass, mapped to
MarkdownTemplate(requires php-markdown to be loaded), mapped to
LessTemplate(requires Less to be installed via node), mapped to
.less(Requires Symfony_Process 2)
MustacheTemplate(requires phly_mustache), mapped to
MetaTemplate\Template class has a static
create method, which
creates template instances from a given path.
<?php use MetaTemplate\Template; $template = Template::create('/path/to/foo.phtml'); echo get_class($template); // => "\MetaTemplate\Template\PHPTemplate"
All templates implement the
which provides a
render method which, you probably guessed it, returns
the rendered contents.
render method takes two arguments, which are both optional:
$context: The template's context, in most engines this is what
$thisinside the template script refers to.
$locals: A array, which defines the local variables available in the template script.
These two arguments allow to inject the data into the template script.
If the templating engine does not have to support contexts or locals, these two arguments are simply ignored. This is the case with the Markdown and Less engines.
Digging one layer deeper
If want to setup all engine mappings up by yourself and have no
default setup of Engines, then the
is for you.
This class simply provides the instance behind the static methods of
To map a template class to one or more file extensions, just call
register method the same way you would on the
$registry = new \MetaTemplate\Util\EngineRegistry; $registry->register('\\MetaTemplate\\Template\\LessTemplate', 'less');
You can then use the
create method to create new Template instances
for the provided path.
Writing your own engines
As previously noted, all templating engines need to implement the
MetaTemplate\Template\TemplateInterface. Though, there is the
MetaTemplate\Template\Base class, which you can inherit from, which
handles some redundant aspects, such as template data loading.
Base class defines a
prepare method, which lets you hook into
the template initialization. This method is called before the
Your template's content is loaded into the
Look at the supplied templating engines, if you need some examples.