Smarty----预过滤器,后过滤器,输出滤镜

来源:互联网 发布:mysql设置root密码 编辑:程序博客网 时间:2024/06/05 00:57

注:在smarty lib 3.0下

require_once "libs/smarty/Smarty.class.php";$smarty = new Smarty();$smarty->template_dir=$_SERVER['DOCUMENT_ROOT'].'/smarty_files/templates';$smarty->compile_dir=$_SERVER['DOCUMENT_ROOT'].'/smarty_files/templates_c';$smarty->left_delimiter='<{';$smarty->right_delimiter='}>';//预过虑器function remove_dw_comments($tpl_source,&$smarty){    //$tpl_source 是系统自带一个变量    return preg_replace("/<!--#.*-->/U", "", $tpl_source);}$smarty->register_prefilter("remove_dw_comments");//后过虑器function add_header_comment($tpl_source,&$smarty){    return "<?php echo \"<!-- Create by Smarty! -->\n\ ?>\n".$tpl_source;}$smarty->register_postfilter("add_header_comment");//输出滤镜function light($output,&$smarty){    return str_replace('smarty', "<font color=blue>smarty</font>", $output);}$smarty->register_outputfilter('light');$smarty->display('filter.html');

filter.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Caech</title></head><body><body><!--# 这是一段注释内容,在编译后的文件不会出现. -->hello worldsmarty</body></html>

output:

hello world smarty


0 0