Ride: Template Library
Template abstraction library of the PHP Ride framework., (*1)
What's In The Library
Engine
The implementation of Engine will implement the actual template engine through the Template interface.
A template engine decides the features and the syntax (or language) to write in., (*2)
EngineModel
An EngineModel is an interface for the data store of the available template engines., (*3)
Theme
Optional support for template themes through the Theme interface.
Each theme can implement it's own version of a template.
This makes it possible to return a different representation of the same data., (*4)
A theme chooses the engines it's available in., (*5)
You can make a theme hierarchical by defining it's parent.
This makes it easy to create subthemes and helps the engine to fallback when a resource could not be found., (*6)
ThemeModel
A ThemeModel is an interface for the data store of the available themes., (*7)
Template
A Template instance defines the resource and the variables to render the template.
With a ThemedTemplate, you can specify a template in a specific theme., (*8)
The resource should be generic so it's not bound to a specific template engine.
For example: use base/index instead of view/smarty/themes/bootstrap/base/index.tpl., (*9)
Code Sample
Check this code sample to get an idea of this library:, (*10)
<?php
use ride\library\template\engine\EngineModel;
use ride\library\template\engine\ThemeModel;
use ride\library\template\TemplateFacade;
function foo(EngineModel $engineModel, ThemeModel $themeModel) {
$resource = 'path/to/resource';
$variables = array('var1' => 'value1');
// template facade should be made available by your implementation
$templateFacade = TemplateFacade($engineModel, $themeModel);
$templateFacade->setDefaultEngine('smarty');
$templateFacade->setDefaultTheme('my-theme');
// a simple template rendering
$template = $templateFacade->createTemplate($resource, $variables);
$template->set('var2', 'value2');
$output = $templateFacade->render($template);
// a template for a non-default theme
$template = $templateFacade->createTemplate($resource, $variables, 'overriden-theme');
// a template for a non-default theme with a specific engine
$template = $templateFacade->createTemplate($resource, $variables, 'overriden-theme', 'my-engine');
// get the file representation of a specific template
$file = $templateFacade->getFile($template); // ride\library\system\file\File
// get's the available template for a specific namespace
$engine = null;
$theme = null;
$templates = $templateFacade->getFiles('path/to', $engine, $theme);
echo $engine; // smarty
echo $theme; // my-theme
// reads the meta from the template comments in the beginning of the resource
// syntax: [key: value([; key: value])*]
// eg for Smarty: {* name: My Title; action: index *}
// will return array('name' => 'My Title', 'action' => 'index')
$meta = $templateFacade->getTemplateMeta($template);
}
Implementations
For more examples, you can check the following implementations of this library:
- ride/lib-template-php
- ride/lib-template-smarty
- ride/lib-template-twig
- ride/app-template-php
- ride/app-template-smarty
- ride/app-template-twig, (*11)
Installation
You can use Composer to install this library., (*12)
composer require ride/lib-template