定义调解器的文件必须放置在 Smarty 中,具体路径是:libs/plugins/。其文件名称,必须按照 Smarty 的格式 modifier.调解器名.php
下面通过一个实例演示 Smarty 中,自定义变量调解器的使用
程序思路:做两个变量调解器,功能是:一个转换文本;一个截取文本。
init.inc.php(Smarty初始化文件)
template_dir = ROOT_PATH.'/tpl/'; //设置模板文件目录
$_tpl->compile_dir = ROOT_PATH.'./com/'; //设置编译文件目录
$_tpl->left_delimiter = '<{'; //设置左定界符
$_tpl->right_delimiter = '}>'; //设置右定界符
?>
index.php(主文件)
assign('str',$_str); //字符串赋值给str
$_tpl->assign('str1',strtolower($_str)); //字符串全部转换为小写赋给str1
$_tpl->assign('str2',strtoupper($_str)); //字符串全部转换为大写赋给str2
$_tpl->assign('str3',ucfirst($_str)); //字符串首字母转换为大写赋给str3
$_tpl->assign('str4',substr($_str,15).'...'); //截取字符串前15个字符,后面的用'...'代替,并赋给str4
$_tpl->assign('str5',strtoupper(substr($_str,15)).'...'); //截取字符串前15个字符转换为大写,后面的用'...'代替,并赋给str4
$_tpl->display('index.tpl'); //引入模板
?>
tpl/index.tpl
Smarty 中的变量调解器
<{$str}>
<{$str1}>
<{$str2}>
<{$str3}>
<{$str4}>
<{$str5}>
<{$str|transform}>
<{$str|transform:"lower"}>
<{$str|transform:"upper"}>
<{$str|transform:"firstdx"}>
<{$str|subString:0:15:"###"}>
<{$str|subString:0:15:"@@@"|transform:"upper"}>
<{$str|transform:"upper"|subString:0:15:"@@@"}>