Forums / Setup & design / Counter increment problem in template page

Counter increment problem in template page

Author Message

Lex 007

Monday 14 February 2005 12:45:36 am

Hello

I have the following code in my template. The problem is the variable $counter never gets incremeted. Where is my problem ?

Thank you.

{let page_limit=10
counter=0
children=fetch('content','list',
hash(parent_node_id,2,
limit,$page_limit,
offset,$view_parameters.offset))
child_count=fetch('content','list_count',hash(parent_node_id,$node.node_id))}
{section name=Child loop=$children}
{set counter=$counter+1}
mml[{$counter}]=new Array('0','{$Child:item.name}',{$Child:item.url_alias|ezurl},'');
{let sub_children=fetch('content','list',hash(parent_node_id,$Child:item.node_id,limit,$page_limit))
sub_child_count=fetch('content','list_count',hash(parent_node_id,$Child:item.node_id))}
{section name=SubChild loop=$:sub_children}
{set counter=$counter+1}
mml[{$counter}]=new Array('1','{$:item.name}',{$:item.url_alias|ezurl},'');
{/section}

{/let}

{/section}

{/let}

Lex 007

Monday 14 February 2005 12:52:20 am

In fact I have this error when activating debug :

Undefined index: counter in /path/to/ezpublish ....

kracker (the)

Monday 14 February 2005 1:29:32 am

I always thought you had to reference variables like your trying to do in the above, by using full scope, here is an example :

Instead of :
{set counter=$counter+1}

Shouldn't it be similar to this instead :
{let counter=$Child:counter+1}
{let counter=$SubChild:counter+1}
{let counter=$Folder:Subfolder:index}

At least I was doing something like this recently and had to figure this out in my own prison. I got it working using code similar to the above examples.

The important part I found was if your doing individual loops you can leave out the full scope but if your tryign to do loops inside of loops you must use full variable scope which is descending, per section loop child or sub loop subchild your variables have to include the full variable scope {$Folder:Subfolder:index} / $Child:SubChild:item.name / $Child:Subchild:counter

Then I found that all section loops already include a per iteration counter variable called index and you can reference it like this : {$Child:index}

You may wish to spend little more time reading the documentation while trying to do this to make sure i'm not completely off my rocker ...

http://ez.no/ez_publish/documentation/reference/template_functions/program_flow/section

Snippet from above link:
* Each time the section iterates it sets four template variables in the new namespace. The variables are index, number, key and item.

* index - is a number which starts at 0 and increases for each iteration.
* number - same as index but starts at 1
* key - if an array is iterated the key of the current item is set, if a number it will be the same as item
* item - if an array is iterated the current item is set, if a number the current iteration index is set (same as index)

cheers,
//kracker

But you don't have to take my word for it (, find out yourself) -- Reading Rainbow : Lamar (Host)

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Eivind Marienborg

Monday 14 February 2005 3:49:04 am

You could also check out the inc function:
http://www.ez.no/ez_publish/documentation/reference/template_operators/mathematics/inc

Example from the above link:

{let i=255}
{set i=inc( $i )}
{$i}
{/let}

returns 256.

Lex 007

Monday 14 February 2005 4:09:11 am

Thank you all for your help.

The code you posted works ... but not inside my specific code :-(

In fact, the problem seems more to come from the {section} loops : the variable $counter is not recognized as a "global" variable in the loops, althought it is defined in a {let} tag before.

kracker -> I'm trying to build my code as you said

kracker (the)

Monday 14 February 2005 4:22:25 am

Hrm,

Well, I was being a bit lazy and should have just rewrote your code to match was I thought it should look like instead.

<b>Modified Original</b>

{let page_limit=10
children=fetch('content','list',
hash(parent_node_id,2,
limit,$page_limit,
offset,$view_parameters.offset))
child_count=fetch('content','list_count',hash(parent_node_id,$node.node_id))}

{section name=Child loop=$children}
mml[{$Child:index}]=new Array('0','{$Child:item.name}',{$Child:item.url_alias|ezurl},'');

{let sub_children=fetch('content','list',hash(parent_node_id,$Child:item.node_id,limit,$page_limit))
sub_child_count=fetch('content','list_count',hash(parent_node_id,$Child:item.node_id))}

{section name=SubChild loop=$:sub_children}
mml[{$Child:SubChild:index}]=new Array('1','{$:item.name}',{$:item.url_alias|ezurl},'');
{/section}
{/let}

{/section}
{/let} 

<b>My Example</b>
I'm using this in an upcoming js navigation component for eZ publish, It might help... (works great for me)

    ///////////////////////////////////////////////////////////////
    // Menu : Dynamic : Fetchs

    var treeArray = new Array();

{let folder_list=fetch(content,list,hash(parent_node_id,2,sort_by,array(array(priority, false() )) ))
     nodes_count=$folder_list|count() }
 {section name=Folder loop=$folder_list}
 {* Exclude: Forms Folder *} {section-exclude match=$:item.node_id|eq(70)}
 {* Exclude: Faqs Folder *} {section-exclude match=$:item.node_id|eq(84)}
 {* Exclude: Download Folder *} {section-exclude match=$:item.node_id|eq(83)}
 {let sub_folder_list=fetch(content,list,hash(parent_node_id,$Folder:item.node_id,sort_by,array(array(priority))))
      sub_nodes_count=$Folder:sub_folder_list|count() }
{* // Parents *}
treeArray[{$Folder:index}] = new Array();

        treeArray[{$Folder:index}]['name'] = '{$Folder:item.name|shorten(30)}';
        treeArray[{$Folder:index}]['path'] = {$Folder:item.url_alias|ezroot(single)};

        {section name=Subfolder loop=$Folder:sub_folder_list}
        {let sub_menu_list=fetch(content,list,hash(parent_node_id,$Folder:Subfolder:item.node_id,sort_by,array(array(priority))))
             sub_menu_count=$Folder:Subfolder:sub_menu_list|count() }

{* // Children *}
treeArray[{$Folder:index}][{$Folder:Subfolder:index}] = new Array();
         treeArray[{$Folder:index}][{$Folder:Subfolder:index}]['name'] = '{$Folder:Subfolder:item.name|shorten( 25 )}';
         treeArray[{$Folder:index}][{$Folder:Subfolder:index}]['path'] = '/{$Folder:Subfolder:item.url_alias:item.url_alias|ezroot|trim("\x22")}';
{* // GrandChildren *}
       {section name=Submenu loop=$Folder:Subfolder:sub_menu_list}

treeArray[{$Folder:index}][{$Folder:Subfolder:index}][{$Folder:Subfolder:Submenu:index}] = new Array();
         treeArray[{$Folder:index}][{$Folder:Subfolder:index}][{$Folder:Subfolder:Submenu:index}]['name'] = '{$Folder:Subfolder:Submenu:item.name|shorten( 25 )}';
         treeArray[{$Folder:index}][{$Folder:Subfolder:index}][{$Folder:Subfolder:Submenu:index}]['path'] = '/{$Folder:Subfolder:Submenu:item.url_alias:item.url_alias|ezroot|trim("\x22")}';
        {/section}

     {/section}
    {/let}

{/section}
{/let}

////////////////////////////////////////////////////////////////

cheers,
//kracker

E & A : Reintroducing
E & A : Now : http://www.epitaph.com/_lib/stream.php?f=Now.mp3&p=/149/349/4028.mp3&id=4028

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Ɓukasz Serwatka

Monday 14 February 2005 5:02:15 am

For me this works fine

{let count=0}
    {section loop=array(1,2,3,4,5,6,7,8)}
        {set count=$count|inc}
    {/section}
    {$count}	
{/let}

Output is : 8

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

Lex 007

Monday 14 February 2005 6:05:36 am

I probably should I have exposed this in the first message, sorry : I actually want the output to look like this way :

mml[0]=new Array('0','Map',"/index.php/mysite/map",'');
mml[1]=new Array('0','Guestbook of this web site',"/index.php/mysite/guestbook_of_this_web_site",'');
mml[2]=new Array('1','Madonna DeLafore',"/index.php/mysite/guestbook_of_this_web_site/madonna_delafore",'');
mml[3]=new Array('1','Bill Collins',"/index.php/mysite/guestbook_of_this_web_site/bill_collins",'');
mml[4]=new Array('1','Tina Turnoff',"/index.php/mysite/guestbook_of_this_web_site/tina_turnoff",'');
mml[5]=new Array('0','History',"/index.php/mysite/history",'');
mml[6]=new Array('0','Philosophy',"/index.php/mysite/philosophy",'');
mml[7]=new Array('0','Activity',"/index.php/mysite/activity",'');
mml[8]=new Array('1','Private',"/index.php/mysite/activity/private",'');

With the first value of the Array() (0 or 1 im this case) telling the level of the menu (child or subchild)

Indeed, I guess I must be having a problem in my EZ Publish install because even the code you posted for your JS menu displays funny things (counters are NOT incremented as well ...)

Lex 007

Thursday 17 February 2005 3:44:29 am

Here is the final version of my dynamic menu (using CSS instead of Javascript for browser compatibility). Works on EZ Publish 3.4.4

menu.tpl :

<div id="holdm">
<ul id="nav">
{let page_limit=10
     children=fetch('content','list',
                    hash(parent_node_id,2,
                    limit,$page_limit,
                    offset,$view_parameters.offset,class_filter_type, "include", class_filter_array, array(1)))
     child_count=fetch('content','list_count',hash(parent_node_id,$node.node_id,class_filter_type, "include", class_filter_array, array(1)))}
 
{section name=Child loop=$children last-value}
    <li>
        <a href={$Child:item.url_alias|ezurl}>{$Child:item.name}</a>
    
        {let sub_children=fetch('content','list',hash(parent_node_id,$Child:item.node_id,limit,$page_limit,class_filter_array, array(1)))
            sub_child_count=fetch('content','list_count',hash(parent_node_id,$Child:item.node_id,class_filter_type, "include", class_filter_array, array(1)))}

        {section show = $:sub_child_count|gt(0)}
        <ul>
            {section name=SubChild loop=$:sub_children last-value}
                
                    <li>
                        <a href={$:item.url_alias|ezurl}>{$:item.name}</a>
                        
                        {let sub_sub_children=fetch('content','list',hash(parent_node_id,$:item.node_id,limit,$page_limit,class_filter_type, "include", class_filter_array, array(1)))
                          sub_sub_child_count=fetch('content','list_count',hash(parent_node_id,$:item.node_id,limit,$page_limit,class_filter_type, "include", class_filter_array, array(1)))}
                        {section show = $:sub_sub_child_count|gt(0)}
                            <ul>
        
                            {section name=sub_SubChild loop=$:sub_sub_children last-value}
                                
                                    <li>
                                        <a href={$:item.url_alias|ezurl}>{$:item.name}</a>
                                    </li>
                                
                            {/section}
                           
                            </ul>
                        {/section}
                        {/let}
                        
                        
                        
                    </li>
                
            {/section}
             
            </ul>
        {/section}
        {/let}
    </li>
{/section}

{/let}
</ul>
</div>
<SCRIPT language="javascript" type="text/javascript" src={"javascripts/multimenu.js"|ezdesign}></SCRIPT>

multimenu.js

activateMenu = function(nav) {
	if (document.all && document.getElementById(nav).currentStyle) {  
		var navroot = document.getElementById(nav);
        /* Get all the list items within the menu */
        var lis=navroot.getElementsByTagName("LI");  
		for (i=0; i<lis.length; i++) {
            /* If the LI has another menu level */
            if(lis[i].lastChild.tagName=="UL"){
                /* assign the function to the LI */
             	lis[i].onmouseover=function() {		
                   /* display the inner menu */
                   this.lastChild.style.display="block";
      			}
				lis[i].onmouseout=function() {                       
                    this.lastChild.style.display="none";
				}
            }
		}
	}
}

window.onload= function()
{
    activateMenu('nav'); 
}

multimenu.css

#holdm{position:absolute; top:67px; left:0px; margin-left:auto; z-index:100; }

#nav{
	list-style: none;
	}

 #nav ul { 
	padding: 0;
	margin:0;
	list-style: none;
    width:12.5em;
    z-index:99;
    position:relative;
    overflow:visible;  
	background-color:#FFFFFF;
	
   }

#nav li { 
    margin:0;   
    position: relative; 
    float:left;
	width: 12.5em;
    display:block;
    height:auto;
	background-color:#E5E5E5; 
}
 
#nav a {
    text-decoration:none; 
    display:block;
    padding: 0.1em;  
    margin:0.2em 0 0.2em 0.1em; 
    background-color:#E5E5E5;
    width:12em; 
    height:1em;
	 font-weight:bold ;
}

#nav a:hover{
    background-color:#FFFFFF; 
    color:#333333;
    background-image:none;
	 font-weight:bold ;
}

#nav ul{
    display:none;   
}

/*all see this */
#nav ul ul, #nav ul ul ul{
    display:none; 
    position:absolute; 
    margin-top:-1.8em; 
    margin-left:12em;
}

/* non-IE browsers see this */
 #nav ul li>ul, #nav ul ul li>ul{
    margin-top:-1.4em; 
 }

#nav li:hover ul ul, #nav li:hover ul ul ul{
    display:none; 
}

#nav li:hover ul, #nav ul li:hover ul, #nav ul ul li:hover ul{
    display:block;
}

li>ul { 
	top: auto;
	left: auto;
}

Lo' F.

Wednesday 01 June 2011 10:01:31 am

Encountered a problem like that...

In order to increment the counter inside a section loop I had to set the variable as global..

{def $count=0}
{section name="Order" ...}

    {set scope='global' $count = $Order:number}

{/section}

{$#count}

loredanaebook.it