Home > PHP > PHP wrapper class for Zend View

PHP wrapper class for Zend View

I recently posted an article with an example of a Zend Session wrapper I’ve been using for some of my projects.

In that same vein I’ve created a simple wrapper for Zend View. I found this useful in instances where I didn’t need to use Zend Framework’s whole MVC implementation. Zend View is extremely helpful in creating a quick templating system for smaller projects or custom frameworks.

Once again I’ll be assuming the following:

First our view wrapper/abstraction class (view.php) :

 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to calisza@gmail.com so we can send you a copy immediately.
 * @package    FlexiDev_View
 * @copyright  Copyright (c) 2007 - 2008,  Flexible Developments - Barry Roodt (https://calisza.wordpress.com)
 * @license    New BSD License
 class View {
 	public $tmplVars;
 	public $view;

 	public function __construct(){
 		$this->tmplVars = array();
 		// Instantiate our Zend View
		$this->view = new Zend_View();
		 * reset our basepath and scriptpaths, this is because we don't need the 'scripts', 'helpers' and 'filters' sub folders 
		 * relative to this script - of course you could change this as your needs require
		// set and assign some global vars to our template
		// this is just an example - you can add more here
		$globals  = array("base_url" => $_SERVER["HTTP_HOST"]);
	 * Method to add to our template variable stack 
	 * @param string variable name
	 * @param mixed value
 	public function addTmplVar($key, $value){
 		/* If our stack doesn't already exist, create a new array */
 		if (!is_array($this->tmplVars)){
 			$this->tmplVars = array($key=>$value); 			
 		} else {
 			// Add to our variable stack
 			$this->tmplVars[$key] = $value;
	 * Method to render the required view/template
	 * @param string template script name
	 * @param bool (optional) clear variable stack (default=true)
 	public function getTemplate($tmpl, $clearVars=true){
 		/* Add our variable stack to the template */
		/* If required, clear our stack so that we can start with a fresh template on the next call */
		if ($clearVars)
			$this->tmplVars = "";
		/* Return the rendered template */
		return $this->view->render($tmpl);


Next for some example usage (index.php) :

$items = array ("Name" => "Barry", "Surname" => "Roodt", "Title" => "Mr");
$view = new View();
$view->addTmplVar("myitems", $items);
echo $view->getTemplate("template.phtml");

And finally our view template (template.phtml) :

<div>Hi <?php echo $this->myitems["name"]; ?></div>
<div>Here is some information for you
<?php foreach($this->myitems as $key=>$val) : ?>
    <p><strong><?php echo $key; ?>: </strong><?php echo $val; ?></p>
<?php endforeach; ?>
<div>You can also use Zend View's default helpers like so : <?php echo $this->escape($this->myitems["name"]); ?></div>

You can also view an example of this class in action here.

As always, comments, critiques and corrections welcome.

  1. October 13, 2008 at 4:58 pm

    Odd looking subversion $Id$ string you got in there. There’s no revision number and the date and time is not in ISO 8601 format. In addition you have “1.0” which doesn’t seem to belong there. And your username is two words instead of one. For comparison:

    $Id: action_class_inc.php 10900 2008-10-13 14:14:41Z charlvn $

  2. calisza
    October 13, 2008 at 11:10 pm

    Thanks for pointing that out, removed. Carried over from a comment header I’ve been using for ages and just keep modifying out of habit.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: