跳到内容

输出过滤器

当模板被渲染之后,它的输出可以通过一个或多个输出过滤器发送。

注意 这不同于 prefilterspostfilters,因为 pre- 和 postfilters 在编译模板之前(保存在磁盘之前)对其进行操作,而输出过滤器在执行模板输出时对其进行操作。

Smarty 将模板输出作为第一个参数传递,并期望函数返回处理的结果。

输出过滤器可以作为 Extension 的一部分添加,也可以如下所示进行注册。

这将提供对垃圾邮件发送者的基本保护

<?php

function protect_email($tpl_output, \Smarty\Template\ $template)
{
    return preg_replace(
        '!(\S+)@([a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,3}|[0-9]{1,3}))!',
        '$1%40$2', 
        $tpl_output
    );
}

// register the outputfilter
$smarty->registerFilter("output", "protect_email");
$smarty->display("index.tpl');