Using Latte with Slim 4
This article written by Daniel Opitz describes how to use Latte with the Slim Framework.
First, install the Slim Framework and then Latte using Composer:
composer require latte/latte
Configuration
Create a new directory templates
in your project root directory. All templates will be placed there later.
Add a new template
configuration key in your config/defaults.php
file:
$settings['template'] = __DIR__ . '/../templates';
Latte compiles the templates to native PHP code and stores them in a cache on the disk. So they are as fast as if they had been written in native PHP.
Add a new template_temp
configuration key in your config/defaults.php
file: Make sure the directory
{project}/tmp/templates
exists and has read and write access permissions.
$settings['template_temp'] = __DIR__ . '/../tmp/templates';
Latte automatically regenerates the cache every time you change the template, which can be turned off in the production environment to save a little performance:
// change to false in the production environment
$settings['template_auto_refresh'] = true;
Next, add a DI container definitions for the Latte\Engine
class.
<?php
use Latte\Engine;
use Latte\Loaders\FileLoader;
use Psr\Container\ContainerInterface;
// ...
return [
// ...
Engine::class => function (ContainerInterface $container) {
$latte = new Engine();
$settings = $container->get('settings');
$latte->setLoader(new FileLoader($settings['template']));
$latte->setTempDirectory($settings['template_temp']);
$latte->setAutoRefresh($settings['template_auto_refresh']);
return $latte;
},
];
This alone would technically work to render a Latte template, but we also need to make it work with the PSR-7 response object.
For this purpose we create a special TemplateRenderer
class which does this work for us.
So next create a file in src/Renderer/TemplateRenderer.php
and copy/paste this code:
<?php
namespace App\Renderer;
use Latte\Engine;
use Psr\Http\Message\ResponseInterface;
final class TemplateRenderer
{
public function __construct(
private Engine $engine,
) {
}
public function template(
ResponseInterface $response,
string $template,
array $data = [],
): ResponseInterface
{
$string = $this->engine->renderToString($template, $data);
$response->getBody()->write($string);
return $response;
}
}
Usage
Instead of using the Latte Engine object directly we use the TemplateRenderer
object to render the template into a
PSR-7 compatible object.
A typical Action handler class might look like this to render a template with the name home.latte
:
<?php
namespace App\Action\Home;
use App\Renderer\TemplateRenderer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class HomeAction
{
public function __construct(
private TemplateRenderer $renderer,
) {
}
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
): ResponseInterface
{
$viewData = [
'items' => ['one', 'two', 'three'],
];
return $this->renderer->template($response, 'home.latte', $viewData);
}
}
To make it work, create a template file in templates/home.latte
with this content:
<ul n:if="$items">
{foreach $items as $item}
<li id="item-{$iterator->counter}">{$item|capitalize}</li>
{/foreach}
</ul>
If everything is configured correctly you should see the following output:
One
Two
Three