Jump to content

render nav with root parent included


adrianmak
 Share

Recommended Posts

Here is the code , which is copied and simplified from a function of pw's default profile

function renderList($items) {

    if($items instanceof Page) $items = array($items);

	$out = '';

	foreach($items as $item) {
        $out .= "<li><a href='$item->url'>$item->title</a></li>";
        
        if ($item->hasChildren()) {
            $out .= renderList($item->children);
        }
	}
        $out = "<ul>$out</ul>\n";
	return $out;
}

However the output markup is not I desired.

<ul>
   <li><a href="/about/">About  Us</a></li>
   <ul>
     <li><a href="/about/history/">History</a></li>
     <li><a href="/about/location/">Location</a></li>
   </ul>
</ul>

I want the markup should be, the parent root should be on the same level as of it's child. Like

<ul>
   <li><a href="/about/">About  Us</a></li>
   <li><a href="/about/history/">History</a></li>
   <li><a href="/about/location/">Location</a></li>
</ul>
Link to comment
Share on other sites

Quick shot. not tested

function renderList($items, $wrap = true) {

    if($items instanceof Page) $items = array($items);

    $out = '';

    foreach($items as $item) {
        $out .= "<li><a href='$item->url'>$item->title</a></li>";
        
        if ($item->hasChildren()) {
            $out .= renderList($item->children, false);
        }
    }
    if ($wrap) $out = "<ul>$out</ul>\n";
    return $out;
}
  • Like 1
Link to comment
Share on other sites

Quick shot. not tested

function renderList($items, $wrap = true) {

    if($items instanceof Page) $items = array($items);

    $out = '';

    foreach($items as $item) {
        $out .= "<li><a href='$item->url'>$item->title</a></li>";
        
        if ($item->hasChildren()) {
            $out .= renderList($item->children, false);
        }
    }
    if ($wrap) $out = "<ul>$out</ul>\n";
    return $out;
}

yeah~~~~~ it works.

I was thinking, there should have some kind of indicator to not wrap the outter UL tag when transvering  a list

I scratched my head for few hrs but not clue.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...