# Help using slim/twig-view (subclasses)

**URL:** <https://discourse.slimframework.com/t/help-using-slim-twig-view-subclasses/2343>\
**Category:** Questions\
**Created:** [April 16, 2018, 1:16pm UTC](https://discourse.slimframework.com/t/help-using-slim-twig-view-subclasses/2343 "2018-04-16T13:16:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://discourse.slimframework.com/u/devnull)\
**Post date:** [April 16, 2018, 1:16pm UTC](https://discourse.slimframework.com/t/help-using-slim-twig-view-subclasses/2343/1 "2018-04-16T13:16:43Z")

</div>

Hi,

The docs show how to get _slim/twig-view_ working, but does so in spaghetti style.

I’m trying to do it more tidily but am having trouble getting it working, I get an error

> Message: Call to a member function render() on null

My _index.php_ looks like this:

```
$container = $app->getContainer();
$view = new \CMS\View\Main($container);
$container['view'] = $view->getTwig();
$app->get('/go/{pageset}','\CMS\Controller\Unauth\Go:go')->setName('go');

```

_\CMS\View\Main_ looks like this:

```
<?php
namespace CMS\View;
use \Slim\Views\Twig;
use \Slim\Views\TwigExtension;
use \CMS\Config\SlimConf;
class Main {
protected $ci;
private $view;
//Constructor
public function __construct(\Slim\Container $ci) {
    $this->ci = $ci;
}
public function getTwig() {
   $this->view = new Twig ( SlimConf::TWIG_TEMPLATE_DIR, [
        'cache' => SlimConf::TWIG_CACHE_DIR
    ]);
    $basePath = rtrim(str_ireplace('index.php', '', $this->ci['request']->getUri()->getBasePath()), '/');
    $this->view->addExtension(new TwigExtension($this->ci['router'], $basePath));
    return $this->view;
}
}
?> 

```

My _Go.php_ glass looks like this:

```
namespace CMS\Controller\Unauth;
class Go {
protected $ci;
private $pageset;
//Constructor
public function __construct(\Slim\Container $ci) {
    $this->ci = $ci;
}
public function go($request, $response, $args) {
    $this->pageset=$args['pageset'];
    return $this->ci->view->render($response,'master.html');
}
}

```

If I replace the view return with something like this:

```
return $response->withStatus(403)
                    ->write("FOOBAR !");

```

It returns ok. So my Slim & class is setup generally ok, its just the Twig that’s broken !

---

<div class="post-metadata">

**Author:** ![djsipe](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/djsipe/32/651_2.png) [@djsipe](https://discourse.slimframework.com/u/djsipe)\
**Post date:** [April 18, 2018, 6:40pm UTC](https://discourse.slimframework.com/t/help-using-slim-twig-view-subclasses/2343/2 "2018-04-18T18:40:52Z")

</div>

I think you may need to rework your `index.php` a bit to something more like this:

```php
$container = $app->getContainer();
$container['view'] = function () {
    $view = new \CMS\View\Main($container);
    $view->getTwig();
};
$app->get('/go/{pageset}','\CMS\Controller\Unauth\Go:go')->setName('go');

```

Slim uses [Pimple](https://pimple.symfony.com) on the back end for dependencies. When using Pimple, you have to define your dependencies as callbacks. The use of callbacks allows your code to only create service instances when the service is actually needed.
