Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

输出过滤器

当通过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.tpl");

// 现在,所有显示的电子邮件地址都会经过简单保护,以防止垃圾邮件爬虫
?>

    

参见 registerFilter(), loadFilter(), $autoload_filters, 后置过滤器$plugins_dir.

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors