Smarty 是什么?
为何使用它?
使用场景和工作流程
语法比较
模板继承
最佳实践
速成课程
模板后置过滤器是 PHP 函数,在 编译模板之后通过它们运行你的模板。后置过滤器可以 注册 也可以通过 loadFilter() 函数或通过设置 $autoload_filters 变量,在 插件目录 中加载。Smarty 将编译后的模板代码作为第一个参数传递,并期望该函数返回处理结果。
示例 17.12。使用模板后置过滤器
<?php
// put this in your application
function add_header_comment($tpl_source, Smarty_Internal_Template $template)
{
return "<?php echo \"<!-- Created by Smarty! -->\n\"; ?>\n".$tpl_source;
}
// register the postfilter
$smarty->registerFilter('post','add_header_comment');
$smarty->display('index.tpl');
?>
上述后置过滤器会让已编译的 Smarty 模板 index.tpl 类似于
<!-- Created by Smarty! -->
{* rest of template content... *}
请参阅 registerFilter()、前置过滤器、输出过滤器 和 loadFilter()。