FuelPHP Bin
<?php public static function generate_tree(Model_Section $section = null) { if (is_null($section)) { $sections = Model_Section::find('all'); $top_level = $level = 0; } else { $sections = $section->descendants()->get(); $top_level = $level = $section->depth(); $top_level++; } if ($sections) { $el = 'ul'; echo "<$el>"; $skip = array(); foreach ($sections as $s) { if (in_array($s->id, $skip)) continue; if ($s->depth() < $level) echo str_repeat("</li></$el>", $level - $s->depth() - $top_level)."</li>"; echo "\n<li>{$s->title}"; if ($children = $s->descendants()->get()) { foreach ($children as $c) { if ($c->depth() > $level) echo "<$el>"; if ($c->depth() < $level) echo str_repeat("</li></$el>", $level - $s->depth() - $top_level)."</li>"; echo "\n<li>{$c->title}"; if ($c->depth() <= ($level-$top_level)) echo "</li>"; $skip[] = $c->id; $level = $c->depth(); } echo "</$el>"; } else { $level = $s->depth(); } echo "</li>"; } } echo "</$el>"; }