Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at:
View Scripts - Zend_View
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.
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.
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.
The callback function or method should take the value to be escaped as its first parameter, and all other parameters should be optional.
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.
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:
These would be the related template files:
Some may find it easier to simply provide a Zend_View-compatible template engine. Zend_View_Interface defines the minimum interface needed for compatability:
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:
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: