插件扩展(自定义的变量修饰器):如果smarty 自带函数 以及 php 函数都不能满足我们的需求了,则可以自定义变量修饰器函数(对后端传来的数据做进一步处理)。
自定义的变量修饰器,和Smarty自带的变量修饰器的使用方法一样。
smarty/libs/plugins/modifier.mytool.php(自定义的变量修饰器,以modifier开头的都是修饰器):
<?php
//自定义变量修饰器,本质上就是一个PHP函数,函数名以"smarty_modifier_"开头。第一个参数$string就是要处理的文本数据。
function smarty_modifier_mytool($string,$param){
//处理的过程
$string .= $param;
//返回处理之后的结果
return $string;
}
plugin.php(后端):
<?php
include "libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->template_dir = "templates";
$smarty->compile_dir = "templates_c";
$smarty->assign('str','要处理的后端文本内容');
$smarty->display('plugin.tpl');
plugin.tpl(前端视图):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{$str|mytool:'---这是自己的定义的变量修饰器,测试看看'} {* 和Smarty自带的变量修饰器的使用相同。 要处理的后端文本数据|变量修饰器名:参数1:参数2 *}
</body>
</html>