PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTML » HTML_TreeMenu » Manual

This manual page shows the basic steps to get HTML_TreeMenu working.

Installing the files

Before using HTML_TreeMenu, you'll have to put some files in your project's htdocs directory:

You can get on your harddrive in $pear_data_dir/HTML_TreeMenu or online at the PEAR SVN-Web.

Simple example

<?php

require_once 'HTML/TreeMenu.php';

$menu_styles      = new HTML_TreeNode(array('text'=>'Styles'));
$menu_pays        = new HTML_TreeNode(array('text'=>'Countries'));
$menu_restaurants = new HTML_TreeNode(array('text'=>'Restaurants'));
$menu_plats       = new HTML_TreeNode(array('text'=>'Menus'));

for ($i = 1; $i < 10; $i++) {
    $menu_styles->addItem(
        new HTML_TreeNode(array('icon'=>'folder.gif', 'text' => "Style #$i"))
    );
    $menu_pays->addItem(
        new HTML_TreeNode(array('icon'=>'folder.gif', 'text' => "Country #$i"))
    );
    $menu_restaurants->addItem(
        new HTML_TreeNode(array('icon'=>'folder.gif', 'text' => "Restaurant #$i"))
    );
    $menu_plats->addItem(
        new HTML_TreeNode(array('icon'=>'folder.gif', 'text' => "Menu #$i"))
    );
}

$menu  = new HTML_TreeMenu();
$menu->addItem($menu_styles);
$menu->addItem($menu_pays);
$menu->addItem($menu_restaurants);
$menu->addItem($menu_plats);

// Chose a generator. You can generate DHTML or a Listbox
$tree = new HTML_TreeMenu_DHTML($menu);

echo $tree->toHTML();

?>

Tree-construction methods

Actually, you have three different methods to build the tree:

By hand

This is the 'hard' way, like the example above. You have to set each node by hand.

Importing XML

You can import a formatted XML file into the tree structure. It supports string of XML_Tree object :)

XML Format

<treemenu>
  <node text="First node" icon="folder.gif" expandedIcon="folder-expanded.gif" />
  <node text="Second node" icon="folder.gif" expandedIcon="folder-expanded.gif">
    <node text="Sub node" icon="folder.gif" expandedIcon="folder-expanded.gif" />
  </node>
  <node text="Third node" icon="folder.gif" expandedIcon="folder-expanded.gif">
</treemenu>

Example with a string (not tested)

<?php
require_once 'HTML/TreeMenu.php';

// Load the XML file
$xml = file_get_contents('tree.xml');

$menu = new HTML_TreeMenu();
$menu->createFromXML($xml);

// Chose a generator. You can generate DHTML or a Listbox
//$tree = new HTML_TreeMenu_Listbox($menu);
$tree = new HTML_TreeMenu_DHTML($menu);

echo $tree->toHTML();

?>