# Return twig template by controller?

**URL:** <https://discourse.slimframework.com/t/return-twig-template-by-controller/890>\
**Category:** Questions\
**Created:** [November 9, 2016, 2:05pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890 "2016-11-09T14:05:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![callerd](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@callerd](https://discourse.slimframework.com/u/callerd)\
**Post date:** [November 9, 2016, 2:05pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/1 "2016-11-09T14:05:25Z")

</div>

In my controller file I have:

`use Slim\Views\Twig;`

in Controller function I tried something like:  
`return $view->render($response, 'home.twig');`

I get there error message: Call to a member function render() on null

What do I have to include or use to solve this render() problem?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 9, 2016, 2:24pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/2 "2016-11-09T14:24:16Z")

</div>

What does your controller’s constructor look like? Typically you would do something more

```php
return $this->view->render($response, 'home.twig');

```

---

<div class="post-metadata">

**Author:** ![callerd](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@callerd](https://discourse.slimframework.com/u/callerd)\
**Post date:** [November 9, 2016, 2:28pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/3 "2016-11-09T14:28:15Z")

</div>

I need to get there some more data and for some other pages I need also db inserts etc…

My standard code is:  
namespace App\Controllers;

```
use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Illuminate\Database\Query\Builder;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class SitecheckController
{
    private $view;
    private $logger;
    protected $table;

    public function index(Request $request, Response $response, $args)
    {
        //return "contr. return msg!";
        return $this->view->render($response, 'home.twig');
    }
}

```

I tried also with $this, but the same problem…

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 9, 2016, 2:37pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/4 "2016-11-09T14:37:17Z")

</div>

How are you constructing `$view` ? Do you have a construct or invoke method in that controller?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 9, 2016, 2:38pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/5 "2016-11-09T14:38:14Z")

</div>

See [https://github.com/akrabat/slim-bookshelf/blob/master/app/src/Bookshelf/AuthorController.php#L16](https://github.com/akrabat/slim-bookshelf/blob/master/app/src/Bookshelf/AuthorController.php#L16)

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [November 9, 2016, 2:49pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/6 "2016-11-09T14:49:24Z")

</div>

You need to inject the Twig object into your controller.

---

<div class="post-metadata">

**Author:** ![callerd](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@callerd](https://discourse.slimframework.com/u/callerd)\
**Post date:** [November 9, 2016, 2:51pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/7 "2016-11-09T14:51:18Z")

</div>

I tried to do copy and rename this controller from github, but I get now this error:  
`Argument 1 passed to App\Controllers\SitecheckController::__construct() must be an instance of Slim\Views\Twig`

My construct class is:  
private $view;  
private $router;  
private $flash;  
public function \_\_construct(Twig $view, Router $router, FlashMessages $flash)  
{  
$this-\>view = $view;  
$this-\>router = $router;  
$this-\>flash = $flash;  
}

is there still something to add in \_\_construct?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 9, 2016, 3:02pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/8 "2016-11-09T15:02:09Z")

</div>

Is your controller configured in your container to pass along those things to the constructor? [https://github.com/akrabat/slim-bookshelf/blob/master/app/src/dependencies.php#L40](https://github.com/akrabat/slim-bookshelf/blob/master/app/src/dependencies.php#L40)

And of course anything you define there needs to have its dependencies defined as well, such as

> <https://github.com/akrabat/slim-bookshelf/blob/master/app/src/dependencies.php#L13>

---

<div class="post-metadata">

**Author:** ![dadema](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@dadema](https://discourse.slimframework.com/u/dadema)\
**Post date:** [January 10, 2017, 10:42am UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/9 "2017-01-10T10:42:46Z")

</div>

Hello! I don’t understand how to use twig render outside the response psr7 …  
If in my controller I use a private function to send an email, I can’t use

```
$this->view->render('email/template.twig');

```

because slim return

> Argument 1 passed to Slim\Views\Twig::render() must implement interface Psr\Http\Message\ResponseInterface, string given …

I used and followed the bookshelf example, infact everything else is ok.  
What is the best practice to send htmlbody email rendered by twig in a separate controller function ?  
For a single function as sendEmail() I must pass the $response object from dispatch funcion? It’s correct to think in this way ?  
Thank you very very much!

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [January 10, 2017, 1:03pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/10 "2017-01-10T13:03:03Z")

</div>

@dadema, You are probably looking to _fetch_ the template, not _render_ it.

```php
$emailBody = $this->view->fetch('email/template.twig'); // fetch, not render
// send $emaiBody

```

---

<div class="post-metadata">

**Author:** ![dadema](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@dadema](https://discourse.slimframework.com/u/dadema)\
**Post date:** [January 10, 2017, 1:52pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/11 "2017-01-10T13:52:21Z")

</div>

Thank you @tflight ! Thanks for your help !  
It works now without load another Twig in my DI :)) I was making a mess 😊  
Now I also discovered `fetchFromString`

> <https://github.com/slimphp/Twig-View/blob/4cc4629f9a3e95f87694207423f7f235e69fc24f/src/Twig.php#L99>

  
👍

---

<div class="post-metadata">

**Author:** ![kevashcraft](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@kevashcraft](https://discourse.slimframework.com/u/kevashcraft)\
**Post date:** [January 27, 2017, 6:41pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/12 "2017-01-27T18:41:10Z")

</div>

Thank you!! 20 minutes of searching and fetch is what I was looking for 😃

---

<div class="post-metadata">

**Author:** ![teseo](https://avatars.discourse-cdn.com/v4/letter/t/76d3ee/32.png) [@teseo](https://discourse.slimframework.com/u/teseo)\
**Post date:** [March 31, 2017, 3:05pm UTC](https://discourse.slimframework.com/t/return-twig-template-by-controller/890/13 "2017-03-31T15:05:32Z")

</div>

this is exactly what I was looking for. Cheers!
