In our last Zend Framework tutorial we had gone through Error handling in Zend Framework, today in this tutorial we will cover the concept of views in Zend Framework.
By now we all are familiar with the word view in the MVC architecture of web applications.
A view presents the information to us in a very systematic and presentable manner.
- In Zend framework, Zend_View is a class that works with the view part of model-view-controller pattern.
- Zend_View is used in two steps:
- The controller script creates an instance of Zend_View and assigns a variable to that instance.
- The controller then tells the Zend_View to render a particular view, and hence gives control to the view script which generates the view output.
- Controller scripts:
- Let us see how the controller instantiates the Zend_View class and asks to render the view output by passing variables.
- We are aware of the fact that the whole application is controlled by a controller; hence we also know that the required information will be passed to the view through the controller.
- So keeping this in mind let us see the ways in which a controller can pass values to a view. A controller should pass values to the view before it hands control to the view script i.e. the view file.
- A controller can pass values by assigning them to variables, or can pass values in bulk through arrays.
- Assigning variables: normally, we can assign values one at a time to property names of the view instance as shown below:
- Assigning through an array: We can even use assign() method to assign values from an array in bulk as shown below:
- Using objects we can assign values as follows:
- Instead we can even pass variables one by one as shown below by passing a string variable name and then its value:
- Now after providing values the controller asks the Zend_View object to render the view. The render() method is used for this purpose.
- This method only returns the rendered view, but doesn’t display or print it, so this is to be done by us.
- Example of Rendering the view file is shown below:
$info=new Zend_View(); $info->A=”Apple”; $info->B=”Banana”;
$info=new Zend_View(); $fruits=array( ‘A’=> “Apple”, ‘B’=> “Banana” ); $info->assign($fruits);
$info=new SomeClass(); $info->A=”Apple”; $info->B=”Banana”; $info->assign((array) $info);
Here $info is an object.
$info=new Zend_View(); $info->assign(‘A’,”Apple”); $info->assign(‘B’,”Banana”);
$info=new Zend_View(); $info->A=”Apple”; $info->B=”Banana”; Echo $info->render(‘view_file.php’);
$info = new Zend_View(); $info->setScriptPath('/path/to/app/views');
- The Zend_View executes the requested view file inside the scope of Zend_View instance. Hence, in view scripts, the $this directly points to the Zend_View instance itself.
- The variables assigned to the view from controller are referred to as instance properties. For example, if the variable passed to view by controller is A, it will be used in view file as $this->A.
- Let us go through an example where a controller renders a view file to display it.
- The situation is that, we want to display the products category list available on our shopping portal. This list is passed in an array to the view file through controller which is then displayed in the view file.
- The controller file code for this is given below:
prod_Controller extends Action_Controller { // array of product category $data=array( ‘cat1’=>’Electronics’, ‘cat2’=>’Kids Wear’, ‘cat2’=>’Food Products’ ); Zend_Loader::loadClass('Zend_View'); $info = new Zend_View(); $info->cat = $data; // and render a view script called "catlist.php" Echo $info->render('catlist.php'); }
<table> <tr> <th>Categories</th> </tr> <?php foreach ($this->cat as $key => $val): ?> <tr> <td><?php echo $this->escape($val) ?></td> <td><?php echo $this->escape($val) ?></td> </tr> <?php endforeach; ?> </table> <?php endif;?>
Thus we learned views in simple language in this Views in Zend Framework tutorial.