This repository has been archived on 2024-06-20. You can view files and clone it, but cannot push or open issues or pull requests.
peryoudow/vendor/league/plates/docs/templates/index.md
2020-01-06 14:23:12 +01:00

1.8 KiB

layout permalink title
default templates/ Templates

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 League\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 League\Plates\Engine('/path/to/templates');

// Create a new template
$template = new League\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();