Skip to content

View

Overview

A Skinny application delegates rendering of templates to its view object. A Skinny application view is a subclass of \Skinny\View that implements this interface:

<?php
public render(string $template);

The view object’s render method must return the rendered content of the template specified by its $template argument.

Rendering

You can use the Skinny application’s render() method to ask the current view object to render a template with a given set of variables. The Skinny application’s render() method will echo() the output returned from the view object to be captured by an output buffer and appended automatically to the response object’s body. This assumes nothing about how the template is rendered; that is delegated to the view object.

<?php
$app = \Skinny\Skinny::newInstance();
$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 an array as the second argument of the Skinny 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
);

Custom

A Skinny application delegates rendering of templates to its view object. A custom view is a subclass of \Skinny\View that implements this interface:

<?php
public render(string $template);

The view object’s render method must return the rendered content of the template specified by its $template argument. When the custom view’s render method is invoked, it is passed the desired template pathname (relative to the Skinny application’s “templates.path” setting) as its argument. Here’s an example custom view:

<?php
class CustomView extends \Skinny\View
{
    public function render($template)
    {
        return 'The final rendered template';
    }
}

The custom view can do whatever it wants internally so long as it returns the template’s rendered output as a string. A custom view makes it easy to integrate popular PHP template systems like Twig or Smarty.

Heads Up! A custom view may access data passed to it by the Skinny application’s render() method with $this->data.

Skinny has built-in support for Smarty and Twig, although the templating engines themselves must be installed separately.

Example View

<?php
class CustomView extends \Skinny\View
{
    public function render($template)
    {
        // $template === 'show.php'
        // $this->data['title'] === 'Sahara'
    }
}

Example Integration

If the custom view is not discoverable by a registered autoloader, it must be required before the Skinny application is instantiated.

<?php
require 'CustomView.php';

$app = \Skinny\Skinny::newInstance(array(
    'view' => new CustomView()
));

$app->get('/books/:id', function ($id) use ($app) {
    $app->render('show.php', array('title' => 'Sahara'));
});

$app->run();

Data

Heads Up! Rarely will you set or append data directly on the view object. Usually, you pass data to the view with the Skinny application's render() method. See Rendering Templates.

The view object’s setData() and appendData() methods inject data into the view object; the injected data is available to view templates. View data is stored internally as a key-value array.

Setting Data

The view object’s setData() instance method will overwrite existing view data. You may use this method to set a single variable to a given value:

<?php
$app->view->setData('color', 'red');

The view’s data will now contain a key “color” with value “red”. You may also use the view’s setData() method to batch assign an entire array of data:

<?php
$app->view->setData(array(
    'color' => 'red',
    'size' => 'medium'
));

Remember, the view’s setData() method will replace all previous data.

Appending Data

The view object also has a appendData() method that appends data to the view’s existing data. This method accepts an array as its one and only argument:

<?php
$app->view->appendData(array(
    'foo' => 'bar'
));