slimphp / Twig-View

Slim Framework view helper built on top of the Twig templating component
http://slimframework.com
MIT License
359 stars 87 forks source link

Rendering a template from a middleware #270

Closed arvindpdmn closed 4 months ago

arvindpdmn commented 2 years ago

I am using Twig without a container. The Readme shows examples of rendering a template from index.php where Twig::fromRequest($request) is called. However, doing this inside a middleware throws this error:

Fatal error: Uncaught RuntimeException: Twig could not be found in the server request attributes using the key "view". in C:\Users\arvindpdmn\Documents\workspace\slim\vendor\slim\twig-view\src\Twig.php:74

How do I render a template from a middleware? In particular, there's one middleware that returns early when there's an error. The response will be rendered without calling the other middleware.

arvindpdmn commented 2 years ago

I found one possible solution but I wonder if there's a better way:

$response = new Response();
$twig = Twig::create(__DIR__.'/../views');
$rsp = $twig->fetch('mytemplate.twig', []);
$response->getBody()->write($rsp);
return $response;
jap1968 commented 2 years ago

I am also trying to use Twig in Slim 4 and I am a bit confussed. This repository seems to be oriented for those using a MVC pattern in their applications, but the base slim-skeleton creates a file structure oriented to a ADR (Action Domain Responder) pattern. In my case, I am finding more useful the solution proposed here: https://www.twilio.com/blog/adding-twig-as-a-view-renderer-to-slim-in-php. In this case, the application does not require the package Twig-View.

hatchjaw commented 2 years ago

I guess this defeats the purpose of the middleware approach, but to use twig-view in slim-skeleton Actions, twig must be added as a dependency, so php-di can find it:

// app/dependencies.php
return function ( ContainerBuilder $containerBuilder ) {
    $containerBuilder->addDefinitions( [
        // ... other dependencies
        \Slim\Views\Twig::class => function ( ContainerInterface $c ): \Slim\Views\Twig {
            $loader   = new \Twig\Loader\FilesystemLoader( '/path/to/templates' );
            $settings = [
                'cache' => '/path/to/cache',
                // ...
            ];

            return new \Slim\Views\Twig( $loader, $settings );
        }
    ] );
};

Then add Slim\Views\Twig as a parameter to your Action constructor:

class ViewyAction extends Action {
    private \Slim\Views\Twig $twig;

    public function __construct( LoggerInterface $logger, \Slim\Views\Twig $twig ) {
        parent::__construct( $logger );
        $this->twig = $twig;
    }

    //...

Then you can call Twig::render() in your action() method:

    protected function action(): Response {
        return $this->twig->render( $this->response, 'hi.twig', [
            'name' => $this->args['name']
        ] );
    }
odan commented 4 months ago

It is recommended to use this Twig-View package with a DI container (and dependency injection). I close this as solved.