Learn About Views in Zend Framework

0
1585
Views in Zend Framework

Views in Zend FrameworkIn 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:
    1. The controller script creates an instance of Zend_View and assigns a variable to that instance.
    2. 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.
      1. Assigning variables: normally, we can assign values one at a time to property names of the view instance as shown below:
      2. Assigning through an array: We can even use assign() method to assign values from an array in bulk as shown below:
      3. Using objects we can assign values as follows:
      4. Here $info is an object.

      5. 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:
    • We should remember that the Zend_View looks for the view_file.php in the location or folder where the controller lies. But if the view file is somewhere else we need to specify its location in order to tell Zend_View to search for it in the specified location. This work is done using setScriptPath() method as shown below:
    • So now after specifying the path, you call the render method, the view file will be searched in the path specified and once it finds the file the control will be passed to the view script.
  • View Scripts:
    • 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:
    • Now let us see the view file catlist.php
    • We use escape() method here to apply output escaping to variables.

Thus we learned views in simple language in this Views in Zend Framework tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here