IntroductionZend_View is a class for working with the "view" portion of the model-view-controller pattern. That is, it exists to help keep the view script separate from the model and controller scripts. It provides a system of helpers, output filters, and variable escaping. Zend_View is template system agnostic; you may use PHP as your template language, or create instances of other template systems and manipulate them within your view script. Essentially, using Zend_View happens in two major steps: 1. Your controller script creates an instance of Zend_View and assigns variables to that instance. 2. The controller tells the Zend_View to render a particular view, thereby handing control over the view script, which generates the view output. Controller ScriptAs a simple example, let us say your controller has a list of book data that it wants to have rendered by a view. The controller script might look something like this:
View ScriptNow we need the associated view script, "booklist.php". This is a PHP script like any other, with one exception: it executes inside the scope of the Zend_View instance, which means that references to $this point to the Zend_View instance properties and methods. (Variables assigned to the instance by the controller are public properties of the Zend_View instance). Thus, a very basic view script could look like this:
Note how we use the "escape()" method to apply output escaping to variables. OptionsZend_View has several options that may be set to configure the behaviour of your view scripts.
Short Tags with View ScriptsIn our examples, we make use of PHP long tags: <?php. We also favor the use of » alternate syntax for control structures. These are convenient shorthands to use when writing view scripts, as they make the constructs more terse, keep statements on single lines, and eliminate the need to hunt for brackets within HTML. In previous versions, we often recommended using short tags (<? and <?=), as they make the view scripts slightly less verbose. However, the default for the php.ini short_open_tag setting is typically off in production or on shared hosts -- making their use not terribly portable. If you use template XML in view scripts, short open tags will cause the templates to fail validation. Finally, if you use short tags when short_open_tag is off, the view scripts will either cause errors or simply echo PHP code back to the viewer. If, despite these warnings, you wish to use short tags but they are disabled, you have two options:
Warning
View Stream Wrapper Degrades PerformanceUsage of the stream wrapper will degrade performance of your application, though actual benchmarks are unavailable to quantify the amount of degradation. We recommend that you either enable short tags, convert your scripts to use full tags, or have a good partial and/or full page content caching strategy in place. Utility AccessorsTypically, you'll only ever need to call on assign(), render(), or one of the methods for setting/adding filter, helper, and script paths. However, if you wish to extend Zend_View yourself, or need access to some of its internals, a number of accessors exist:
|