过滤器能够在模板进入smarty编译模板前设置模板的内容,好比注释,
这些可有可无的内容 咱们不须要它们,所以能够在进入模板以前将其过滤php
registerFilter('pre','');html
<?php require "./mySmarty.cla -->ss.php"; $smarty = new mySmarty(); //定义一个前置过滤器函数 function prevadd($tplName){ $reg ="/<!--.*-->/"; return preg_replace($reg,'',$tplName); } $smarty ->registerFilter("pre","prevadd");
registerFilter('post','');缓存
<?php //定义一个后置过滤器 function poster($tplname){ return "<---author:smarty zheng--->".$tplname; } $smarty ->registerFilter("post","poster");
registerFilter('output','');
函数
<?php //定义输出过滤器 function outmessage($tplname){ echo "13232<br/>"; return str_replace("过滤器工具","tool",$tplname); } $smarty ->caching = 1; $smarty ->registerFilter("output","outmessage"); $smarty ->assign('name','prvfileter'); $smarty->clearCompiledTemplate('filter.html');//清除编译目录下的编译文件或者指定条件的编译文件。 $smarty ->display("filter.html");
对于输出过滤器来讲,输出过滤器在整个smarty的执行流程过程当中.是在编译文件生成以后,因此输出过滤器的内容不会出如今view_c编译好的目录文件中,可是若是caching=1 那么输出过滤器的内容会保存到cache目录缓存目录中去工具
利用smarty的过滤功能能够为模板作统一的设置,能够将方法定义在父类控制器中。例如将全部的模板中的注释去除掉.
post