Zend Framework Custom Dojo (Dijit) Helpers

Submitted by morf on Sat, 12/26/2009 - 12:00am

Implementing custom Dojo or Dijit into the Zend Framework View Helper is pretty simple. Lately i need this feature for Menu and MenuItem Dijits. I will provide you an example how to do it yourself for another Dijits. This example should work with other Dijits as well (i tried Dialog, DropDownButton, Menu, MenuBar, MenuItem, TitlePane, ToolBar, Tooltip Dialog, and Tree so far).

Ok let's show the code. You have to create custom Dijit Zend_View_Helper by extending Zend_Dojo_View_Helper_DijitContainer class. Don't forget to alter: DocBlock, $_dijit, $_module, and direct function (eg. menu() or menuItem() ) and function DocBlock.

Dijit Menu Implementation

/**
 * Dijit Menu Implementation
 * @author morf
 * @version
 */

/**
 * Menu helper
 *
 * @uses viewHelper Zend_View_Helper
 */
class Zend_View_Helper_Menu extends Zend_Dojo_View_Helper_DijitContainer
{
 /**
 * Dijit being used
 * @var string
 */
 protected $_dijit  = 'dijit.Menu';

 /**
 * Dojo module to use
 * @var string
 */
 protected $_module = 'dijit.Menu';

 /**
 * dijit.Menu
 *
 * @param  string $id
 * @param  string $content
 * @param  array $params  Parameters to use for dijit creation
 * @param  array $attribs HTML attributes
 * @return string
 */
 public function menu($id = null, $content = '', array $params = array(), array $attribs = array()) {
 if (0 === func_num_args()) {
 return $this;
 }

 return $this->_createLayoutContainer($id, $content, $params, $attribs);
 }
}

Dijit MenuItem implementation

/**
 * Dijit MenuItem Implementation
 * @author morf
 * @version
 */

/**
 * MenuItem helper
 *
 * @uses viewHelper Zend_View_Helper
 */
class Zend_View_Helper_MenuItem extends Zend_Dojo_View_Helper_DijitContainer
{
 /**
 * Dijit being used
 * @var string
 */
 protected $_dijit  = 'dijit.MenuItem';

 /**
 * Dojo module to use
 * @var string
 */
 protected $_module = 'dijit.MenuItem';

 /**
 * dijit.MenuItem
 *
 * @param  string $id
 * @param  string $content
 * @param  array $params  Parameters to use for dijit creation
 * @param  array $attribs HTML attributes
 * @return string
 */
 public function menuItem($id = null, $content = '', array $params = array(), array $attribs = array()) {
 if (0 === func_num_args()) {
 return $this;
 }

 return $this->_createLayoutContainer($id, $content, $params, $attribs);
 }
}

Use case:

/**
 * Usecase:
 */
 $this->menu()->captureStart(
 'hosting-nav-menu',
 array(),
 array(
 'style' => 'width: 100%',
 )
 );

 echo $this->menuItem(
 'hosting-nav-menu-programm',
 'Programms',
 array(),
 array()
 );

 echo $this->menuItem(
 'hosting-nav-menu-service',
 'Services',
 array(),
 array()
 );

 echo $this->menuItem(
 'hosting-nav-menu-ad',
 'Ads',
 array(),
 array()
 );

 echo $this->menu()->captureEnd('hosting-nav-menu');

Now you can download working example application here.