Templates
Plates templates are very simple PHP objects. Generally you'll want to create these using the two factory methods, make()
and render()
, in the engine. For example:
// Create new Plates instance
$templates = new Jinya\Plates\Engine('/path/to/templates');
// Render a template in a subdirectory
echo $templates->render('partials/header');
// Render a template
echo $templates->render('profile', ['name' => 'Jonathan']);
For more information about how Plates is designed to be easily added to your application, see the section on dependency injection.
Manually creating templates
It's also possible to create templates manually. The only dependency they require is an instance of the engine object. For example:
// Create new Plates instance
$templates = new Jinya\Plates\Engine('/path/to/templates');
// Create a new template
$template = new Jinya\Plates\Template\Template($templates, 'profile');
// Render the template
echo $template->render(['name' => 'Jonathan']);
// You can also render the template using the toString() magic method
echo $template;
Check if a template exists
When dynamically loading templates, you may need to check if they exist. This can be done using the engine's exists()
method:
if ($templates->exists('articles::beginners_guide')) {
// It exists!
}
You can also run this check on an existing template:
if ($template->exists()) {
// It exists!
}
Get a template path
To get a template path from its name, use the engine's path()
method:
$path = $templates->path('articles::beginners_guide');
You can also get the path from an existing template:
$path = $template->path();