foreach,foreachelse

foreach,foreachelse

Table of Contents目录

iteration 用于显示当前循环的执行次数[待考]

first : 当前 foreach 循环第一次执行时 first 被设置成 true.

last : 当前 foreach 循环执行到最后一遍时 last 被设置成 true.

show: 是 foreach 的一个参数. 取值为布尔值 true 或 false. 如果指定为 false 该循环不显示,如果循环指定了 foreachelse 子句,该子句显示与否也取决于 show 的取值.

total: 用于显示循环执行的次数,可以在循环中或循环执行后调用.

Attribute Name Type Required Default 描述
from string Yes n/a The name of the array you are looping through
item string Yes n/a The name of the variable that is the current element
key string No n/a The name of the variable that is the current key
name string No n/a The name of the foreach loop for accessing foreach properties
属性 类型 是否必须 缺省值 描述
from string Yes n/a 待循环数组的名称
item string Yes n/a 当前处理元素的变量名称
key string No n/a
当前处理元素的键名

name string No n/a 该循环的名称,用于访问该循环

foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案).
foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是只能处理简单数组.
foreach 必须和 /foreach 成对使用,且必须指定 from 和 item 属性.
name 属性可以任意指定(字母、数字和下划线的组合).
foreach 可以嵌套,但必须保证嵌套中的 foreach 名称唯一.
from 属性(通常是数组)决定循环的次数.
foreachelse 语句在 from 变量没有值的时候被执行.

Example 7-4. foreach

例 7-4. foreach 演示

{* this example will print out all the values of the $custid array *}
{* 该例将输出数组 $custid 中的所有元素的值 *}
{foreach from=$custid item=curr_id}
	id: {$curr_id}<br>
{/foreach}

OUTPUT:

id: 1000<br>
id: 1001<br>
id: 1002<br>

Example 7-5. foreach key
例 7-5. foreach 键的演示

{* The key contains the key for each looped value

assignment looks like this:

$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}<br>
 {/foreach}
{/foreach}

OUTPUT:

phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>

foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为{$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的 name 属性.

时间: 2024-08-09 23:03:16

foreach,foreachelse的相关文章

夺命雷公狗---Smarty NO:10 foreach数组的遍历

功能:主要实现对数组的遍历输出 基本语法: foreach,foreachelse {foreach from=数组 key=键 name=名称 item=内容 } {foreachelse} {/foreach} from:要遍历输出的数组 item:每次遍历时,系统会自动将遍历的结果放入item内容中 key:键值,每次遍历时,系统会将遍历的键值放入key中 name:foreach名称,为foreach起名 foreachelse:当数组为空时,执行此句 demo4.html示例代码 <!

smarty -- foreach用法

{foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an associative array as well a numerically-indexed array, unlike {section} which is for looping over numerically-indexed arrays only. The syntax for {for

PDO知识点总结

面向对象:object 最先翻译为物质 简称oop 构造函数是对象被创建时自动调用的方法,用来完成类初始化的工作 构造函数可以接受参数,能够在创建对象时赋值给对象属性构造函数可以调用类方法或其他函数构造函数可以调用其他类的构造函数 构造方法在面向对象中,函数被称为方法1 特殊的名称:__construct 2 在实例化对象时,自动被调用3 在内存中划分了对应的内存空间区域 4 构造方法可以像普通方法一样,接收参数:这里的参数通常是用来初始化类中的属性5 构造方法不能重复定义,每个类当中有且只能有

smarty模板基础2

Smarty自带了一些内置函数,这些内置函数是Smarty模板引擎的组成部分.他们被编译成相应的内嵌PHP代码,以获得最大性能. 您创建的自定义函数不能与内置函数同名,也不必修改这些内置函数. 其中一些具有assign属性,用来收集函数结果,并将结果分配给一个命名的模板变量,而不是将内容输出,类似于{assign}函数. 一.{if}{elseif}{else} 条件 随着一些特性加入到模版引擎,Smarty的{if}语句与php的if语句一样富有弹性.每一个{if}必须与一个{/if}成对出现

smarty内置函数

1.{append} 追加 2.{assign} 赋值 3.{block} 块 4.{call} 调用 5.{capture}捕获 6.{config_load}用来从配置文件中加载config变量(#variables#)到模版. 7.{extends} 继承 {extends}标签用在模板继承中子模版对父模板的继承.具体的细节参考模板继承一节. {extends}标签用在模版中的第一行: 如果子模板用{extends}标签继承父模板,那么它只能包含{block}标签(内容),其它任何模板内容

ecstore关于smarty语法调用

以下是smarty语法 转自http://www.phpwindow.com/ecstore_smarty2.html assign 属性 类型 是否必须 描述 var string yes 被赋值的变量名 value string yes 赋给变量的值 <{assign var="age" value=20}> <{$age}> 输出: 20 <{assign var="arr" value=array("s",&

Smarty3&mdash;&mdash;内置函数

Table of Content {$var} {$append} {assign} {block} {call} {config_load} {debug} {extends} {for} {foreach},{foreachelse} {function} {if} {elseif} {else} {include} {include php} {insert} {ldelim} {rdelim} {literal} {nocache} {php} {section} {sectionels

laravel 模版引擎使用

laravel 模版引擎以 @标签 开头,以 @end标签 结尾,常用有 foreach foreachelse if for while等 1)foreach 和 foreachelse 差不到,区别就是 foreachelse 还有一个@empty 分支,当循环的变量为空时执行 @foreach ($obj as $key=>$value) {{--循环体--}} @endforeach @foreachelse ($obj as $key=>$value) {{--循环体--}} @em

PHP 模板引擎Smarty的基本语法

所有的smarty标签都被加上了定界符.在smarty里,所有定界符以外的内容都是静态的,当smarty遇到了模板标签,将尝试解释他们,然后再以恰当的方式输出. 默认情况下是 {和},但它们是可定制的.定制方法是: $smarty->left_delimiter = '<!--{'; $smarty->right_delimiter = '}-->'; 1.注释 模板注释被*号包围,例如 {* this is a comment *} smarty注释将不被输出.它是模板内在的注释