Return twig template by controller?


#1

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?


#2

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

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

#3

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…


#4

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


#5

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


#6

You need to inject the Twig object into your controller.


#7

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?


#8

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

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


#9

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!


#10

@dadema, You are probably looking to fetch the template, not render it.

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

#11

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


:+1:


#12

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


#13

this is exactly what I was looking for. Cheers!