- 2012/04/04

Have you ever met the condition where you want to inject html code to Zend_Navigation menu ? Yes, take a look at picture below :

nav custom

Of course you can accomplish that in several ways, but this is my way to inject html tag without having to create custom view to render navigation menu.

  • Add “extra tags” to navigation application resource config
  • Create view helper to convert “extra tags”

Here is the break down :

Add “extra tags” To Navigation Application Resource Config

<?php
return array(
    'pages' => array(
        'index' => array(
            'label'     => '{{i class=||icon-home||}}{{/i}}siMukti.NET',
            'route'     => 'default',
            'order'     => '-100',
            'visible'   => true,
            'id'        => 'index'
        ),
        'blog' => array(
            'label'     => '{{i class=||icon-th-list||}}{{/i}}Blog',
            'route'     => 'blog-index',
            'visible'   => true,
            'id'        => 'blog'
        ),
        'about' => array(
            'label'     => '{{i class=||icon-info-sign||}}{{/i}}About Me',
            'route'     => 'about-index',
            'visible'   => true,
            'id'        => 'about',
        )
    )
);

I choose to use characters that will not escaped by default in “extra tags”.

Create View Helper To Convert “extra tags”

Lets say that I create Iconify view helper class to convert these “extra tags”

<?php
/**
 * Description of Iconify
 *
 * @author Sarjono Mukti Aji <[email protected]>
 */
use \Zend_View_Helper_Abstract as HelperAbstract;
 
class View_Helper_Iconify extends HelperAbstract
{
    public function iconify($string)
    {
        if(is_string($string)) {
            $string = str_replace(array('{{', '}}', '||'), array('<','>', '"'), $string);
        }
        return $string;
    }
}

What’s Next ?

Just simply call iconify view helper to render zend navigation menu in layout script.

<?php 
    echo $this->iconify($this->navigation()
                             ->menu()
                             ->setUlClass('block_nav top_nav_left')
                             ->setMaxDepth(0)
                             ->renderMenu()
                        ); 
?>

What it looks like ? you can see at top menu at this site as a live demo for this post ;)

Update 2016: This blog is no longer using Zend Framework 2