Zend Framework - View Variables, Joomla Style

Click on the picture to enlarge

A while back, we were developing an app using the Zend Framework and found ourselves putting quite a bit of logic into the view script. We didn't feel as if this logic belonged in the controller and always resented the idea of putting too much PHP code into a view script, in case we ever needed a web designer or someone not so PHP savvy to work with the code.

Having worked with Joomla 1.5 in the past, we were quite pleased with the way they approached views. They had two separate view layers. One of them determined which view variables were to be available in the view. This file did not output HTML or anything special. It was merely some additional PHP code to run before the view script was rendered. Afterwards, the actual view script was rendered with items passed from the controller and the previous view file.

We quite liked this and decided we wanted something just like that in the Zend Framework. We wanted it to work with the existing Zend_View structure, so we extended the class and implemented this code.

We put this library together, hopefully others may find it useful.

 

<?php

/*
 Copyright (c) 2010 Oomta Pty Ltd

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

class Oomta_View extends Zend_View
{
	protected function _run()
	{
		/*
			View scripts are usually in the following form...
			
			action.suffix -> login.phtml
			
			Or if a context switch occurred...
			
			action.context.suffix -> login.ajax.phtml
			
			The action name is always first. Rather than try to find the suffix, append our word after the action name
		*/
		
		$strViewScriptFile = func_get_arg(0);
		$strViewScriptDir = func_get_arg(0);
		
		$strViewScriptDir = substr($strViewScriptDir, 0, strrpos($strViewScriptDir, "/") + 1);

		// Pull the view script name
		$strViewScriptFile = substr($strViewScriptFile, strrpos($strViewScriptFile, "/") + 1);
		
		
		$strViewScriptFile = substr($strViewScriptFile, 0, strpos($strViewScriptFile, ".")) . ".vars" . substr($strViewScriptFile, strpos($strViewScriptFile, "."));


		$strInclude = $strViewScriptDir . $strViewScriptFile;
		if (is_file($strInclude))
		{
	        if ($this->_useViewStream && $this->useStreamWrapper()) {
	            include 'zend.view://' . $strInclude;
	        } else {
	            include $strInclude;
	        }
		}		

		// Run the parent now
		parent::_run(func_get_arg(0));
	}
}

 

To set it up...

  1. Copy the code above and save it in a file named View.php (case sensitive)
  2. Create a folder in the library folder named Oomta (case sensitive)
  3. Replace any code where Zend_View is instantiated (e.g. the bootstrap) with Oomta_View, e.g.
    $view = new Oomta_View();
    

 

To use it

  1. Find the view you wish to create a view variable file for, say for example your action was named add
  2. Create a view.vars.phtml file (to compliment the view.phtml file)
  3. Put any code in here, e.g.
    // Assume an array of customer records was passed in to the view from the controller
    $ddlCustomerList = array();
    $ddlCustomerList[] = "Select...";
    foreach ($this->CustomerList as $arrCustomer)
        $ddlCustomerList[$arrCustomer->CustomerID] = $arrCustomer->CustomerName;
    $this->DDLCustomerList = $ddlCustomerList;
    
  4. Use our newly created array as the data variable for a Zend helper like formSelect()

 

Please note that this implementation may not work with all solutions. If you have made customizations to the core MVC structure, I can't guarantee it will work for you. It should work for most of you that are using Zend_View though.

Let us know if it helps you, or if you know a better way to approach it.


Post a comment below

Name *
E-Mail *
Comment *