Smarty函数
内建函数:smarty自带,不能修改
自定义函数:可以自行修改
保存在plugins目录下
语法格式类似于html标记写法
例如:
1) smarty自定义函数assign
作用于
函数名称:assign
参数:var :声明变量名称 字符串
Value:给该变量赋值 字符串
<{assign var=”title” value=”this is title”}>
$tpl->assign(“title”,”this is title”);
练习:使用自定义函数分配变量content,对应值abcd
<input type=”text” name=”input1…”>
属性无顺序要求
一、了解自定义函数例如:
<{config_load file=”*.conf”session=”one”}>
2) smarty方法assign
二、实现自定义函数
例如:
在a.html中写这句话
<{title num=”10” content=”aaaa” size=”10” color=}>
B.php
第一步:自定义函数
Function fun1(){}
第二步:注册自定义函数
$sql->register_function(*smarty中自定义函数名)
3.*版本不支持
2) 注册块
$tpl->register_block(“模板文件中的块名”,”php中的函数名”)
<hr />
<br />
<a> </a>块
a.html
<{h num=10 content=”aaa”}>
<{/h}>
3) 在plugins目录下新建文件
新建函数文件 world
例如:
function.assign_debug_info.php
function smarty_function_***(){}
步骤
第一步:plugins下新建文件function.world.php
第二步:打开文件
Function smarty_function_world($args,&$smarty){..}
新建块文件hello
block.hello.php
function smarty_block_hello(){}
第一步:plugins下新建块文件block.hello.php
第二步:打开文件
Function smarty_block_hello($args,$content,&$smarty){..}
1、<{counter start=0 skip=2 print=false}><br>
<{counter}><br>
<{counter}><br>
<{counter}><br>
<{counter}><br>
结果:
2
4
6
8
2、<{textformat wrap=40}><br>
This is foo.<br>
<{/textformat}><br>
结果:This is foo.
3、<{* create a link with a popup window when you move your mouse over *}><br>
<A href="b.php" {popup text="This link takes you to my page!"}>b</A><br>
结果:b 点击"b"之后跳转到smarty_fun下的b.php文件,运行b.php
4、<{* popup_init must be called once at the top of the page *}>
<{popup_init src="/javascripts/overlib.js"}>
结果:调用/javascripts/overlib.js文件,显示结果
5、<{mailto address="me@domain.com" text="send me some mail"}>
结果:出现一个send me some mail按钮,点击之后启动电子邮件
6、<{* $height=4, $width=5 *}>
<{math equation="x + y" x=$height y=$width}>
结果:9
7、<{html_table loop=$data}>
<{html_table loop=$data cols=4 table_attr='border="0"'}>
<{html_table loop=$data cols=4 tr_attr=$tr}>
结果:
1 2 3
4 5 6
7 8 9
这组数字以表格的形式显示
1 2 3 4
5 6 7 8
9
此组没有表格
1 2 3 4
5 6 7 8
9
这组也是以表格的形式显示
8、<{html_select_time display_hours=true}><br>
结果:显示小时
9、{html_select_date}
结果:显示日期
10、{html_radios values=$cust_ids checked=$customer_id output=$cust_names 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 />
11、<select name=customer_id>
{html_options values=$cust_ids selected=$customer_id output=$cust_names}
</select>
结果:<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>出现一个按钮
12、<{html_image file="3.jpg"}><br>
结果:显示图片3.jpg
13、<{html_checkboxes values=$cust_ids checked=$customer_id output=$cust_names separator="<br />"}>
结果:<label><input type="checkbox" name="checkbox[]" value="1000" />Joe Schmoe</label><br />
<label><input type="checkbox" name="checkbox[]" value="1001" checked="checked" />Jack Smith</label><br />
<label><input type="checkbox" name="checkbox[]" value="1002" />Jane Johnson</label><br />
<label><input type="checkbox" name="checkbox[]" value="1003" />Charlie Brown</label><br />选择姓名
14、<{section name=rows loop=$data}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
<td>{$data[rows]}</td>
</tr>
{/section}
>
结果:{$data[rows]} {$data[rows]} {$data[rows]} {$data[rows]} {$data[rows]} {$data[rows]} {$data[rows]} {$data[rows]} {$data[rows]}