Home Backend Development PHP Tutorial Smarty Foreach 使用说明_php模板

Smarty Foreach 使用说明_php模板

May 17, 2016 am 09:26 AM
foreach smarty

foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案)。 foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是只能处理简单数组。
foreach 必须和 /foreach 成对使用,且必须指定 from 和 item 属性。
将 from 属性指定的数组中的数据遍历处理到 item 属性指定的变量中。
参考 foreach (array_expression as $key => $value)
from array_expression;item $value;key $key。
name 属性可以任意指定(字母、数字和下划线的组合)。
foreach 可以嵌套,但必须保证嵌套中的 foreach 名称唯一。
from 属性(通常是数组)决定循环的次数。
foreachelse 语句在 from 属性没有值的时候被执行。(from 属性所指定的值为空时,可用 foreachelse 语句指定——否则-干什么)
foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为 {$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的 name 属性。

foreach 演示
{* 该例将输出数组 $custid 中的所有元素的值 *}

复制代码 代码如下:

{foreach from=$custid item=curr_id}
id: {$curr_id}

{/foreach}

输出结果:
id: 1000
id: 1001
id: 1002
foreach 键的演示和嵌套的演示
{*
数组定义如下:
复制代码 代码如下:

$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
*}
{* 键就是数组的下标,请参看关于数组的解释 *}
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}

{/foreach}
{/foreach}

输出结果:
phone: 1
fax: 2
cell: 3
phone: 555-4444
fax: 555-3333
cell: 760-1234

.index
index 包含当前数组索引,从"0"开始
例如:
复制代码 代码如下:


{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 5 == 0} {* $smarty.foreach.foo.index 对 5 求余 *}

{/if}

{/foreach}
Title
{$i.label}


.iteration
iteration 包含当前循环的执行次数,总是从 1 开始,每执行一次自加 1。
例如:
复制代码 代码如下:

{* 输出 0|1, 1|2, 2|3, ... 等等 *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}

.first
当前 foreach 循环第一次执行时 first 被设置成 true。
例如:
复制代码 代码如下:

{* 当循环第一次执行时显示 LATEST , o 否则显示 id *}

{foreach from=$items key=myId item=i name=foo}




{/foreach}
{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if} {$i.label}


.last
当前 foreach 循环执行到最后一遍时 last 被设置成 true.
例如:
复制代码 代码如下:

{* 在列表最后添加水平线 *}
{foreach from=$items key=part_id item=prod name=products}
{$prod}{if $smarty.foreach.products.last}
{else},{/if}
{foreachelse}
... content ...
{/foreach}

.total
total 用于显示循环执行的次数,可以在循环中或循环执行后调用.
例如:
复制代码 代码如下:

{* 在最后显示行数 *}
{foreach from=$items key=part_id item=prod name=foo}
{$prod.label}

{if $smarty.foreach.foo.last}
{$smarty.foreach.foo.total} items

{/if}
{foreachelse}
... something else ...
{/foreach}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

PHP returns the current element in an array PHP returns the current element in an array Mar 21, 2024 pm 12:36 PM

This article will explain in detail about the current element in the array returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Get the current element in a PHP array PHP provides a variety of methods for accessing and manipulating arrays, including getting the current element in an array. The following introduces several commonly used techniques: 1. current() function The current() function returns the element currently pointed to by the internal pointer of the array. The pointer initially points to the first element of the array. Use the following syntax: $currentElement=current($array);2.key() function key() function returns the array internal pointer currently pointing to the element

How to iterate through the properties of an object using the forEach function? How to iterate through the properties of an object using the forEach function? Nov 18, 2023 pm 06:10 PM

How to iterate through the properties of an object using the forEach function? In JavaScript, we often need to traverse the properties of objects. If you want to use a concise way to iterate over the properties of an object, the forEach function is a very good choice. In this article, we will explain how to use the forEach function to iterate over the properties of an object and provide specific code examples. First, let's understand the basic usage of the forEach function. forEach function is Java

How to use PHP and Smarty to achieve front-end and back-end separation development How to use PHP and Smarty to achieve front-end and back-end separation development Jun 25, 2023 pm 01:46 PM

In modern web development, the separation of front-end and back-end has become a very popular trend, which allows developers to better organize projects and improve the efficiency of project development. PHP and Smarty are two very commonly used technologies, which can be used to achieve front-end and back-end separation development. This article will introduce how to use PHP and Smarty to achieve front-end and back-end separation development. What is front-end and back-end separation development? In traditional web development, the front-end is mainly responsible for the presentation of the page and the logic of interaction with the back-end. The backend is mainly responsible for the business

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.

A preliminary study on the template engine Smarty in PHP A preliminary study on the template engine Smarty in PHP May 11, 2023 pm 05:15 PM

Nowadays, website development is inseparable from an important component-template engine. A template engine refers to a tool that combines page templates and data to generate HTML code with a specific format. In various website development frameworks, the template engine is an essential component, because the template engine can greatly reduce the duplication of code and improve the dynamics of the page. One of the most common and popular template engines is Smarty. Smarty is a DSL (DomainSpecif

See all articles