注:在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 world
smarty
</body>
</html>
output:
hello world smarty