PHP模板引擎Smarty内建函数section,sectionelse用法详解,smartysectionelse_PHP教程

WBOY
Release: 2016-07-12 08:54:37
Original
630 people have browsed it

PHP模板引擎Smarty内建函数section,sectionelse用法详解,smartysectionelse

本文实例讲述了PHP模板引擎Smarty内建函数section,sectionelse用法。分享给大家供大家参考,具体如下:

section 是 Smarty 模板中除了 foreach 以外的另一种处理循环的方案,section 比 foreach 要灵活,就像是一个改进的 foreach 语句,除了拥有相同的循环特性外,还提供了很多附加选项,可以更好的控制循环的执行。在模板中,必须使用成对的 section 标记,有两个必须设置的属性 name 和 loop ,关于 section 的属性请看下表:

属性 类型 是否必须 缺省值 描述
name string Yes n/a 该循环的名称
loop [$variable_name] Yes n/a 决定循环次数的变量名称
start integer No 0 循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起. 例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5. 非法值(超过了循环数组的下限)将被自动调整为最接近的合法值.
step integer No 1 该值决定循环的步长. 例如指定step=2将只遍历下标为0、2、4等的元素. 如果step为负值,那么遍历数组的时候从后向前遍历.
max integer No 1 设定循环最大执行次数.
show boolean No true 决定是否显示该循环.

我们通过一个实例,来演示 Smarty 中 {section} 和 {sectionelse} 的使用。

实例思路:从数据库中取出内容,赋给一个数组变量 $_html ,再给这个数组变量分配给模板,然后在模板中进行该数组的遍历。

数据库、主文件 index.php,Smarty 模板初始化文件 init.inc.php,可参考前面一篇《PHP模板引擎Smarty内建函数foreach,foreachelse用法分析》

/tpl/index.tpl

   section,sectionelse 
   <{section loop=$data name="ls" max="100" start="0" step="2" }>  <{if $smarty.section.ls.first}>  <{elseif $smarty.section.ls.last}>  <{else}>  <{/if}>  <{sectionelse}>  <{/section}> <{if $data}>  <{/if}> 
编号(iteration) 编号(rownum) 姓名 电子邮件 添加时间
<{$smarty.section.ls.iteration}> <{$smarty.section.ls.rownum}> <{$data[ls].username}> <{$data[ls].email}> <{$data[ls].addTime}>
对不起!暂时没有数据。
循环的次数为:<{$smarty.section.ls.total}>
Copy after login

执行结果:

section 循环区域中可以使用的变量

变量名 描述
index 用于显示当前循环的索引,从 0 开始(如果设置了 start 属性,那么就由该值开始),每次加 1,(如果指定了 step 属性,那么由该值决定)
index_prev 用于显示上一个循环索引值,循环开始时,此值为 -1
index_next 用于显示下一个循环索引值,循环执行到最后一次时,此值仍然比当前索引值大 1(如果指定了 step 属性,那么由该值决定)
iteration 用于显示循环的次数
first 当前 section 循环在第一次执行时该变量的值为 true
last 当前 section 循环在最后一次执行时该变量的值为 true
rownum 用于显示循环的次数,该属性是 iteration 的别名,两者相同
loop 用于显示该循环上一次循环时的索引值,该值可以用于循环内部或循环结束后
show 是 section 的参数,show 取值为布尔值 true 和 false,如果设置为false,该循环将不显示。如果指定了 sectionelse 子句,该子句是否显示也取决于该值
total 用于显示循环执行的次数。不仅可以在循环中,也可以在执行结束后调用此属性

更多关于PHP相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP模板引擎Smarty内建函数详解
  • PHP模板引擎Smarty内置变量调解器用法详解
  • PHP模板引擎Smarty自定义变量调解器用法
  • PHP模板引擎Smarty中的保留变量用法分析
  • PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
  • PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
  • PHP模板引擎Smarty中变量的使用方法示例
  • smarty模板引擎从php中获取数据的方法
  • ThinkPHP使用smarty模板引擎的方法
  • 在PHP模板引擎smarty生成随机数的方法和math函数详解
  • PHP模板引擎Smarty的缓存使用总结
  • php smarty模板引擎的6个小技巧
  • [PHP]模板引擎Smarty深入浅出介绍

www.bkjia.com true http://www.bkjia.com/PHPjc/1119970.html TechArticle PHP模板引擎Smarty内建函数section,sectionelse用法详解,smartysectionelse 本文实例讲述了PHP模板引擎Smarty内建函数section,sectionelse用法。分享给大家...
Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!