Documentation
Views
Table of Contents ▼
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
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- External libraries : How to use external function libraries
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependency injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
- Data sources : Unified data access management
- Model : How to use DAOs to access databases
- Execution flow : How to manage the execution flow, between plugins and the controller
- Plugins : How to use plugins, and create your own plugins to modularize your code
- Attributes : How to filter access to controllers and actions
- Tests : To write automated integration tests.
- Command-line interface : To create executable scripts on the command line, automatically initialized by Temma
- Helpers : Items offered by Temma to help you in various circumstances