Page 1 of 1

Need way to print count variable when displaying menu

Posted: Sun May 28, 2006 11:33 am
by Ryno
I need a way of assigning a count variable to a , everytime the menu goes one level down. I've tried mucking around with the smarty syntax but have had no luck.

Below is a code snippet of what I need to achieve (see how the value after 'xc' increments each time)

Code: Select all

<ul id="nav2xc">
    <li><a href="#" title="xx">Menu item</a></li>
    <li><a href="#" title="xx">Menu item</a></li>
    <li><a href="#" title="xx">Menu item</a> 
      <ul id="xc1" title="Menu 1">
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
      </ul>
    </li>
    <li><a href="#" title="xx">Menu item</a> 
      <ul id="xc2" title="Menu 2">
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
        <li><a href="#" title="xx">Sub menu item</a></li>
      </ul>
    </li>
  </ul>
Any help would be appreciated.

Ryno

Re: Need way to print count variable when displaying menu

Posted: Sun May 28, 2006 12:31 pm
by tsw
{$node->hierarchy|truncate:1:""}

of if you go over 10 you might need something like

{$node->hierarchy|truncate:2:""|strip:"."}

Re: Need way to print count variable when displaying menu

Posted: Mon May 29, 2006 1:53 am
by Ryno
Hey tsw,

I thought the solution might be something along those lines, although I would need to provide a check to capture values over 10.

Your solution though only solves half of my problem. I am having dramas with the repeat block as follows:

Code: Select all

{repeat string="<ul id=\"xc\">" times=$node->depth-$node->prevdepth}
You can only insert a string for 'string' (funny that) and I don't think it's possible to put '{$node->hierarchy|truncate:1:""}' in there (I've really really tried).

Below is what would be cool, but it doesn't.

Code: Select all

{repeat string="<ul id=\"xc{$node->hierarchy|truncate:1:""}\">" times=$node->depth-$node->prevdepth}
I guess what I should also be asking is how to use the {repeat} block and can I use something else instead?

Ryno

Re: Need way to print count variable when displaying menu

Posted: Mon May 29, 2006 3:58 am
by Dr.CSS
just curious... what you need the xc1 xc2 for... does it have to be 1, 2, 3, ...

  mark

Re: Need way to print count variable when displaying menu

Posted: Mon May 29, 2006 4:22 am
by Ted
Time to break out the goofy smarty syntax...

Code: Select all

{capture name=myul}<ul id="xc{$node->hierarchy|truncate:1:""}">{/capture}
{repeat string=$smarty.capture.myul times=$node->depth-$node->prevdepth}
Hope that helps

Re: Need way to print count variable when displaying menu

Posted: Mon May 29, 2006 5:09 am
by tsw
Mark: css class and id shouldnt start with a number

Re: Need way to print count variable when displaying menu

Posted: Mon May 29, 2006 5:41 am
by Ryno
Wonderful, I feel like Smarty and I have made up for the time being.

I don't expect anyone to post to this as it's my bad and quite frankly, I'm stoked with the amount of help I've recieved but...

I realised the value I actually need is not dependant upon the hierarchy as such. I really just need a count variable that is incremented everytime we go down 1 level.

Code: Select all

{if $node->depth > $node->prevdepth}
So how best to do this count variable thing? I've got a solution but I thinkt it's cludgy, mainly cause I wrote it. Can anyone suggest a 'best practices' solution.?

Ryno

Re: Need way to print count variable when displaying menu

Posted: Mon May 29, 2006 5:42 am
by Ryno
@Mark: It needs to be xc1, xc2, xc3 because it's a show/hide menu thing and that's how the script identifies menu's to be expanded and contracted.

Have a look at http://inspire.server101.com/js/xc/ for more info.

Ryno