实现对smarty的功能扩展
Smarty.class.php文件中成员方法
内建函数:smarty自带 ,不能修改
自定义函数:可以自行修改
保存在plugins目录下
语法格式类似于html标记的写法
<{……}>
一、了解smarty自定义函数
例如:
1) smarty自定义函数assign
作用:用于在模板被执行时为模板变量赋值.
函数名称:assign
参数:var: 声明变量名称 字符串
Value: 给该变量赋值 字符串
<{assign var=”title” value=”this is title”}>
$tpl->assign(“title”,”this is title”);
1、Counter: 用于输出一个记数过程.
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
name | string | No | default | 计数器的名称 |
start | number | No | 1 | 记数器初始值 |
skip | number | No | 1 | 记数器间隔、步长 |
direction | string | No | up | 记数器方向,(增/减) |
| boolean | No | true | 是否输出值 |
assign | string | No | n/a | 输出值将被赋给模板变量的名称 |
<{counter start=0 skip=2 print=false}>
<{counter}><br>
<{counter}><br>
结果
2
4
2、Cycle :用于轮转使用一组值. 该特性使得在表格中交替输出颜色或轮转使用数组中的值变得很容易
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
name | string | No | default | 轮转的名称 |
values | mixed | Yes | N/A | 待轮转的值,可以是用逗号分隔的列表(请查看 delimiter 属性)或一个包含多值的数组. |
| boolean | No | true | 是否输出值 |
advance | boolean | No | true | 是否使用下一个值(为 false 时使用当前值) |
delimiter | string | No | , | 指出values 属性中使用的分隔符,默认是逗号. |
assign | string | No | n/a | 输出值将被赋给模板变量的名称 |
<{section name=rows loop=$data}>
<tr bgcolor="<{cycle values="red,blue"}>">
<td><{$data[rows]}></td>
</tr>
<{/section}>
<tr bgcolor="red">
<td>aaa</td>
</tr>
<tr bgcolor="blue">
<td>bbb</td>
</tr>
<tr bgcolor="red">
<td>ccc</td>
</tr>
结果:aaa bbb ccc
3、eval :按处理模板的方式计算取得变量的值. 该特性可用于在配置文件中的标签/变量中嵌入其它模板标签/变量.
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
var | mixed | Yes | n/a | 待求值的变量(或字符串) |
assign | string | No | n/a | 输出值将被赋给模板变量的名称 |
4、Fetch: 用于从本地文件系统、HTTP或FTP上取得文件并显示文件的内容. 如果文件名称以"http://"开头,将取得该网站页面并显示. 如果文件名称以"ftp://"开头,将从ftp服务器取得该文件并显示.
<{fetch file="http://www.baidu.com"}>
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
file | string | Yes | n/a | 待请求的文件,http或ftp方式. |
assign | string | No | n/a | 输出值将被赋给模板变量的名称 |
结果:
新 闻 网 页 贴 吧 知 道 MP3 图 片 视 频 地 图
窗体顶端
窗体底端
输入法
加入百度推广 | 搜索风云榜 | 关于百度 | About Baidu
©2011 Baidu 使用百度前必读 京ICP证030173号
5、自定义函数 html_checkboxes 根据给定的数据创建复选按钮组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 options 替代. 所有的输出与 XHTML 兼容
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
name | string | No | checkbox | 复选按钮组的名称 |
values | array | Yes, 或指定 options 属性 | n/a | 包含复选按钮组值的数组 |
output | array | Yes, 或指定 options 属性 | n/a | 包含复选按钮组显示值的数组 |
selected | string/array | No | empty | 已选定的元素或元素数组 |
options | associative array | Yes,或指定 values 属性 | n/a | 包含值和显示的关联数组 |
separator | string | No | empty | 分隔每个复选按钮的字符串 |
labels | boolean | No | true | 是否为每个复选按钮添加 <label> 标签 |
6、html_image: 产生一个图象的 HTML 标签. 如果没有提供高度和宽度值,将根据图象的实际大小自动取得.
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
file | string | Yes | n/a | 图象文件的名称或路径 |
border | string | No | 0 | 图象边框大小 |
height | string | No | actual image height | 显示图象高度 |
width | string | No | actual image width | 显示图象宽度 |
basedir | string | no | web server doc root | 图象文件位置的相对路径 |
alt | string | no | "" | 可选图象描述(鼠标指向图象或图象文件不存在时显示的字符串信息) |
href | string | no | n/a | 图象链接到的地址 |
<{html_image file="d.jpg"}>
结果:
7、html_options 根据给定的数据创建选项组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 options 替代.
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
values | array | Yes, unless using options attribute | n/a | 包含下拉列表各元素值的数组 |
output | array | Yes, unless using options attribute | n/a | 包含下拉列表各元素显示值的数组 |
selected | string/array | No | empty | 已选定的元素或元素数组 |
options | associative array | Yes, unless using values and output | n/a | 包含值和显示的关联数组 |
name | string | No | empty | 下拉菜单的名称 |
index.php:
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('cust_options', array(
1001 => 'Joe Schmoe',
1002 => 'Jack Smith',
1003 => 'Jane Johnson',
1004 => 'Charlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display('index.tpl');
index.tpl:
<select name=customer_id>
{html_options options=$cust_options selected=$customer_id}
</select>
OUTPUT: (both examples)
<select name=customer_id>
<option value="1000">Joe Schmoe</option>
<option value="1001" selected="selected">Jack Smith</option>
<option value="1002">Jane Johnson</option>
<option value="1003">Charlie Brown</option>
</select>
结果:
8、html_radios: 根据给定的数据创建单选按钮组. 该函数可以指定哪个元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 options 替代. 所有的输出与 XHTML 兼容.
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
name | string | No | radio | 单选按钮列表的名称 |
values | array | Yes, 或指定 options 属性 | n/a | 包含单选按钮值的数组 |
output | array | Yes, 或指定 options 属性 | n/a | 包含单选按钮显示值的数组 |
checked | string | No | empty | 已选定的元素 |
options | associative array | Yes, 或指定 values 属性 | n/a | 包含值和显示的关联数组 |
separator | string | No | empty | 分隔每个单选按钮的字符串 |
a.html
<{html_radios name="id" options=$cust_radios checked=$customer_id separator="<br />"}>
<input type="radio" name="id[]" value="1000">Joe Schmoe<br />
<input type="radio" name="id[]" value="1001" checked="checked"><br />
<input type="radio" name="id[]" value="1002">Jane Johnson<br />
<input type="radio" name="id[]" value="1003">Charlie Brown<br />
Index.php
require("Smarty.class.php");
$smarty = new Smary;
$smarty->assign('cust_options', array(
1001 => 'Joe Schmoe',
1002 => 'Jack Smith',
1003 => 'Jane Johnson',
1004 => 'Charlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display("a.html");
结果:
9、html_select_date: 用于创建日期下拉菜单. 它可以显示任意年月日.
<{html_select_date}>
<select name="Date_Month">>
<option value="1">january</option>
<option value="2">february</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12" selected>December</option>
</select>
结果:
10、html_select_time :用于创建时间下拉菜单. 它可以显示任意时分秒
<{html_select_time use_24_hours=true}>
<select name="Time_Hour">
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09" selected>09</option>
</select>
<select name="Time_Meridian">
<option value="am" selected>AM</option>
<option value="pm">PM</option>
</select>
结果:
11、html_table 将数组中的数据填充到 HTML 表格中. cols 属性决定表格有多少列. table_attr , tr_attr 和 td_attr 属性决定表格中 tr 和 td 标签的一些附加属性. 如果 tr_attr 和 td_attr 属性值为数组,将轮流使用其中的值. 如果指定了 trailpad 属性,将在表尾最后一行附加一些数据.
12、math :允许模板设计者在模板中进行数学表达式运算. 均表达式中可以使用任何数值类型的变量,结果在 math 标签的位置输出. 表达式中使用的变量被当做参数传递给函数,可以是模板变量或静态值. 目前可以使用的运算符有:+, -, /, *, abs, ceil, cos, exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, srans 和 tan .关于数学函数的详细信息,请查看 PHP 文档.
属性 | 类型 | 是否必须 | 缺省值 | 描述 |
equation | string | Yes | n/a | 待执行的表达式 |
format | string | No | n/a | 结果的格式(遵从sprintf函数) |
var | numeric | Yes | n/a | 表达式变量值 |
assign | string | No | n/a | 输出值将被赋给模板变量的名称 |
[var ...] | numeric | Yes | n/a | 表达式变量值 |
<{math equation=”(x+y)/z” x=2 y=10 z=2}>
结果:6
13、popup 用于创建javascript弹出窗口
14、textformat 用于格式化文本. 该函数主要清理空格和特殊字符,对段落按单词边界换行和行缩进等段落格式化处理.
<{textformat wrap=40}>
This is foo.
This is foo.
This is foo.
This is foo.
This is foo.
This is foo.
This is bar.
bar foo bar foo foo.
bar foo bar foo foo.
bar foo bar foo foo.
bar foo bar foo foo.
bar foo bar foo foo.
bar foo bar foo foo.
bar foo bar foo foo.
<{/textformat}>
结果:
This is foo. This is foo. This is foo. This is foo. This is foo. This is foo. This is bar. bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo foo. bar foo bar foo foo.