The use method of Ecshop template cyclic foreach and the detailed parameters

Source: Internet
Author: User
Tags foreach smarty template

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.