0

Trying to work with the Full Example from Slim Framework + Doctrine. I am trying to add Twig (slim/twig-view) to the mix.

Added src/Providers/Twig

<?php

declare(strict_types=1);

namespace App\Provider;

use Pimple\{Container, ServiceProviderInterface};
use Slim\{Http\Environment, Http\Uri, Views\TwigExtension};

class Twig implements ServiceProviderInterface
{
    public function register(Container $c)
    {
        $c[\Slim\Views\Twig::class] = function ($c) {
            $view = new \Slim\Views\Twig(['settings']['view']['template_path'], ['settings']['view'] 
            ['twig']);

            // Instantiate and add Slim specific extension
            $router = $c('router');
            $uri = Uri::createFromEnvironment(new Environment($_SERVER));
            $view->addExtension(new TwigExtension($router, $uri));

            return $view;
        };
    }
}

Added src/Action/HomeAction

<?php

declare(strict_types=1);

namespace App\Action;

use Slim\{Http, Views\Twig};

class Home
{
    private $view;

    public function __construct(Twig $view)
    {
        $this->view = $view;
    }

    public function __invoke(Http\Request $request, Http\Response $response, $args)
    {
       $result = $this->view->render($response,'index.twig', $data);

        return $response;
    }
}

Added this to src/Provider/Slim

$c[Home::class] = function (Container $c): Home {
        return new Home($c[\Slim\Views\Twig::class]);
    };

I feel like this the issue is here but still learning my way through this.

Jiinx
  • 1
  • 2
  • Does `$c` need to be added in `new \Slim\Views\Twig(['settings']...`? – Nigel Ren Feb 23 '20 at 07:56
  • When I use; `$view = new \Slim\Views\Twig( $c['settings']['view']['template_path'], $c['settings']['view']['twig']);` I get `Function name must be a string` – Jiinx Feb 23 '20 at 15:36
  • You have tagged this as [tag:slim-3] but the linked GitHub sample projects is using slim v4. In case take a look here: https://stackoverflow.com/questions/57471005/how-to-add-twig-view-in-slimframework-v4 – wp78de Feb 24 '20 at 20:46
  • I should have specified (my apologies) that I am using commit [dfa2ef3](https://github.com/1ma/Slim-Doctrine-Demo/tree/dfa2ef3500aedc98b01588c02bce822cf19a3d3a) (Slim3 version) – Jiinx Feb 24 '20 at 21:17

0 Answers0