后过滤器

    模板后过滤器是在模板编译后前运行的PHP函数。后过滤器可以被注册,或者从插件目录中用load_filter()函数调入,或者设置$autoload_filters变量而调用。Smarty将传递编译后的模板代码作为第一个参数,并期望函数返回处理后的结果。

    例子15-3. 使用模板后过滤器

<?php
// 将其放置在你的应用中
function add_header_comment($tpl_source, &$smarty)
{
    return 
"<?php echo \"<!-- Created by Smarty! -->\n\"; ?>\n".$tpl_source;
}

// 注册后过滤器
$smarty->register_postfilter('add_header_comment');
$smarty->display('index.tpl');
?>

    上面的后过滤器将使编译过的Smarty模板index.tpl看起来为:

<!-- Created by Smarty! -->
{* rest of template content... *}

    参见register_postfilter()前过滤器以及load_filter()