PHP-Smarty模板引擎-翻译-手册-第15章高级特性-输出过滤器 Output Filters 输出过滤器
When the template is invoked via display()or fetch(), its output can be sent through one or more output filters. This differs frompostfiltersbecause postfilters operate on compiled templates before they are saved to the disk, whereas output filters operate on thetemplate output when it is executed.
通过display()或fetch()执行模板时,输出可以被发送到一个或多个输出过滤器,与postfilters不同,postfilters在已编译的模板保存在硬盘前处理,而输出过滤器在模板执行后处理输出。
Output filterscan be either registeredor loaded from the plugins directoryby using the load_filter()method or by setting the $autoload_filtersvariable. Smarty will pass the template output as the first argument, and expect the function to return the result of the processing.
输出过滤器可以通过注册,也可以使用load_filter()方法或设置$autoload_filters变量从pluginsdirectory中加载。Smarty将把模板输出传递为第一个参数,然后获取函数处理后的结果。
Example 15-4. Using a template outputfilter 例15-4. 使用模板输出过滤器
<?php
// put this inyour application
// 把下列代码放入你的应用程序中
functionprotect_email($tpl_output, &$smarty)
{
$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;
}
// register the outputfilter
// 注册输出过滤器
$smarty->register_outputfilter('protect_email');
$smarty->display('index.tpl');
// now any occurrence of an email address in the template output will have
// a simple protection against spambots
// 现在模板输出中出现的任何电子邮件地址都拥有针对垃圾邮件机器人的简单保护
?>