Giter Club home page Giter Club logo

Comments (3)

jap1968 avatar jap1968 commented on June 16, 2024 2

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.

from twig-view.

arvindpdmn avatar arvindpdmn commented on June 16, 2024

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;

from twig-view.

hatchjaw avatar hatchjaw commented on June 16, 2024

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']
        ] );
    }

from twig-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.