第一部分入口: smarty模板介绍和入门
第二部分入口: smarty模板内建函数的使用
——— 新内容开始——————————
五、自定义函数
1 、 assign
{assign var=[var] value=[value]}
创建一个模板变量
V ar:变量名
V alue:变量值
2、counter
{counter start=0 skip=2 print=false}
计数器
S tart:开始的值
S kip:步长
P rint:本次是否打印
{counter}<br>
{counter}<br>
3、cycle
<tr bgcolor=[{cycle values="#eeeeee,#d0d0d0"}]>
轮转功能
4、debug
{debug}
打开调试窗口 (观察所有模板变量)
5、eval
{eval var=#ErrorState# assign="state_error"}
计算变量的值
6、fetch
{fetch file=[file] assign=[var]}
F etch表示获取某个文件的内容
F ile:文件名
A ssign:将结果保存到指定的变量中
7、html_image
{html_image file="pumpkin.jpg"}
输出一个img
F ile:要输出的图像
8、html_table
{html_table loop=$data cols=4 table_attr='border="0"'}
用表格table显示数组的值
L oop:要循环的数组
C ols:每行显示多少个
T able_attr:表格属性
H tml_table用于显示简单数据(一维数组)
9、html_checkboxes
{html_checkboxes
values=$cust_ids
checked=$customer_id
output=$cust_names
separator="<br />"}
表示在页面中输出一组复选框
V alues:值的数组
C hecked:被选中项的值的数组
O utput:显示的文本的数组
S eparator:分隔符(每个复选框之间的分隔符)
一组复选框必须设置values和output参数
P hp代码:
$text= array ( '足球' , '电影' , '彩票' , '打麻将' , '美女' );
$smarty->assign( 'text' ,$text);
$value= array (1,2,3,4,5);
$smarty->assign( 'value' ,$value);
$check= array (2,5);
$smarty->assign( 'check' ,$check);
模板代码:
{html_checkboxes name='love' values=$value output=$text checked=$check separator=' '}
10、html_options
<select name=customer_id>
{html_options values=$cust_ids selected=$customer_id output=$cust_names}
</select>
在页面中输出一组下拉列表选项
V alues: (option values ) 值的数组
S elected:被选中项的值
O utput:显示的文本的数组
P hp代码:
$opText= array ( '北京' , '上海' , '美国' );
$smarty->assign( 'opText' ,$opText);
$opValue= array (1,2,3);
$smarty->assign( 'opValue' ,$opValue);
$smarty->assign( 'selected' ,2);
模板代码:
{html_options values=$opValue output=$opText selected=$selected}
11、html_radios
{html_radios values=$cust_ids checked=$customer_id output=$cust_names separator="<br />"}
在页面中输出一组单选按钮
V alues:值的数组
C hecked:被选中项的值
O utput:显示的文本的数组
S eparator:分隔符
P hp代码:
$raText= array ( '男' , '女' , '保密' );
$smarty->assign( 'raText' ,$raText);
$raValue= array (1,2,3);
$smarty->assign( 'raValue' ,$raValue);
$smarty->assign( 'checked' ,3);
模板代码:
{html_radios name='sex' values=$raValue output=$raText checked=$checked separator=' '}
六、smarty中使用配置文件
配置文件有利于设计者管理文件中的模板全局变量。最简单的例子就是模板色彩变量。一般情况下你如果想改变一个程序的外观色彩,你就必须通过去更改每一个文件的颜色变量。如果有这个配置文件的话,色彩变量就可以保存在一个地方,只要改变这个配置文件就可以实现你色彩的更新。
配置文件的格式:
[color]
bgColor = #000000
tableBgColor = #000000
fontBgColor = #00ff00
……
引用配置文件的格式:
{config load}
查看更多关于php-smarty模板使用教程(三) 自定义函数与配置文件的详细内容...