JimmyJazz
JimmyJazz

Reputation: 1

silverstripe function to return menu level

I'm trying to write a function that returns what menu levels are visible on the page...at the moment I'm using <% if %> statements in the template, ie:

<div class="<% if Menu(1) %>navA<% end_if %> <% if Menu(2) %>navB<% end_if %> <% if Menu(3) %>navC<% end_if %>">...</div>

Which, if there are 3 menu levels on a page, returns <div class="navA navB navC">

What I want is a function that returns just the lowest level menu on the current page, ie <div class="navC">

Thanks

Upvotes: 0

Views: 1624

Answers (2)

schellmax
schellmax

Reputation: 6094

that's perfectly possible. just add the following to your Page_Controller class:

function LowestLevel() {
    $i = 1;
    while($this->getMenu($i)->count() > 0) $i++;
    return 'level'.($i-1);
}

now you can call it in your template like so:

<div>lowest level: $LowestLevel</div>

$LowestLevel will print 'level1', 'level2' etc.

in case your class names have to be like 'navA', 'navB'... you need to do some matching like 'level1'->'navA', which shouldn't be too hard - come back to me if you need any help on this.

Upvotes: 1

xeraa
xeraa

Reputation: 10859

What about the following (untested):

<div class="<% if Menu(3) %>navC<% else_if Menu(2) %>navB<% else %>navA<% end_if %>">...</div>

You might want to consider using some custom code in the Controller for logic-heavy stuff, but this should get you going...

Upvotes: 0

Related Questions