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.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/books/:id', function ($id) use ($app) {
  4. $app->render('myTemplate.php', array('id' => $id));
  5. });

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:

  1. <?php
  2. $app->render(
  3. 'myTemplate.php',
  4. array( 'name' => 'Josh' )
  5. );

You can also set the HTTP response status when you render a template:

  1. <?php
  2. $app->render(
  3. 'myTemplate.php',
  4. array( 'name' => 'Josh' ),
  5. 404
  6. );