Error $this undefined

Errore running example from documentation on fresh installation

// Render Twig template in route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render($response, 'profile.html', [
        'name' => $args['name']
    ]);
})->setName('profile');
</code>
error :
Fatal error: Uncaught Error: Using $this when not in object context

while this works:

<code>
$app->get('/', function (Request $request, Response $response, $args) {
    $response->getBody()->write("Hello world!");
    return $response;
});

</code<

I could not find your example in the documentation.

Fatal error: Uncaught Error: Using $this when not in object context

Your example works only within a class / object, but not within a callback function.

Have you tried this? Templates - Slim Framework

1 Like

https://www.slimframework.com/docs/v3/features/templates.html#the-slimtwig-view-component

probabilly I made error using example for v3 but Iā€™m using v4
how can I render template in v4 ?

how can I render template in v4 ?

You can find the link in my previous answer.

1 Like