Controller Scripts

View Scripts

Once your controller has assigned variables and called render(), Zend_View then includes the requested view script and executes it "inside" the scope of the Zend_View instance. Therefore, in your view scripts, references to $this actually point to the Zend_View instance itself.

Variables assigned to the view from the controller are referred to as instance properties. For example, if the controller were to assign a variable 'something', you would refer to it as $this->something in the view script. (This allows you to keep track of which values were assigned to the script, and which are internal to the script itself.)

By way of reminder, here is the example view script from the Zend_View introduction.

  1. span style="color: #ff0000;">'author''title'

Escaping Output

One of the most important tasks to perform in a view script is to make sure that output is escaped properly; among other things, this helps to avoid cross-site scripting attacks. Unless you are using a function, method, or helper that does escaping on its own, you should always escape variables when you output them.

Zend_View comes with a method called escape() that does such escaping for you.

  1. // bad view-script practice:
  2. // good view-script practice:

By default, the escape() method uses the PHP htmlspecialchars() function for escaping. However, depending on your environment, you may wish for escaping to occur in a different way. Use the setEscape() method at the controller level to tell Zend_View what escaping callback to use.

  1. // create a Zend_View instance
  2. // tell it to use htmlentities as the escaping callback
  3. $view->setEscape('htmlentities');
  4.  
  5. // or tell it to use a static class method as the callback
  6. 'SomeClass', 'methodName'));
  7.  
  8. // or even an instance method
  9. 'methodName'));
  10.  
  11. // and then render your view

The callback function or method should take the value to be escaped as its first parameter, and all other parameters should be optional.

Using Alternate Template Systems

Although PHP is itself a powerful template system, many developers feel it is too powerful or complex for their template designers and will want to use an alternate template engine. Zend_View provides two mechanisms for doing so, the first through view scripts, the second by implementing Zend_View_Interface.

Template Systems Using View Scripts

A view script may be used to instantiate and manipulate a separate template object, such as a PHPLIB-style template. The view script for that kind of activity might look something like this:

  1. span style="color: #ff0000;">'template.inc'"booklist" => "booklist.tpl",
  2.         "eachbook" => "eachbook.tpl"'author', $this->escape($val['author''title', $this->escape($val['title']);
  3.         $tpl->parse("books", "eachbook""output", "booklist""nobooks", "nobooks.tpl")
  4.     $tpl->pparse("output", "nobooks");
  5. }

These would be the related template files:

  1. <!-- booklist.tpl -->
  2. <table>
  3.     <tr>
  4.         <th>Author</th>
  5.         <th>Title</th>
  6.     </tr>
  7.     {books}
  8. </table>
  9.  
  10. <!-- eachbook.tpl -->
  11.     <tr>
  12.         <td>{author}</td>
  13.         <td>{title}</td>
  14.     </tr>
  15.  
  16. <!-- nobooks.tpl -->
  17. <p>There are no books to display.</p>

Template Systems Using Zend_View_Interface

Some may find it easier to simply provide a Zend_View-compatible template engine. Zend_View_Interface defines the minimum interface needed for compatability:

  1. /**
  2. * Return the actual template engine object
  3. *//**
  4. * Set the path to view scripts/templates
  5. *//**
  6. * Set a base path to all view resources
  7. */'Zend_View');
  8.  
  9. /**
  10. * Add an additional base path to view resources
  11. */'Zend_View');
  12.  
  13. /**
  14. * Retrieve the current script paths
  15. *//**
  16. * Overloading methods for assigning template variables as object
  17. * properties
  18. *//**
  19. * Manual assignment of template variables, or ability to assign
  20. * multiple variables en masse.
  21. *//**
  22. * Unset all assigned template variables
  23. *//**
  24. * Render the template named $name
  25. */

Using this interface, it becomes relatively easy to wrap a third-party template engine as a Zend_View-compatible class. As an example, the following is one potential wrapper for Smarty:

  1. span style="color: #808080; font-style: italic;">/**
  2.      * Smarty object
  3.      * @var Smarty
  4.      *//**
  5.      * Constructor
  6.      *
  7.      * @param string $tmplPath
  8.      * @param array $extraParams
  9.      * @return void
  10.      *//**
  11.      * Return the template engine object
  12.      *
  13.      * @return Smarty
  14.      *//**
  15.      * Set the path to the templates
  16.      *
  17.      * @param string $path The directory to set as the path.
  18.      * @return void
  19.      */'Invalid path provided');
  20.     }
  21.  
  22.     /**
  23.      * Retrieve the current template directory
  24.      *
  25.      * @return string
  26.      *//**
  27.      * Alias for setScriptPath
  28.      *
  29.      * @param string $path
  30.      * @param string $prefix Unused
  31.      * @return void
  32.      */'Zend_View'/**
  33.      * Alias for setScriptPath
  34.      *
  35.      * @param string $path
  36.      * @param string $prefix Unused
  37.      * @return void
  38.      */'Zend_View'/**
  39.      * Assign a variable to the template
  40.      *
  41.      * @param string $key The variable name.
  42.      * @param mixed $val The variable value.
  43.      * @return void
  44.      *//**
  45.      * Allows testing with empty() and isset() to work
  46.      *
  47.      * @param string $key
  48.      * @return boolean
  49.      *//**
  50.      * Allows unset() on object properties to work
  51.      *
  52.      * @param string $key
  53.      * @return void
  54.      *//**
  55.      * Assign variables to the template
  56.      *
  57.      * Allows setting a specific key to the specified value, OR passing
  58.      * an array of key => value pairs to set en masse.
  59.      *
  60.      * @see __set()
  61.      * @param string|array $spec The assignment strategy to use (key or
  62.      * array of key => value pairs)
  63.      * @param mixed $value (Optional) If assigning a named variable,
  64.      * use this as the value.
  65.      * @return void
  66.      *//**
  67.      * Clear all assigned variables
  68.      *
  69.      * Clears all variables assigned to Zend_View either via
  70.      * {@link assign()} or property overloading
  71.      * ({@link __get()}/{@link __set()}).
  72.      *
  73.      * @return void
  74.      *//**
  75.      * Processes a template and returns the output.
  76.      *
  77.      * @param string $name The template to process.
  78.      * @return string The output.
  79.      */

In this example, you would instantiate the Zend_View_Smarty class instead of Zend_View, and then use it in roughly the same fashion as Zend_View:

  1. //Example 1. In initView() of initializer.
  2. '/path/to/templates''ViewRenderer'':controller/:action.:suffix')
  3.              ->setViewScriptPathNoControllerSpec(':action.:suffix')
  4.              ->setViewSuffix('tpl');
  5.  
  6. //Example 2. Usage in action controller remains the same...
  7. 'Zend PHP 5 Certification Study Guide';
  8.         $this->view->author = 'Davey Shafik and Ben Ramsey'
  9.     }
  10. }
  11.  
  12. //Example 3. Initializing view in action controller
  13. '/path/to/templates''viewRenderer'':controller/:action.:suffix')
  14.                      ->setViewScriptPathNoControllerSpec(':action.:suffix')
  15.                      ->setViewSuffix('tpl');
  16.     }

Controller Scripts