Latte templating engine wrapper for Slim microframework.
You can use this small library to integrate Latte templates into a project based on Slim framework.
This project was created for course APV on Mendel University in Brno.
You can download this library using Composer:
composer require ujpef/latte-view
Create an instance of Latte wrapper. Pass instance of configured Latte engine. You should configure Latte engine before you pass it to the wrapper: set up templates path and set up cache folder for templates.
$engine = new \Latte\Engine\Engine();
$engine->setLoader(new \Latte\Loaders\FileLoader(__DIR__ . '/../templates/'));
$engine->setTempDirectory(__DIR__ . '/../cache');
$latteView = new \Ujpef\LatteView\LatteView($engine);
Make template variable called $name
with $param
value.
Pass multiple values into a template. The $params
array must be associative.
Render a template given by $name
with set of template variables given by $params
associative array and create
new Response object.
Returns new instance of Response object which can be returned from route or middleware.
Add a custom Latte filter - {$variable|customFilter}
.
Add a custom Latte macro - {customMacro param}
.
Define a dependency for Slim framework (change templates source folder and cache directory location if needed):
use Latte\Engine;
use Latte\Loaders\FileLoader;
use Ujpef\LatteView;
$container['view'] = function ($container) use ($settings) {
$engine = new Engine();
$engine->setLoader(new FileLoader(__DIR__ . '/../templates/'));
$engine->setTempDirectory(__DIR__ . '/../cache');
$latteView = new LatteView($engine);
return $latteView;
};
To return result of Latte template rendering call the render()
method.
$app->get('/[{name}]', function (Request $request, Response $response, $args) {
$tplVars = [
'variable' => 123
];
return $this->view->render($response, 'index.latte', $tplVars);
})->setName('index');
In template use:
<!DOCTYPE html>
<html>
<head>
<title>test template</title>
</head>
<body>
contents of variable: {$variable}
</body>
</html>
To use Slim's build in router in a Latte macro like this
{link routeName}
add following lines into dependency definition:
use Latte\Engine;
use Latte\MacroNode;
use Latte\PhpWriter;
use Latte\Loaders\FileLoader;
use Ujpef\LatteView;
$container['view'] = function ($container) use ($settings) {
$engine = new Engine();
$engine->setLoader(new FileLoader(__DIR__ . '/../templates/'));
$engine->setTempDirectory(__DIR__ . '/../cache');
$latteView = new LatteView($engine);
$latteView->addParam('router', $container->router);
$latteView->addMacro('link', function (MacroNode $node, PhpWriter $writer) {
if (strpos($node->args, ' ') !== false) {
return $writer->write("echo \$router->pathFor(%node.word, %node.args);");
} else {
return $writer->write("echo \$router->pathFor(%node.word);");
}
});
return $latteView;
};
Remember to set names for your routes:
$app->get('/test', function (Request $request, Response $response, $args) {
//route implementation
})->setName('routeName');
This also works with route placeholders:
$app->get('/test/{something}', function (Request $request, Response $response, $args) {
//route implementation
})->setName('routeName');
In template use:
<a href="{link routeName ['something' => 123]}">link</a>