adding .last class to last item in menu

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
marksimo
New Member
New Member
Posts: 2
Joined: Mon Mar 17, 2008 7:05 pm

adding .last class to last item in menu

Post by marksimo »

Hi Everyone,

For styling purposes, I'm trying to find a way to add a .first class to the first link in a menu and a .last class to the last link.
I've been trying to change the code from simple_navigation.tpl and can't figure out the way to do this.

Any ideas?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: adding .last class to last item in menu

Post by calguy1000 »

refer to the foreach section in http://smarty.php.net/manual/en

it may help.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
[DrAg]
New Member
New Member
Posts: 7
Joined: Wed Sep 05, 2007 12:16 pm

Re: adding .last class to last item in menu

Post by [DrAg] »

Like to post some working code here, because I could not find any code example on this forum or wiki.

Code: Select all

{* CSS classes used in this template:
.current - The active page in the horizontal menu (first level). 
.bullet_sectionheader - To style section header
hr.separator - To style the ruler for the separator *} 

{if $count > 0}
<ul id="menu">
{foreach from=$nodelist item=node name=items}
{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 or $node->parent == true}
<li class="active"><a href="{$node->url}" {if $node->target ne ""} 
target="{$node->target}"{/if}>{$node->menutext}</a>
{elseif  $smarty.foreach.items.last}
<li class="last"><a href="{$node->url}" {if $node->target ne ""} 
target="{$node->target}"{/if}>{$node->menutext}</a>
{elseif $node->type == 'sectionheader'}
<li><span class="bullet_sectionheader">{$node->menutext}</span>
{elseif $node->type == 'separator'}
<li style="list-style-type: none;"><hr class="separator" />
{else}
<li><a href="{$node->url}"{if $node->target ne ""} target="{$node->target}"{/if}>{$node->menutext}</a>{/if}
{/foreach}

{repeat string="</li></ul>" times=$node->depth-2}</li>
</ul>
{/if}
User avatar
webform
Power Poster
Power Poster
Posts: 503
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

Re: adding .last class to last item in menu

Post by webform »

Hi DrAg

I've adapted your code with some succes - But i have changed some as i use it both for my main menu and a sub menu. The only problem i have is if the a page is the current and the last page in the menu it still shows the graphic. Isn't there some way to tell menumanager that something is both .last class and current?

My code: 

Code: Select all

{if $count > 0}
<ul>
{foreach from=$nodelist item=node name=items}
{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><a href="{$node->url}" {if $node->target ne ""} 
target="{$node->target}"{/if}id="current" name="current">{$node->menutext}</a>

{elseif $node->parent == true}
<li><a href="{$node->url}" {if $node->target ne ""} 
target="{$node->target}"{/if}id="currentAncestor" name="currentAncestor">{$node->menutext}</a>

{elseif  $smarty.foreach.items.last}
<li class="last"><a href="{$node->url}" {if $node->target ne ""} 
target="{$node->target}"{/if}>{$node->menutext}</a>

{elseif $node->type == 'sectionheader'}
<li class="sectionheader">{$node->menutext}

{elseif $node->type == 'separator'}
<li class="separator" style="list-style-type: none;"> <hr />

{else}
<li><a href="{$node->url}"{if $node->target ne ""} target="{$node->target}"{/if}>{$node->menutext}</a>{/if}
{/foreach}

{repeat string="</li></ul>" times=$node->depth-2}</li>
</ul>
{/if}
I've tried to write this code but something is wrong with it - looks like it won't accept the AND operator or it's because it conflicts with the

Code: Select all

{elseif  $smarty.foreach.items.last}

Code: Select all

{elseif  $smarty.foreach.items.last and $node->current == true}
<li class="last"><a href="{$node->url}" {if $node->target ne ""}
target="{$node->target}"{/if}id="current" name="current">{$node->menutext}</a>
Pierre M.

Re: adding .last class to last item in menu

Post by Pierre M. »

[DrAg] wrote: Like to post some working code here, because I could not find any code example on this forum or wiki.
Thank you for sharing. Could you please post this in the wiki (using your forum account) rather than in the forum ? Feel free to contribue to the wiki.
http://wiki.cmsmadesimple.org/index.php ... nu_Manager

Pierre M.
User avatar
webform
Power Poster
Power Poster
Posts: 503
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

Re: adding .last class to last item in menu

Post by webform »

OK i got a solution that is working  ;D

It was wrong to try to write an AND condition - instead i wrote:

Code: Select all

{if $node->current == true}
<li{if $smarty.foreach.items.last} class="last"{else}{/if}><a href="{$node->url}" {if $node->target ne ""} 
target="{$node->target}"{/if}id="current" name="current">{$node->menutext}</a>
Much more simpel and saves some code lines.
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: adding .last class to last item in menu

Post by duclet »

Remove that else, you don't need it. {if $smarty.foreach.items.last} class="last"{/if} is enough.
User avatar
webform
Power Poster
Power Poster
Posts: 503
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

Re: adding .last class to last item in menu

Post by webform »

duclet wrote: Remove that else, you don't need it. {if $smarty.foreach.items.last} class="last"{/if} is enough.
Doh! Your right  :-[
Post Reply

Return to “Developers Discussion”