Documentation

Vues

Presentation

The view is the software layer that takes care of formatting the data that is returned by the server after processing.

By default, Temma uses the Smarty template engine, which makes it very easy to generate HTML pages from the data exported by the controllers.

Temma also natively provides a view used to generate JSON feeds, which can be very practical in the context of AJAX communications, as well as views offering CSV, RSS, INI and iCal exports.

Using a templated view

Some views use templates to process data and generate outgoing flows. As seen in the introduction, Temma will look for a file whose name corresponds to that of the requested action (with the ".tpl" extension), placed in a directory whose name is that of the controller being executed; all placed in the templates/ directory of the project.

To work around this automatic behavior, an action can specify the template to use:

class User extends \Temma\Web\Controller {
    public function list($type=null) {
        // processings...

        // redefining the template in a particular case
        if ($type == 'all')
            $this->_template('user/listAll.tpl');
        // in the other cases, the template will be "user/list.tpl"
    }
}

View definition

It is possible to specify the view to use, individually for each action.

Here is an example of an action whose data is exported in JSON format:

class User extends \Temma\Web\Controller {
    public function get($id) {
        // processings...

        // set the data that will be sent
        // in the JSON stream
        $this->set('json', $data);

        // definition of the used view
        $this->_view('\Temma\Views\Json');
    }
}

Configuration of the default view

You have the possibility to change the default view, by modifying the temma.json configuration file.

For example, if you create a webservice, you will never send HTML, but always JSON. You will then want to use the \Temma\Views\Json view instead of the usual Smarty view, you have to add the defaultView directive in the temma.json file:

{
    "application": {
        // usual configuration (dsn, defaultController, ...)

        // configuration of the default view
        "defaultView": "\\Temma\\Views\\Json"
    }
}
Previous: Controllers
Next: Smarty view

Table of Contents