opsbears / piccolo-templating
Templating tools for the Piccolo framework
This package is not auto-updated.
Last update: 2020-09-18 20:28:28 UTC
README
This module provides two things:
- A standardized
TemplateEngine
interface that all template engine providers should implement. - The
TemplateRenderingChain
class that can try multiple templating engines in order to find and render a specific template.
Installation
Although this module will most likely be pulled in as a dependency from a different module, you can explicitly install it using composer:
composer require opsbears/piccolo-templating
Configuration
By default, you don't need to care about this module too much, as pulling in at least one templating engine will automatically load this module too. However, if you wish, you can explicitly load it in your configuration:
'modules' => [
//...
TemplatingModule::class,
//...
],
Usage
Using a template engine directly
If you just want to use one templating engine, that's easy. Just load the template engine module, which will set up
the dependency injection container to pass that engine instead of the TemplateEngine
interface. You can then
request the TemplateEngine
interface as a dependency:
function myFunction(TemplateEngine $tpl) {
$tpl->renderFile(
'/path/to/template/directory',
'/path/to/template/directory/templateName.twig'
['myVariable' => 'mydata]
);
}
Warning! If you load multiple template engine modules, the last engine configured will take precedence.
Using the rendering chain
If you want to support more than one template engine, you can use the TemplateRenderingChain
class to do that. The
template engines will automatically register themselves with it, so all you have to do is request the rendering chain
as a dependency:
function myFunction(TemplateRenderingChain $renderingChain) {
$renderingChain->render(
'/path/to/template/directory',
'templateName'
['myVariable' => 'mydata]
);
}
This will then automatically look in /path/to/template/directory
for the file templateName
if the extension
supported by each template engine. If it finds one, it will render it and pass back the result.