今天学了Smarty自定义函数,测试的例子如下:
php文件:
<?php
include("./init.inc.php");
$tpl->assign("title","abc");
//注册自定义函数
$tpl->register_function("title","fun1");
//因为参数出现顺序无关,把参数保存到数组
function fun1($args){
$a="";
$b=$args["num"]."<br>";
for($i=0;$i<$b;$i++){
$a.="<font size=".$args["size"]." color=".$args["color"].">".$args["content"]."</font><br><br>";
}
return $a;
}
//注册一个块
$tpl->register_block("hello","fun2");
//定义块函数
function fun2($args,$content){
$a="";
$b=$args["num"]."<br>";
for($i=0;$i<$b;$i++){
$a.="<font size=".$args["size"]." color=".$args["color"].">".$content."</font><br><br>";
}
return $a;
}
$tpl->display("a.html");
?>
模板文件:a.html
<{$title}><br />
<{assign var="name" value="zhangsan"}>
The value of the $name is <{$name}><br />
<{assign var="sex" value="man"}>
The value of the $sex is <{$sex}><br />
模仿assign实现自定义函数:title<br />
<{title num="5" content="aaa" size="2" color="red"}>
<{title num="5" content="qqq" size="7" color="green"}>
自定义块:hello<br />
<font size="5">font</font><br />
<{hello num="5" size="3" color="blue"}>
1111111111
<{/hello}>