博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
smarty学习——内建函数(部分接上)
阅读量:6637 次
发布时间:2019-06-25

本文共 6266 字,大约阅读时间需要 20 分钟。

9.{foreach} {foreachelse}

格式如下:

{
foreach $arrayvar as $itemvar}{
foreach $arrayvar as $keyvar=>$itemvar}

 

foreach 和for 的目的是相似的都是进行循环的数据操作,具有以下特性:

a.支持嵌入,就是我们可以在foreach 中继续使用foreach

b.通常使用的数据 $arrayvar  是数组类型的数据。

c.foreachelse 是当在数组中没有数据时执行。

d.{foreach} 内置一些属性  @index, @iteration, @first, @last, @show, @total.

e.里面可以包含{break} { continue}

如下例子:

简单例子:

assign('myColors', $arr);?>模板文件如下:
    {
    foreach $myColors as $color}
  • {
    $color}
  • {
    /foreach}
输出结果:
  • red
  • green
  • blue

处理键值:

'John', 'lname' => 'Doe', 'email' => 'j.doe@example.com');$smarty->assign('myPeople', $people);?> 键值模板:
    {
    foreach $myPeople as $value}
  • {
    $value@key}: {
    $value}
  • {
    /foreach}
输出:
  • fname: John
  • lname: Doe
  • email: j.doe@example.com

foreach 的嵌套:

assign('contacts', array( array('phone' => '555-555-1234', 'fax' => '555-555-5678', 'cell' => '555-555-0357'), array('phone' => '800-555-4444', 'fax' => '800-555-3333', 'cell' => '800-555-2222') ));?> 模板文件:{
* key always available as a property *}{
foreach $contacts as $contact} {
foreach $contact as $value} {
$value@key}: {
$value} {
/foreach}{
/foreach}{
* accessing key the PHP syntax alternate *}{
foreach $contacts as $contact} {
foreach $contact as $key => $value} {
$key}: {
$value} {
/foreach}{
/foreach} 输出 phone: 555-555-1234 fax: 555-555-5678 cell: 555-555-0357 phone: 800-555-4444 fax: 800-555-3333 cell: 800-555-2222

@index 的使用:

{
foreach $items as $i} {
if $i@index eq 3} {
* put empty table row *}
{
/if}
{
/foreach}
nbsp;
{
$i.label}

{break}的使用:

{
$data = [1,2,3,4,5]} {
foreach $data as $value} {
if $value == 3} {
* abort iterating the array *} {
break} {
/if} {
$value} {
/foreach} {
* prints: 1 2 *}

 10. {if } {elseif} {else}

这几个语法比较常用:

这是使用的语法表格:

Qualifier Alternates Syntax Example Meaning PHP Equivalent
== eq $a eq $b equals ==
!= ne, neq $a neq $b not equals !=
> gt $a gt $b greater than >
< lt $a lt $b less than <
>= gte, ge $a ge $b greater than or equal >=
<= lte, le $a le $b less than or equal <=
===   $a === 0 check for identity ===
! not not $a negation (unary) !
% mod $a mod $b modulous %
is [not] div by   $a is not div by 4 divisible by $a % $b == 0
is [not] even   $a is not even [not] an even number (unary) $a % 2 == 0
is [not] even by   $a is not even by $b grouping level [not] even ($a / $b) % 2 == 0
is [not] odd   $a is not odd [not] an odd number (unary) $a % 2 != 0
is [not] odd by   $a is not odd by $b [not] an odd grouping ($a / $b) % 2 != 0

 

11 .include

include的语法表格:

Attribute Name Type Required Default Description
file string Yes n/a The name of the template file to include
assign string No n/a The name of the variable that the output of include will be assigned to
cache_lifetime integer No n/a Enable caching of this subtemplate with an individual cache lifetime
compile_id string/integer No n/a Compile this subtemplate with an individual compile_id
cache_id string/integer No n/a Enable caching of this subtemplate with an individual cache_id
scope string No n/a Define the scope of all in the subtemplate assigned variables: 'parent','root' or 'global'
[var ...] [var type] No n/a variable to pass local to template

可选的选项:

Name Description
nocache Disables caching of this subtemplate
caching Enable caching of this subtemplate
inline If set merge the compile code of the subtemplate into the compiled calling templat

 

12 .include_php

 

Attribute Name Type Required Default Description
file string Yes n/a The name of the php file to include as absolute path
once boolean No TRUE whether or not to include the php file more than once if included multiple times
assign string No n/a The name of the variable that the output of include_php will be assigned to

可选的:

Name Description
nocache Disables caching of inluded PHP script

 

13.{ldelim} {rdelim}

进行javascript 以及css 文件的添加:可以使用 {literal}{/literal}

简单例子:

 14.{section},{sectionelse} 也是类似的进行循环操作的标签:
Attribute Name Type Required Default Description
name string Yes n/a The name of the section
loop mixed Yes n/a Value to determine the number of loop iterations
start integer No 0 The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.
step integer No 1 The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.
max integer No n/a Sets the maximum number of times the section will loop.
show boolean No TRUE Determines whether or not to show this section

 例子:

'John Smith', 'home' => '555-555-5555', 'cell' => '666-555-5555', 'email' => 'john@myexample.com'), array('name' => 'Jack Jones', 'home' => '777-555-5555', 'cell' => '888-555-5555', 'email' => 'jack@myexample.com'), array('name' => 'Jane Munson', 'home' => '000-555-5555', 'cell' => '123456', 'email' => 'jane@myexample.com') );$smarty->assign('contacts',$data);?> 模板文件:{section name=customer loop=$contacts}

name: {

$contacts[customer].name}
home: {
$contacts[customer].home}
cell: {
$contacts[customer].cell}
e-mail: {
$contacts[customer].email}

{
/section} 输出:

name: John Smith

home: 555-555-5555
cell: 666-555-5555
e-mail: john@myexample.com

name: Jack Jones

home phone: 777-555-5555
cell phone: 888-555-5555
e-mail: jack@myexample.com

name: Jane Munson

home phone: 000-555-5555
cell phone: 123456
e-mail: jane@myexample.com

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://xcsvo.baihongyu.com/

你可能感兴趣的文章
实现单滑页效果
查看>>
对象池
查看>>
unity编辑器扩展
查看>>
Json解析
查看>>
责任链模式
查看>>
进程线程
查看>>
生命周期
查看>>
枚举器和迭代器以及unity协程
查看>>
序列化
查看>>
IP和端口号
查看>>
UI框架
查看>>
扩展方法
查看>>
async/await
查看>>
Socket通信
查看>>
粘包和分包
查看>>
C#连接、访问MySQL数据库
查看>>
动画系统
查看>>
Quaternion
查看>>
01定时回调
查看>>
02定时回调增加任务循环功能
查看>>