当通过display()或
fetch()来调用模板的时候,
输出的内容将会经过一个或者多个输出过滤器来进行处理。
对比
后置过滤器,
后置过滤器是在模板编译后但没有被存储到编译文件前调用,
输出过滤器是模板执行后准备输出时调用。
输出过滤器可以通过注册过滤器来调用,
或者放置到插件目录中,用
loadFilter()函数或者
设置
$autoload_filters来调用。
Smarty把准备输出的内容作为第一个参数传递到函数中,并期待函数返回经过处理的内容。
Example 17.13. 使用输出过滤器
<?php
// 在PHP程序中
function protect_email($tpl_output, Smarty_Internal_Template $template)
{
$tpl_output =
preg_replace('!(\S+)@([a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,3}|[0-9]{1,3}))!',
'$1%40$2', $tpl_output);
return $tpl_output;
}
// 注册过滤器
$smarty->registerFilter("output","protect_email");
$smarty->display("index.html");
// 现在,所有显示的电子邮件地址都会经过简单保护,以防止垃圾邮件爬虫
?>
参见
registerFilter(),
loadFilter(),
$autoload_filters,
后置过滤器 和
$plugins_dir.