Jump to content

Recommended Posts

Posted

Thanks for the replies, and thanks renobird, that's exactly what I want, except I want to be able to pass a selector instead of a text field

Posted

The module I have is too complicated to take apart right now. I have a lot of stuff going on that is specific to my local needs.

The easiest way to handle this is to put this in a template.

 
<?php $items = $pages->find('your_selector');?>

<dl class="accordion">
<?php foreach ($items as $item):?> 
    <dt><a href="<?php echo $item->url;?>"><?php echo $item->title;?></a></dt>
    <dd><?php echo $item->body;?></dd>
<?php endforeach;?>
</dl>

Then include your own CSS/JS, or use the CSS/JS from my demo


If you need a nested accordion, then it's a little tricker, and would be easier with a recursive function. 

Posted

Here's a quick recursive version. Written in the browser and untested.

Should give you a solid place to start.

<?php 
function renderAccordion(pageArray $pages){
    $out = "<dl class='accordion'>";
    foreach ($pages as $page){
        $out .= "<dt><a href='$page->url'>$page->title</a></dt>";
        $out .= "<dd>";
        if ($page->numChildren){
            $out .= renderAccordion($page->children()); // go recursive
        } else
            $out .= $page->body;
        }
       $out .= "</dd>";
    }
    $out .= "</dl>";

    return $out;
}

$items = $pages->find("your_selector");
echo renderAccordion($items); 

This will just keep going. You could set it to accept a $levels argument that would stop it after a specified depth.

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
  • Recently Browsing   0 members

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