Data

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

The view object’s setData() and appendData() methods inject data into the view object; the injected data isavailable 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 asingle variable to a given value:

  1. <?php
  2. $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() methodto batch assign an entire array of data:

  1. <?php
  2. $app->view->setData(array(
  3. 'color' => 'red',
  4. 'size' => 'medium'
  5. ));

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 acceptsan array as its one and only argument:

  1. <?php
  2. $app->view->appendData(array(
  3. 'foo' => 'bar'
  4. ));