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

eZ debug

Timing: Jan 17 2025 23:00:35
Script start
Timing: Jan 17 2025 23:00:35
Module start 'content'
Timing: Jan 17 2025 23:00:36
Module end 'content'
Timing: Jan 17 2025 23:00:36
Script end

Main resources:

Total runtime1.1934 sec
Peak memory usage4,096.0000 KB
Database Queries221

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0072 587.8438180.8125
Module start 'content' 0.00721.0652 768.6563820.8906
Module end 'content' 1.07230.1210 1,589.5469357.6797
Script end 1.1933  1,947.2266 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00400.3365210.0002
Check MTime0.00150.1244210.0001
Mysql Total
Database connection0.00100.081910.0010
Mysqli_queries1.074390.02202210.0049
Looping result0.00230.19392190.0000
Template Total1.157797.020.5788
Template load0.00200.164220.0010
Template processing1.155796.840220.5779
Template load and register function0.00020.014310.0002
states
state_id_array0.00130.112710.0013
state_identifier_array0.00200.171420.0010
Override
Cache load0.00180.14831040.0000
Sytem overhead
Fetch class attribute can translate value0.00220.183360.0004
Fetch class attribute name0.00160.1373150.0001
XML
Image XML parsing0.00620.522060.0010
class_abstraction
Instantiating content class attribute0.00000.0040200.0000
General
dbfile0.00980.8225550.0002
String conversion0.00000.000730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
10content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
10content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
24content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
10content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
9content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 65
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs