Introduction – Menu structure and supported output modes

Menu hash structure

The menu structure is defined by a multidimensional hash. This makes it quite easy to generate and traverse:

Menu multidimensional hash

=> array(
'title' => 'Menu item 1'
'url' => '/item1.php',
'sub' => array(
11 => array('title' => 'Menu item 1.1''url' => '/item1.1.php'),
12 => array(
'title' => 'Menu item 1.2'
'url' => '/item1.2.php',
'sub' => array(
121 => array('title' => 'Menu item 1.2.1''url' => '/item1.2.1.php'),
122 => array('title' => 'Menu item 1.2.2''url' => '/item1.2.2.php')
=> array(
'title' => 'Menu item 2'
'url' => '/item2.php',
'sub' => array(
21 => array('title' => 'Menu item 2.1''url' => '/item2.1.php'),
22 => array('title' => 'Menu item 2.2''url' => '/item2.2.php')

Each entry should have at least 'url' and 'title' keys and may also have 'sub' key containing the children of this entry. Note that keys in the entry arrays serve as node identifiers and should be unique.

The menu entries can also contain custom keys. If such keys are present, then they will be used by renderers in creating the output (this usually means that the content of such a key will be assigned to the template placeholder with the same name).

Supported output modes

HTML_Menu supports five output modes: 'tree' (default), 'rows', 'urhere', 'prevnext' and 'sitemap'. Lets use the array defined above as menu structure assuming that element 'Menu item 1.2' is currently active and try each menu type.


This type of the menu mostly follows the internal structure of the menu hash. Different levels of the menu are marked by indentation, only the elements leading to the active item or immediately following it are shown.

Output for menu type 'tree'

Menu item 1
    Menu item 1.1   
    Menu item 1.2
        Menu item 1.2.1
        Menu item 1.2.2
Menu item 2

This is quite similar to 'tree' type, only different levels are not marked by indentation, but are shown on different rows of the menu.

Output for menu type 'rows'

Menu item 1  Menu item 2
Menu item 1.1  Menu item 1.2
Menu item 1.2.1  Menu item 1.2.2

This is so-called 'breadcrumb' navigation, allowing to easily understand your position within site hierarchy.

Output for menu type 'urhere'

Menu item 1 >> Menu item 1.2

This is the menu often used in documentation (including the PEAR manual), the links lead to previous, next and parent entries of the current entry.

Output for menu type 'prevnext'

<< Menu item 1.1  ^ Menu item 1 ^  Menu item 1.2.1 >>

This is 'tree' type menu, but with all entries shown.

Usage example

Very basic usage example

// Load the class
require_once 'HTML/Menu.php';

// Instantiate the menu object, we presume that $data contains menu structure
$menu =& new HTML_Menu($data'tree');

// Output the menu
HTML_Menu (Previous) Initializes the menu, sets the type and menu structure. (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.