Page 1 of 1

How to count elements of submenu?

Posted: Sat Jun 29, 2013 5:11 pm
by dunn00
I'm showing the menu items like this

Code: Select all

{menu start_level ="3" number_of_levels="1" }
it has another submenu

Code: Select all

{menu start_level ="4" number_of_levels="1" }
with numbers of elements.

I want to put the number of menu items (from level 4) in brackets next to level 3 menu.

for example

Code: Select all

colors (3)
  - red
  - blue
  - white

shapes (4)
  - triangle
  - circle
  - square
  - diamond

Re: How to count elements of submenu?

Posted: Sat Jun 29, 2013 9:00 pm
by velden
Here an example of an altered Menu template which does that. You need to call it with 'number_of_levels=2'

Code: Select all

{* CSS classes used in this template:
.currentpage - The active/current page
.bullet_sectionheader - To style section header
hr.separator - To style the ruler for the separator *} 

{if $count > 0}
<ul id="mainmenu" class="clearfix">
{foreach from=$nodelist item=node}
{if $node->depth > $node->prevdepth}
{assign var="enteredsub" value=true}{assign var="subcount" value=0}
{elseif $node->depth < $node->prevdepth}
{assign var="enteredsub" value=false}({$subcount})</a></li>
{elseif $node->index > 0 && !$enteredsub}</a></li>
{/if}
{if $enteredsub}{assign var="subcount" value=$subcount+1}
{else}

{if $node->current == true}
<li{if $node->extra1 ne ""} class="{$node->extra1}"{/if}><a href="{$node->url}" class="currentpage"{if $node->target ne ""} target="{$node->target}"{/if}>{$node->menutext}

{elseif $node->parent == true && $node->depth == 1 and $node->type != 'sectionheader' and $node->type != 'separator'}
<li class="activeparent{if $node->extra1 ne ""} {$node->extra1}{/if}"> <a href="{$node->url}" class="activeparent"{if $node->target ne ""} target="{$node->target}"{/if}>{$node->menutext}

{elseif $node->type == 'sectionheader'}
<li class="sectionheader{if $node->extra1 ne ""} {$node->extra1}{/if}"><a>{$node->menutext}

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

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

{/if}
{/if}
{/foreach}
{if $enteredsub}({$subcount}){/if}</a></li>
</ul>
{/if}
It uses two extra variables:
enteredsub to determine if it is 'in the submenu' and subcount to count the submenu items.
This should give you an idea how to implement this in your situation. I'm not sure if this is the most efficient way.

Re: How to count elements of submenu?

Posted: Sun Jun 30, 2013 7:02 pm
by dunn00
thank you very much

Re: How to count elements of submenu?

Posted: Sun Jul 28, 2013 10:09 pm
by Rolf
dunn00 wrote:thank you very much
Solved? Please http://forum.cmsmadesimple.org/viewtopi ... =7&t=22304