Latte Slim wrapper
Latte templating engine wrapper for Slim microframework., (*1)
You can use this small library to integrate Latte templates into a project based on Slim framework., (*2)
This project was created for course APV on Mendel University in Brno., (*3)
You can download this library using Composer:, (*4)
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., (*5)
$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., (*6)
Pass multiple values into a template. The $params
array must be associative., (*7)
Render a template given by $name
with set of template variables given by $params
associative array and create
new Response object., (*8)
Returns new instance of Response object which can be returned from route or middleware., (*9)
Add a custom Latte filter - {$variable|customFilter}
., (*10)
Add a custom Latte macro - {customMacro param}
., (*11)
Define a dependency for Slim framework (change templates source folder and cache directory location if needed):, (*12)
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., (*13)
$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:, (*14)
<!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:, (*15)
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:, (*16)
$app->get('/test', function (Request $request, Response $response, $args) { //route implementation })->setName('routeName');
This also works with route placeholders:, (*17)
$app->get('/test/{something}', function (Request $request, Response $response, $args) { //route implementation })->setName('routeName');
In template use:, (*18)
<a href="{link routeName ['something' => 123]}">link</a>