PHP模板引擎Smarty内建函数section,sectionelse用法详解,smartysectionelse_PHP教程
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深入浅出介绍

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function