Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim Framework passing data to Twig view

Tags:

php

slim

I'm trying to build a page in Slim that will bring up a subscribers details. I've worked out how to create the route and the relevant method in the controller which all works correctly. I'm using Twig for the views and can't work out how I would access the subscriber from the view.

Route

$app->get('/subscriber/{id}', 'SubscriberController:getSubscriber');

Subscriber controller

public function getSubscriber($request, $response, $args)
{
    $subscriber = Subscriber::where('id', $args['id'])->first();
}

I've been using the below in my controller to render my Twig templates

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

How would I pass in or access my subscriber variable in the Twig template? I can't work out how to pass it through?

like image 496
Joe Ainsworth Avatar asked Dec 19 '22 14:12

Joe Ainsworth


1 Answers

On the render method the 3. parameter is data where you can give the twig template variables.

$data = ['subscriber' => $subscriber];
return $this->container->view->render($response, 'subscriber.twig', $data);

now you can access this variable inside twig.

like image 67
jmattheis Avatar answered Jan 08 '23 03:01

jmattheis