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.
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.
// bad view-script practice:
// 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.
// create a Zend_View instance
// tell it to use htmlentities as the escaping callback
$view->setEscape('htmlentities');
// or tell it to use a static class method as the callback
'SomeClass', 'methodName'));
// or even an instance method
'methodName'));
// 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:
span style="color: #ff0000;">'template.inc'"booklist" => "booklist.tpl",
"eachbook" => "eachbook.tpl"'author', $this->escape($val['author''title', $this->escape($val['title']);
$tpl->parse("books", "eachbook""output", "booklist""nobooks", "nobooks.tpl")
$tpl->pparse("output", "nobooks");
}
These would be the related template files:
<!-- booklist.tpl -->
<table>
<tr>
<th>Author</th>
<th>Title</th>
</tr>
{books}
</table>
<!-- eachbook.tpl -->
<tr>
<td>{author}</td>
<td>{title}</td>
</tr>
<!-- nobooks.tpl -->
<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:
/**
* Return the actual template engine object
*//**
* Set the path to view scripts/templates
*//**
* Set a base path to all view resources
*/'Zend_View');
/**
* Add an additional base path to view resources
*/'Zend_View');
/**
* Retrieve the current script paths
*//**
* Overloading methods for assigning template variables as object
* properties
*//**
* Manual assignment of template variables, or ability to assign
* multiple variables en masse.
*//**
* Unset all assigned template variables
*//**
* Render the template named $name
*/
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:
span style="color: #808080; font-style: italic;">/**
* Smarty object
* @var Smarty
*//**
* Constructor
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*//**
* Return the template engine object
*
* @return Smarty
*//**
* Set the path to the templates
*
* @param string $path The directory to set as the path.
* @return void
*/'Invalid path provided');
}
/**
* Retrieve the current template directory
*
* @return string
*//**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/'Zend_View'/**
* Alias for setScriptPath
*
* @param string $path
* @param string $prefix Unused
* @return void
*/'Zend_View'/**
* Assign a variable to the template
*
* @param string $key The variable name.
* @param mixed $val The variable value.
* @return void
*//**
* Allows testing with empty() and isset() to work
*
* @param string $key
* @return boolean
*//**
* Allows unset() on object properties to work
*
* @param string $key
* @return void
*//**
* Assign variables to the template
*
* Allows setting a specific key to the specified value, OR passing
* an array of key => value pairs to set en masse.
*
* @see __set()
* @param string|array $spec The assignment strategy to use (key or
* array of key => value pairs)
* @param mixed $value (Optional) If assigning a named variable,
* use this as the value.
* @return void
*//**
* Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via
* {@link assign()} or property overloading
* ({@link __get()}/{@link __set()}).
*
* @return void
*//**
* Processes a template and returns the output.
*
* @param string $name The template to process.
* @return string The output.
*/
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:
//Example 1. In initView() of initializer.
'/path/to/templates''ViewRenderer'':controller/:action.:suffix')
->setViewScriptPathNoControllerSpec(':action.:suffix')
->setViewSuffix('tpl');
//Example 2. Usage in action controller remains the same...
'Zend PHP 5 Certification Study Guide';
$this->view->author = 'Davey Shafik and Ben Ramsey'
}
}
//Example 3. Initializing view in action controller
'/path/to/templates''viewRenderer'':controller/:action.:suffix')
->setViewScriptPathNoControllerSpec(':action.:suffix')
->setViewSuffix('tpl');
}
|
|