# Twig View - Slim4

**URL:** <https://discourse.slimframework.com/t/twig-view-slim4/4186>\
**Category:** Questions\
**Created:** [May 24, 2020, 8:05am UTC](https://discourse.slimframework.com/t/twig-view-slim4/4186 "2020-05-24T08:05:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vino](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@Vino](https://discourse.slimframework.com/u/Vino)\
**Post date:** [May 24, 2020, 8:05am UTC](https://discourse.slimframework.com/t/twig-view-slim4/4186/1 "2020-05-24T08:05:13Z")

</div>

Hi All,

```
  Request your help in setting up Twig, We are trying to migrate our application from Slim 3.x to Slim 4.x , hence all these issues and request your guidance.

```

**container.php**

```auto
<?php

use Slim\App;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Selective\Config\Configuration;

return [
    Configuration::class => function () {
        return new Configuration(require __DIR__. '/settings.php');
    },

    App::class => function (ContainerInterface $container) {
        AppFactory::setContainer($container);
        $app = AppFactory::create();
        return $app;
    },
    TwigMiddleware::class => function (ContainerInterface $container) {
        return TwigMiddleware::createFromContainer($container->get(App::class), Twig::class);
    },

    Twig::class => function (ContainerInterface $container) {
        $config = $container->get(Configuration::class);
        $settings = $config->getArray('twig');
        $options = $settings['options'];
        $options['cache'] = $options['cache_enabled'] ? $options['cache_path'] : false;
        $twig = Twig::create($settings['paths'], $options); 
        return $twig;
  },
];

?>

```

**middleware**

```auto
<?php

use Slim\App;
use Slim\Views\TwigMiddleware;
use Selective\Config\Configuration;
use Selective\BasePath\BasePathMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\Middleware\ErrorMiddleware;

return function (App $app) {
       $app->addRoutingMiddleware();
       $app->add(TwigMiddleware::class);
       $app->add(BasePathMiddleware::class);
       $app->add(ErrorMiddleware::class);
};
?>

```

**routes.php**

```auto
<?php
use Slim\App;
use Slim\Routing\RouteContext;
use Slim\Routing\RouteCollectorProxy;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Views\Twig;

return function(App $app) {
################################################################
# Page 1.0
################################################################
$app->get('/', function($request, $response) {
	  return $this->$view->render($response, 'page_dashboard.html');
})->setName('root');

$app->get('/login', function($request, $response) {
      return $this->$view->render($response, 'page_login.html');
})->setName('login');

};

?>
```

---

<div class="post-metadata">

**Author:** ![faridsilva](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/faridsilva/32/202_2.png) [@faridsilva](https://discourse.slimframework.com/u/faridsilva)\
**Post date:** [May 26, 2020, 5:30pm UTC](https://discourse.slimframework.com/t/twig-view-slim4/4186/2 "2020-05-26T17:30:30Z")

</div>

Hello Vino, I think this post can give some help

> <https://stackoverflow.com/questions/57471005/how-to-add-twig-view-in-slimframework-v4>

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 26, 2020, 8:49pm UTC](https://discourse.slimframework.com/t/twig-view-slim4/4186/3 "2020-05-26T20:49:10Z")

</div>

You should provide more details, e.g. the error message.

This line is not valid:

> [@Vino](#):
>
> `return $this->$view->render`

Within class methods non-static properties may be accessed by using `->` (Object Operator):

`return $this->view->render($response, 'page_dashboard.html');`

[https://www.php.net/manual/en/language.oop5.properties.php](https://www.php.net/manual/en/language.oop5.properties.php)

You can use PhpStorm, a linter and phpstan to check your code.

---

<div class="post-metadata">

**Author:** ![Vino](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@Vino](https://discourse.slimframework.com/u/Vino)\
**Post date:** [May 27, 2020, 10:38am UTC](https://discourse.slimframework.com/t/twig-view-slim4/4186/4 "2020-05-27T10:38:00Z")

</div>

Hi Odan,

```
Thank you very much, and sorry for not providing more information.
```
