1, foreach use rules
He has the following several parameters from, item name Iteration Index
2. How to use the Foreach Loop
If PHP wants to pass an array (such as: $array) to Ecshop's smarty template. Then we will accept it through the from= $array.
{foreach from = $array item = Item}
3, Ecshop in the Smarty How to express the subscript
Take a look at the following example:
{foreach from = $array item = Item Name=name}
{$smarty. Foreach.name.iteration}
{/foreach}
The iteration here is the subscript starting from 1,
If you want to start with a subscript from 0, you should use the {$smarty. Foreach.name.index}
4, how to determine whether the Foreach Loop start and end, the last element.
{if $smarty. Foreach.last} represents the last element of the loop. {if $smarty. Freach.first} Indicates the start of the loop.
5. How to use double loops
Examples are as follows:
{foreach from = $test Item =item}
{foreach from= $item. Children Item=child}
{$child. Name}
{/foreach}
{/foreach}
The above five is officially found, and I have summarized the common usage of foreach in EC templates through several days of application.
If backstage: $smarty->assign (' Test ', $test);
{foreach from= $test item=list Name=listone}
<p>{$list}</p>
{/foreach}
From represents assign the corresponding first argument ' test ', the item is the following variable to loop through, name is the names of the foreach loops that are used to access the Foreach property, and the variable name of the key's current name.
But we often see this writing:
<!--{foreach from= $test item=list name=listone key=key}-->
<p>{$list}</p>
<!--{/foreach}-->
In fact, these two are the same, but added <!---->, you have found that using dreamwearver to open these statements will be the same as the annotation of gray display, such a glance can see the label, there is the debugging template, with the browser open, add <!---- > will comment out if foreach and so on, without affecting the original appearance of the template, more convenient for debugging templates.
Multiple loops (two cycles):
<!--{foreach from= $categories item=list}-->
<!--{foreach from= $list. Cat item=cat}-->
<a href= "{$cat. URL}" >{$cat .name}</a>
<!--{/foreach}-->
<!--{/foreach}-->
Only the first two data are recycled:
<!--{foreach from= $categories item=list name=catlist}-->
<!--{if $smarty .foreach.catlist.iteration<2}-->
<a href= "{$list. URL}" >{$list .name}</a>
<!--{/if}-->
<!--{/foreach}-->
Foreachelse:
<!--{foreach from= $categories item=list}-->
<a href= "{$list. URL}" >{$list .name}</a>
<!--{foreachelse}-->
<p>no data</p>
<!--{/foreach}-->
If {foreach from= $categories item=list} A piece of data is not there, then execute the contents of {Foreachelse}{/foreach} below.