[SOLVED] MenuManager: Getting first item of second-level

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
nils73
Power Poster
Power Poster
Posts: 520
Joined: Wed Sep 08, 2004 3:32 pm

[SOLVED] MenuManager: Getting first item of second-level

Post by nils73 »

Hello everybody,

I have been trying to solve this problem for some hours now, but it takes me nowhere. I have something like the following as a navigation:

1. Content
1.1. Management
1.2. System
1.3. This
2. Open
2.1. Source
2.2. Solutions
3. Award
3.1. Winning
3.1.1. Software

What I need to do is to assign a class-attribute to each element, that follows the parent-element, as long as the parent-element is the root-element. Which is in the above example true for "1.1. Management", "2.1. Source" and "3.1. Winning". Any ideas how to do this with MenuManager? Below is the code I am using right now:

Code: Select all


{if $count > 0}
<ul class="menu-one">
{foreach from=$nodelist item=node name=alist}
{if $node->depth > $node->prevdepth}
{repeat string="<ul>" times=$node->depth-$node->prevdepth}
{elseif $node->depth < $node->prevdepth}
{repeat string="</li></ul>" times=$node->prevdepth-$node->depth}
</li>
{elseif $node->index > 0}</li>
{/if}
{if $node->current == true}
<li{if ($node->current == true) && ($node->parent == false)} class="{if $smarty.foreach.alist.first}first {/if}{if $smarty.foreach.alist.last}last {/if}open"{/if}><h3><dfn>Aktive Seite ist {$node->hierarchy}: </dfn>{$node->menutext}</h3>
{else}
<li{if $node->parent == true} class="{if $smarty.foreach.alist.first}first {/if}{if $smarty.foreach.alist.last}last {/if}open"{else}{if $smarty.foreach.alist.first} class="first"{/if}{if $smarty.foreach.alist.last} class="last"{/if}{/if}><a href="{$node->url}"{if $node->accesskey != ''} accesskey="{$node->accesskey}"{/if}{if $node->tabindex != ''} tabindex="{$node->tabindex}"{/if}{if $node->titleattribute != ''} title="{$node->titleattribute}"{/if}{if $node->target != ''} target="{$node->target}"{/if}><dfn>{if $node->parent == true}Aktiver Bereich ist {/if}{$node->hierarchy}: </dfn>{$node->menutext}</a>
{/if}
{/foreach}
{repeat string="</li></ul>" times=$node->depth-1}</li>
</ul>
{/if}

I hope anybody comes up with a solution that is not a dirty hack. I know that I can just use an extra-field but I want a generic solution. So any help is highly appreciated.

Regards
Nils
Last edited by nils73 on Wed Oct 21, 2009 5:19 pm, edited 1 time in total.
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: MenuManager: Getting first item of second-level

Post by Jos »

I would do something with a smarty variable.
{foreach from=$nodelist item=node name=alist}
{assign var=foo value=''}
{if $node->depth > $node->prevdepth}
{if $node->depth == 2}{assign var=foo value=' class="firstelement"'}{/if}
{repeat string="" times=$node->depth-$node->prevdepth}
{elseif $node->depth prevdepth}
(assuming $node->depth = 2 for the second level)

Then add the {$foo} variable the way you want in the tag or somewhere else

I didn't test this. I hope this helps
Last edited by Jos on Mon Oct 19, 2009 1:16 pm, edited 1 time in total.
nils73
Power Poster
Power Poster
Posts: 520
Joined: Wed Sep 08, 2004 3:32 pm

Re: MenuManager: Getting first item of second-level

Post by nils73 »

Jos wrote: I didn't test this. I hope this helps
This would assign the variable to all elements on second level. But I only need it on the first entry of each second level. So this would not help, sorry to say. However thanks for this trick - might help someone else here. ;-)

Regards
Nils
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: MenuManager: Getting first item of second-level

Post by Jos »

It should be only in the first one, because it's in the if statement:
{if $node->depth > $node->prevdepth}

And this one is only true if we go to a new level
nils73
Power Poster
Power Poster
Posts: 520
Joined: Wed Sep 08, 2004 3:32 pm

Re: MenuManager: Getting first item of second-level

Post by nils73 »

Jos wrote: And this one is only true if we go to a new level
Yes, but if you look at my example your statement is true for

2.1
2.2
2.3

But what I need is to only get hold of 2.1, but not 2.2 and not 2.3 - but with your statement I get hold of the whole level (2.1, 2.2, 2.3) but not 2 and not 2.1.1 et al. So I am still on my quest ... ;-)

Regards
Nils
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: MenuManager: Getting first item of second-level

Post by Jos »

Yeah I understood correctly.
I tested my code now, and for me it works the way you want it. Did you actually tested it?
Last edited by Jos on Wed Oct 21, 2009 3:18 pm, edited 1 time in total.
nils73
Power Poster
Power Poster
Posts: 520
Joined: Wed Sep 08, 2004 3:32 pm

Re: MenuManager: Getting first item of second-level

Post by nils73 »

Mea culpa, Jos and thank you for your patience.

There are only two words to express what happened: IT WORKS!

I don't know where my brain went when I read through your solution, but it wasn't in its place. Sorry for not testing it properly. Now here is the code (for people striding along looking for a solution):

Code: Select all


{if $count > 0}
<ul class="menu-one">
{foreach from=$nodelist item=node name=alist}
{assign var='foo' value=''}
{if $node->depth > $node->prevdepth}
{if $node->depth == 2}{assign var='foo' value='firstelement '}{/if}
{repeat string="<ul>" times=$node->depth-$node->prevdepth}
{elseif $node->depth < $node->prevdepth}
{repeat string="</li></ul>" times=$node->prevdepth-$node->depth}
</li>
{elseif $node->index > 0}</li>
{/if}
{if $node->current == true}
<li{if ($node->current == true) && ($node->parent == false)} class="{if $foo != ''}{$foo}{/if}{if $smarty.foreach.alist.first}first {/if}{if $smarty.foreach.alist.last}last {/if}open"{/if}><h3><dfn>Aktive Seite ist {$node->hierarchy}: </dfn>{$node->menutext}</h3>
{else}
<li{if $node->parent == true} class="{if $foo != ''}{$foo}{/if}{if $smarty.foreach.alist.first}first {/if}{if $smarty.foreach.alist.last}last {/if}open"{else}{if $smarty.foreach.alist.first} class="{if $foo != ''}{$foo}{/if}first"{elseif $smarty.foreach.alist.last} class="{if $foo != ''}{$foo}{/if}last"{else}{if $foo != ''} class="firstelement"{/if}{/if}{/if}><a href="{$node->url}"{if $node->accesskey != ''} accesskey="{$node->accesskey}"{/if}{if $node->tabindex != ''} tabindex="{$node->tabindex}"{/if}{if $node->titleattribute != ''} title="{$node->titleattribute}"{/if}{if $node->target != ''} target="{$node->target}"{/if}><dfn>{if $node->parent == true}Aktiver Bereich ist {/if}{$node->hierarchy}: </dfn>{$node->menutext}</a>
{/if}
{/foreach}
{repeat string="</li></ul>" times=$node->depth-1}</li>
</ul>
{/if}

Many thanks.
Nils
Last edited by nils73 on Wed Oct 21, 2009 5:20 pm, edited 1 time in total.
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: [SOLVED] MenuManager: Getting first item of second-level

Post by Jos »

Glad it works for you too  ;D

However you don't need to do this

Code: Select all

{if $foo != ''}{$foo}{/if}
because the variable $foo is empty if it's not the first item on the second level

Code: Select all

{$foo}
will do allright.

And I don't know why you need

Code: Select all

{if $smarty.foreach.alist.first}first {/if}{if $smarty.foreach.alist.last}last {/if}
this will only apply to the first en last item in the foreach statement, so the very first menu-item and the last one, which can in fact be 6 levels deep.

But if you have a good reason for it, its fine ;)
Post Reply

Return to “Layout and Design (CSS & HTML)”