Rendering
You can use the Slim application’s render()
method to ask the current view object to render a template with agiven set of variables. The Slim application’s render()
method will echo()
the output returned from the viewobject to be captured by an output buffer and appended automatically to the response object’s body. This assumesnothing about how the template is rendered; that is delegated to the view object.
<?php
$app = new \Slim\Slim();
$app->get('/books/:id', function ($id) use ($app) {
$app->render('myTemplate.php', array('id' => $id));
});
If you need to pass data from the route callback into the view object, you must explicitly do so by passing anarray as the second argument of the Slim application’s render()
method like this:
<?php
$app->render(
'myTemplate.php',
array( 'name' => 'Josh' )
);
You can also set the HTTP response status when you render a template:
<?php
$app->render(
'myTemplate.php',
array( 'name' => 'Josh' ),
404
);