PHP模板引擎Smarty内建函数section,sectionelse用法详解,smartysectionelse
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
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>section,sectionelse</title> </head> <body> <table align="center" border="1" width="800"> <tr> <th>编号(iteration)</th> <th>编号(rownum)</th> <th>姓名</th> <th>电子邮件</th> <th>添加时间</th> </tr> <{section loop=$data name="ls" max="100" start="0" step="2" }> <!-- 使用 section 遍历数组 $data,max 表示最多可以循环多少条,start 表示从哪个数组下标开始显示,step决定了循环的步长,如果设置为2,那么将遍历下标为0,2,4……的元素 --> <!-- 在此,我们做几个保留变量 $smarty.section 的操作 --> <!-- 当数据显示第一条的时候,第一行的表格背景为黄色,使用属性:first --> <!-- 当数据显示最后一条的时候,最后一行的表格背景为蓝色,使用属性:last --> <{if $smarty.section.ls.first}> <tr align="center" bgcolor="#FFFF00"> <{elseif $smarty.section.ls.last}> <tr align="center" bgcolor="#0000FF"> <{else}> <tr align="center"> <{/if}> <td><{$smarty.section.ls.iteration}></td> <!-- iteration 是保留变量中显示行号的属性 --> <td><{$smarty.section.ls.rownum}></td> <!-- rownum 是保留变量中显示行号的属性 --> <td><{$data[ls].username}></td> <!-- 输出数组第二维下标为 username 的元素值 --> <td><{$data[ls].email}></td> <!-- 输出数组第二维下标为 email 的元素值 --> <td><{$data[ls].addTime}></td> <!-- 输出数组第二维下标为 addTime 的元素值 --> </tr> <{sectionelse}> <!-- 如果分配过来的数组没有内容的话,显示下面内容 --> <tr> <td colspan="5">对不起!暂时没有数据。</td> </tr> <{/section}> <{if $data}> <!-- 如果循环的次数不为空的话,那么使用 Smarty 的保留变量 {$smarty.section} 显示出循环的次数 --> <tr> <td align="center" colspan="5">循环的次数为:<{$smarty.section.ls.total}></td> </tr> <{/if}> </table> </body> </html>
执行结果:
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深入浅出介绍

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
