预处理/后处理 {/programmers/plugins/plugins-prefilters-postfilters/}
预处理和后处理插件在概念上非常相似;它们的区别在于执行的时间——更准确地说是它们的执行时间。
string
smarty_prefilter_
name
string
\$source
object
\$template
预处理用于在编译之前立即处理模板的源代码。预处理函数的第一个参数是模板源代码,可能已经被其他预处理修改过。插件应该返回修改后的源代码。注意,这个源代码没有被保存在任何地方,它只用于编译。
string
smarty_postfilter_
name
string
\$compiled
object
\$template
后处理用于在编译完成后,但在编译的模板被保存到文件系统之前,立即处理模板的编译输出(PHP 代码)。后处理函数的第一个参数是编译的模板代码,可能已经被其他后处理修改过。插件应该返回这段代码的修改版本。
<?php
/*
* Smarty 插件
* -------------------------------------------------------------
* 文件: prefilter.pre01.php
* 类型: prefilter
* 名称: pre01
* 目的: 将html标签转换为小写。
* -------------------------------------------------------------
*/
function smarty_prefilter_pre01($source, Smarty_Internal_Template $template)
{
return preg_replace('!<(\w+)[^>]+>!e', 'strtolower("$1")', $source);
}
?>
<?php
/*
* Smarty 插件
* -------------------------------------------------------------
* 文件: postfilter.post01.php
* 类型: postfilter
* 名称: post01
* 目的: 输出列出所有当前模板变量的代码。
* -------------------------------------------------------------
*/
function smarty_postfilter_post01($compiled, Smarty_Internal_Template $template)
{
$compiled = "<pre>\n<?php print_r(\$template->getTemplateVars()); ?>\n</pre>" . $compiled;
return $compiled;
}
?>