跳至内容

预过滤器

模版预处理器是 PHP 函数,你的模版在编译前都要经过它们处理。这样可以用于对你的模版进行预处理以删除不需要的注释、监控人们在它们的模版中放入的内容等。

Smarty 将把模版源代码作为第一个参数传递,并期望函数返回生成的模版源代码。

预处理器可以作为一个扩展的一部分添加,或者按照下面所示进行注册。

这将删除模版源中的所有 HTML 注释

<?php

function remove_dw_comments($tpl_source, \Smarty\Template\ $template)
{
    return preg_replace("/<!--#.*-->/U",'',$tpl_source);
}

// register the prefilter
$smarty->registerFilter('pre', 'remove_dw_comments');
$smarty->display('index.tpl');