0

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

JimmyJazz
  • 1
  • 1
  • 1
    kind reminder, abandoning your questions will result in less motivation for the community to give answers on your future issues. you should try to a) accept an answer, b) post feedback why the given answers don't fix your problem or c) post a solution you found yourself, so the issue can be marked as solved (and someone having the same problem can find the answer here). – schellmax Aug 08 '11 at 09:32

2 Answers2

1

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.

schellmax
  • 5,678
  • 4
  • 37
  • 48
0

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...

xeraa
  • 10,456
  • 3
  • 33
  • 66
  • Thanks for that, it does make sense, but yeah...I was hoping to this into the controller instead of the template. I just cant find any documentation to help with this one. – JimmyJazz Aug 04 '11 at 02:14
  • In that case use @schellmax's solution - `$this->getMenu($level)` should be good. IMHO this is very front-end specific, so I'd stick with the template approach. For example if you'd want to provide a machine-readable representation of your information, you'd probably use XML or JSON. There you probably wouldn't need the class (I assume you want to use it for styling) - so I'd keep it in the specific template and not the generic Controller. At least that's my approach to these kind of problems... – xeraa Aug 05 '11 at 14:50