// get depth
 $depth = $item->data('depth') + $startDepth;
 // create item html
 $menuItem = Dom::fragment(View::factory('menu/item/base', array('item' => $item, 'alias' => $alias, 'depth' => $depth))->render());
 // calulate depth difference
 $depthDiff = $depth - $currentDepth;
 // update current depth
 $currentDepth = $depth;
 // lookup or create the correct parent to add the item to
 if ($depthDiff > 0) {
     if (isset($nodeBranch)) {
         // use nodebranch as template
         $newParent = clone $nodeBranch;
     } else {
         // create ul node
         $newParent = Dom::node('ul');
     }
     // append class
     $newParent->attribute('class', 'menuLevel' . $depth, TRUE);
     // add it to the last created item
     $parent->last_child()->append($newParent);
     // set the newly created element as the new parent element
     $parent = $newParent;
     // add this parent to the stack
     $stack[] = $parent;
 } elseif ($depthDiff == 0) {
     // do nothing
 } elseif ($depthDiff < 0) {
     // traverse back thought the tree to set a new parent
     for ($j = 0; $j > $depthDiff; $j--) {
         array_pop($stack);
<?php

echo View::factory('menu/base', array('nodeRoot' => Dom::node('ul')->attribute('class', 'main'), 'nodeBranch' => Dom::node('ul'), 'data' => $data, 'alias' => $alias, 'startDepth' => $startDepth))->render();