实现对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”);
练习:
使用自定义函数分配变量 content,对应值为abc;
例:
<?php
include("init.inc.php");
$tpl->assign("title","abc");
//注册自定义函数
$tpl->register_function("title","fun1");
//因为参数出现书序无关,把参数保存到数组
function fun1($args){
$a="";
for($i=0;$i<$args["num"];$i++){
$a.="<font size=".$args["size"].">".$args["content"]."</font><br>";
}
return $a;
}
//注册一个块
$tpl->register_block("hello","fun2");
//定义块函数
function fun2($args,$content)
{
$a="";
for($i=0;$i<$args["num"];$i++){
$a.="<font size=".$args["size"].">".$content."</font><br>";
}
return $a;
}
$tpl->display("a.html");
?>
注:
<input type=”text” name=”input1”>
属性无顺序要求
<{config_load file=”*.conf” section=”one”}>
2) smarty方法assign
二、 实现自定义函数
1)例如:
a.html
<{title num=”10” content=”aaaa” size=”10” color=”red” }>
b.php
第一步:自定义函数
function fun1(){}
第二步:注册自定义函数
$tpl->register_function(“模板文件中的函数名”,”php中的函数名”)
Plugins/function_自定义函数名_*
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){..}