add twig to container
This commit is contained in:
parent
c0f9551c0a
commit
e86e3a1238
2 changed files with 13 additions and 4 deletions
|
|
@ -3,16 +3,21 @@
|
|||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use DI\Bridge\Slim\Bridge;
|
||||
use Slim\Views\Twig;
|
||||
use Slim\Views\TwigMiddleware;
|
||||
|
||||
$container = require __DIR__.'/container.php';
|
||||
$app = Bridge::create($container);
|
||||
|
||||
$app->add(TwigMiddleware::create($app, $container->get(Twig::class)));
|
||||
|
||||
// change first param to false for production
|
||||
$app->addErrorMiddleware(true, true, true);
|
||||
|
||||
$app->get('/', function (Response $response) {
|
||||
$response->getBody()->write("Hello world!");
|
||||
return $response;
|
||||
$app->get('/admin', function (Response $response, Twig $twig) {
|
||||
return $twig->render($response, 'admin.html.twig', [
|
||||
'name' => 'John',
|
||||
]);
|
||||
});
|
||||
|
||||
return $app;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,13 @@
|
|||
<?php
|
||||
|
||||
use DI\Container;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
$container = new Container([
|
||||
|
||||
// Note: For production scenarios, cache should be set to some 'path/to/cache'
|
||||
// to store compiled templates (thus avoiding recompilation on every request).
|
||||
// For more information, see https://twig.symfony.com/doc/3.x/api.html#environment-options
|
||||
Twig::class => Twig::create(__DIR__.'/../views/templates', ['cache' => false]),
|
||||
]);
|
||||
|
||||
return $container;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue