smarty学习——内建函数(部分接上)
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}
如下例子:
简单例子:
<?php
$arr = array('red', 'green', 'blue');
$smarty->assign('myColors', $arr);
?>模板文件如下: <ul>
{foreach $myColors as $color}
<li>{$color}</li>
{/foreach}
</ul> 输出结果: <ul>
<li>red</li>
<li>green</li>
<li>blue</li>
</ul>
处理键值:
<?php
$people = array('fname' => 'John', 'lname' => 'Doe', 'email' => 'j.doe@example.com');
$smarty->assign('myPeople', $people);
?> 键值模板: <ul>
{foreach $myPeople as $value}
<li>{$value@key}: {$value}</li>
{/foreach}
</ul> 输出: <ul>
<li>fname: John</li>
<li>lname: Doe</li>
<li>email: j.doe@example.com</li>
</ul>
foreach 的嵌套:
<?php
$smarty->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 的使用:
<table>
{foreach $items as $i}
{if $i@index eq 3}
{* put empty table row *}
<tr><td>nbsp;</td></tr>
{/if}
<tr><td>{$i.label}</td></tr>
{/foreach}
</table>
{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}
简单例子:
<script language="JavaScript">
function foo() {ldelim}
... code ...
{rdelim}
</script> 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 |
例子:
<?php
$data = array(
array('name' => '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}
<p>
name: {$contacts[customer].name}<br />
home: {$contacts[customer].home}<br />
cell: {$contacts[customer].cell}<br />
e-mail: {$contacts[customer].email}
</p>
{/section} 输出: <p>
name: John Smith<br />
home: 555-555-5555<br />
cell: 666-555-5555<br />
e-mail: john@myexample.com
</p>
<p>
name: Jack Jones<br />
home phone: 777-555-5555<br />
cell phone: 888-555-5555<br />
e-mail: jack@myexample.com
</p>
<p>
name: Jane Munson<br />
home phone: 000-555-5555<br />
cell phone: 123456<br />
e-mail: jane@myexample.com
</p>
smarty学习——内建函数(部分接上)的更多相关文章
- smarty学习——内建函数 部分
Smarty自带一些内建函数. 内建函数是模板语言的一部分. 用户不能创建名称和内建函数一样的自定义函数,也不能修改内建函数. 一.包含的内建函数 {$var=...}{append}{assign} ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- Smarty学习笔记(一)
1.Smarty的配置: 将lib的内容复制到自己的工程,然后引入 实例化和配置Smarty基本属性: $smarty = new Smarty(); $smarty->left_delimit ...
- 移动端头部固定,上划逐渐透明 (vue)
移动端头部固定,上划逐渐透明 <template> <div> <router-link tag="div" to="/" cla ...
- Git学习系列之Windows上安装Git详细步骤(图文详解)
前言 最初,Git是用于Linux下的内核代码管理.因为其非常好用,目前,已经被成功移植到Mac和Windows操作系统下. 鉴于大部分使用者使用的是Windows操作系统,故,这里详细讲解Windo ...
- Git学习系列之Windows上安装Git之后的一些配置(图文详解)
不多说,直接上干货! 前面博客 Git学习系列之Windows上安装Git详细步骤(图文详解) 第一次使用Git时,需要对Git进行一些配置,以方便使用Git. 不过,这种配置工作只需要进行一次便可, ...
- Django:学习笔记(8)——文件上传
Django:学习笔记(8)——文件上传 文件上传前端处理 本模块使用到的前端Ajax库为Axio,其地址为GitHub官网. 关于文件上传 上传文件就是把客户端的文件发送给服务器端. 在常见情况(不 ...
- TortoiseGit学习系列之Windows上本地代码如何通过TortoiserGit提交到GitHub详解(图文)
不多说,直接上干货! 前面博客 TortoiseGit学习系列之Windows上TortoiseGit的安装详解(图文) 上面博文给大家讲解了一下如何本地安装TortoiseGit. 这篇为大家讲一下 ...
- 2017-2018-1 20155232 《信息安全系统设计基础》第四周学习总结以及课上myod练习补充博客
2017-2018-1 20155232 <信息安全系统设计基础>第四周学习总结以及课上myod练习补充博客 课上myod练习 1 参考教材第十章内容 2 用Linux IO相关系统调用编 ...
随机推荐
- hdoj2476 String painter
题意:有一刷子,能将区间内涂成同一字母.给出src,dst串,问最少涂几次? 用dp[i][j]表示区间[i,j]内最少涂的次数.len=1,2时很明显.len=3时,dp[i][j]要么就在dp[i ...
- android-------高德地图两点路线和多个点路线绘制
最近朋友需要两点路线和多个点路线绘制这个功能,帮忙弄了一下,写这篇博客与大家分享一下. 两点路线 是起点和终点两个经纬度点,高德绘制出路线,可以实现实线和虚线功能 效果图: 相关属性: mPol ...
- Population Size CodeForces - 416D (贪心,模拟)
大意: 给定$n$元素序列$a$, 求将$a$划分为连续的等差数列, 且划分数尽量小. $a$中的$-1$表示可以替换为任意正整数, 等差数列中必须也都是正整数. 贪心策略就是从前到后尽量添进一个等差 ...
- Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)
大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...
- Android将view保存为图片并放在相册中
在Android中,可以将view保存为图片并放在相册中,步骤为 view->bitmap->file,即先将view转化为bitmap,再将bitmap保存到相册中. 需要将红框标注的v ...
- OAF中trunc函数的使用(转)
原文地址:OAF中trunc函数的使用 需求:在做OAF开发时,经常会需要查询功能,由于需求的不同,往往不能使用OAF标准的查询功能,需要自己客户化实现查询功能,而在查询功能中,经常会遇到查询的时间范 ...
- POJ 3481 treap
这是利用treap写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了. 相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生 ...
- 在 Ubuntu 14.04 Chrome中安装Flash Player(转)
在 Ubuntu 14.04 中安装 Pepper Flash Player For Chromium 一个 Pepper Flash Player For Chromium 的安装器已经被 Ubu ...
- MFC界面美化
http://www.360doc.com/content/11/0222/15/5404234_95120736.shtml http://www.360doc.com/content/11/010 ...
- kbmMW CopyRawRecords 用法
复制一个ClientQuery数据集到另外一个ClientQuery,我们应该怎么做?并注意什么呢? kbmMW为我们提供了好几个方法,有LoadFromDataSet,CopyRawRecords, ...