4
votes

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?

1

1 Answers

11
votes

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.