add Twig for template rendering
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
7cc2d40660
commit
92a4d2fcf8
7 changed files with 333 additions and 81 deletions
|
|
@ -5,9 +5,13 @@ namespace NoteCat;
|
|||
|
||||
use DI\Container;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
class Dependencies
|
||||
{
|
||||
private const TWIG = 'view';
|
||||
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
|
||||
|
||||
public static function createContainer(): Container
|
||||
{
|
||||
$container = new Container();
|
||||
|
|
@ -15,6 +19,7 @@ class Dependencies
|
|||
// Controllers
|
||||
$container->set(HelloWorldController::class, function (ContainerInterface $c) {
|
||||
return new HelloWorldController(
|
||||
$c->get(self::TWIG),
|
||||
$c->get(HelloWorld::class)
|
||||
);
|
||||
});
|
||||
|
|
@ -24,6 +29,12 @@ class Dependencies
|
|||
return new HelloWorld();
|
||||
});
|
||||
|
||||
$container->set(self::TWIG, function (ContainerInterface $c) {
|
||||
// TODO cache
|
||||
$twigSettings = [];
|
||||
return Twig::create(self::TWIG_TEMPLATE_DIR, $twigSettings);
|
||||
});
|
||||
|
||||
return $container;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,19 +5,25 @@ namespace NoteCat;
|
|||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
class HelloWorldController
|
||||
{
|
||||
private Twig $view;
|
||||
private HelloWorld $hello;
|
||||
|
||||
public function __construct(HelloWorld $hello)
|
||||
public function __construct(Twig $view, HelloWorld $hello)
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->hello = $hello;
|
||||
}
|
||||
|
||||
public function hello(Request $request, Response $response): Response
|
||||
{
|
||||
$response->getBody()->write('<b>' . $this->hello->getHello() . "</b>\n");
|
||||
return $response;
|
||||
$renderData = [
|
||||
'hello' => $this->hello->getHello(),
|
||||
];
|
||||
|
||||
return $this->view->render($response, 'helloworld.html', $renderData);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
src/Middlewares.php
Normal file
15
src/Middlewares.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace NoteCat;
|
||||
|
||||
use Slim\App;
|
||||
use Slim\Views\TwigMiddleware;
|
||||
|
||||
class Middlewares
|
||||
{
|
||||
public static function setMiddlewares(App $app): void
|
||||
{
|
||||
$app->add(TwigMiddleware::createFromContainer($app));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue