PHP头条
热点:

php-smarty模板使用教程(四)-程序员篇


Smarty程序设计篇

模板设计-à前台人员;程序设计-àphp开发人员 一、 Smarty模板常量 SMARTY_DIR  Smarty框架的路径 代码:$smarty=new Smarty(); //在这里,我们输出了smarty框架的路径   echo SMARTY_DIR; 二、Smarty模板 变量 l $template_dir :模板路径  l $compile_dir :编译路径 l $config_dir :配置文件路径 l $cache_dir :缓存文件 以上四个属性在2.6是公有的,在3.0里有私有的,建议大家使用相应的set和get方法进行设置和读取 l $left_delimiter :左分隔 l $right_delimiter :右分隔符 以上二个属性是公有的,可以直接访问 这们的默认值是 “{“   “}” 为了不和css和js起冲突,一般情况下,程序员会将这两个属性的值设置为以下几种 <{$name}> {*$name*} l $caching :是否开启缓存  l $cache_lifetime :默认的缓存时间 如果想在smarty中使用缓存,都必须将当前caching的属性设置为true Caching在3.0中是公有的,可以直接设置,它的值。默认是false,表示不开启缓存功能 默认的缓存时间的单位是秒,值是3600(一小时) l $debugging 开启缓试窗口 在smarty中,开启调试共有2种方法: 1)在模板中使用   {debug} 2)在程序中使用  $smarty->debugging=true {debug}可以观察模板中所有的变量,包括模板创建的变量 $debugging属性打开的调试只能观察php文件分配的变量 l $php_handling 在smarty2.6中,如果想在模板中使用{php}{/php}这个函数,必须设置该属性的值为true,在3.0中,可以不设置 问题: 在smarty中,编译目录和缓存目录是不是自动生成的?答:是!!! Tip:不过不要过渡依赖这个自动生成,应该尽量手动创建! 如果模板的内容发生改变,smarty会重新生成缓存文件  
  1. //要开启缓存,设置为true即可 
  2.     $smarty->caching=true; 
  3.      
  4.     //这个缓存时间的设置一般取决于网站的流量,越大,时间越小,越小,时间越大 
  5.     $smarty->cache_lifetime=3600; 
  6.      
  7.     $smarty->assign('name','lisi'); 
  8.      
  9.     //开启smarty的调试窗口 
  10.     $smarty->debugging=true; 
  11.      
  12.     $smarty->display('demo01.htpl'); 
三、 Smarty模板方法 l assign :为模板变量赋值 (传值) l assignByRef :为模板变量赋值 (传地址) l append :将变量或值追加至数组 l appendByRef :将变量以地址形式追加至数组 append认为,数据是可变的,是不固定的 l clearAllAssign :清除所有已赋值的模板变量 l clearAssign :清除指定的模板变量 l clearCache :清除缓存 l configLoad :加载配置文件 在smarty中,如果想加载一个配置文件,有两种方式: 1) 在模板中  {config_load file=’file’} 2) 在程序中  $smarty->configLoad(配置文件名,配置节); l clearConfig :清除某个配置文件的变量 l display :显示某模板 1) 读取模板内容 2) 替换字符串 3) 输出 l fetch :返回替换之后的字符串 1) 读取模板内容 2) 替换字符串 3) 返回字符串(返回给php程序) l templateExists :判断模板是否存在 在smarty中,如果使用了一个不存在的模板则会出错,所以在显示一个模板之前应 该对这个模板进行判断,判断其是否存在 下面是代码部分:  
													
  1. //assignByRef 为模板变量赋 变量地址 
  2.     $name='xiaogang'; 
  3.     //$smt->assign('name',$name); 
  4.     $smt->assignByRef('name',$name); 
  5.     $name="lixiaolong"; 
  6.     $smt->display('demo2.html'); 
  7.  
 
																					
  1. /*append的用法、appendByRef的用法*/ 
  2.     $c1='美国'; 
  3.     $smarty->appendByRef('city',$c1);   //将c1变量以地址形式传给city 
  4.     $smarty->append('city','北京');   //将北京追加模板变量数组city中 
  5.     $smarty->append('city','上海');   //将上海追加模板变量数组city中 
  6.     $c1='山东'; 
  7.      
  8.     //这种写法相当于 
  9.       // $smarty->assign('city',array('北京','上海')); 
 
	
																																
  1. //将所有之前模板变量的值清空 
  2.     //$smarty->clearAllAssign(); 
  3.      
  4.     //将模板变量name的值清空 
  5.     //$smarty->clearAssign('name'); 
  6.      
  7.     //$smarty->caching=true; 
  8.     //表示清除demo02.htpl这个模板文件的缓存文件 
  9.     //$smarty->clearCache('demo02.htpl'); 
  10.      
  11.     //加载demo02.conf配置文件 
  12.     //$smarty->configLoad('demo02.conf'); 
  13.     //表示将配置文件中的fgcolor的值清除 
  14.     //$smarty->clearConfig('fgcolor'); 
 
																																																
  1. //fetch方法一般用于生成静态页 
  2. //  $str=$smarty->fetch('demo03.htpl'); 
  3. //  $filename='html/'.date("YmdHis").'.html'; 
  4. //  file_put_contents($filename, $str); 
  5.      
  6.     //显示某个模板 
  7.     ///$smarty->display('demo03.htpl'); 
  8.      
  9.      
  10.     //调用了一个不存在的模板,程序出错 
  11.     //如果模板存在,返回值为true,反之为false 
  12.     if($smarty->templateExists('demo04.htpl')) 
  13.         $smarty->display('demo04.htpl'); 
  14.     else 
  15.         echo '模板不存在!'; 
  16.      
Assign和assignByRef的区别:   phpstar   四、 对象应用 l 注册对象到模板,然后通过类似于用户自定义函数的形式来访问它。 l 给模板分配对象,然后通过访问其它赋值变量类似的方法进行访问  
																																																																	
  1. class Person 
  2.     { 
  3.         public $name; 
  4.         public $age; 
  5.          
  6.         public function display() 
  7.         { 
  8.             echo '你好,我的名字是'.$this->name.'年龄是'.$this->age.'
    '; 
  9.         } 
  10.     } 
  11.     $per=new Person(); 
  12.     $per->name='zhangsan'; 
  13.     $per->age='30'; 
  14.  
  15.     include('../Smarty/Smarty.class.php'); 
  16.      
  17.     $smarty=new Smarty(); 
  18.     $smarty->assign('per',$per);    //为模板分配了一个对象per 
  19.     $smarty->display('demo04.htpl'); 
    五、 过滤器 就是用来过滤数据的 1、过滤器的种类 Prefilters预过滤器 Postfilters后过滤器 Output Filters输出过滤器(输出滤镜) 2、过滤器的流程 l tpl源文件 =〉Prefilter =〉编译tpl文件 => Postfilter =>保存到磁盘=> 编译过的php文件执行=〉Output Filters(=〉如果有smarty cache的话,Output Filters的内容会缓存) =>结果输出。 读取tpl模板文件之后,对它进行编译之前,触发的是预过滤器 编译之后,保存之前,触发的是后过滤器 保存之后,输出之前,触发的是输出过滤器 3、使用过滤器 在smarty中,使用过滤器必须要先注册 在smarty2.6中,使用过滤器通过以下形式: l Prefilters注册预过滤器 $smarty->register_prefilter(“func”);  l Postfilters 注册后过滤器 $smarty->register_postfilter(“func”);  l Output Filters 注册输出过滤器 $smarty->register_outputfilter(“func”); 在smarty3.0中,使用以下形式注册: $smarty->registerFilter($type, $callback) Type:过滤器类型   取值范围:pre、post、output Callback:回调函数 (当触发过滤器时执行的函数) 4、开始使用  
																																																																																					
  1. //display1是预过滤器的回调函数 
  2.     function display1($tpl) //tpl表示模板内容 
  3.     { 
  4.         echo '这是预过滤器!
    '; 
  5.         //$tpl=str_replace('#title#', '家教网', $tpl); 
  6.         return $tpl; 
  7.     } 
  8.     function display2($tpl) 
  9.     { 
  10.         echo '这是后过滤器!
    '; 
  11.         //$tpl=''.$tpl; 
  12.         return $tpl; 
  13.     } 
  14.     function display3($tpl) 
  15.     { 
  16.         echo '这是输出过滤器!
    '; 
  17. $tpl=str_replace('smarty', 'smarty', $tpl); 
  18.         return $tpl; 
  19.     } 
  20.     include('../Smarty/Smarty.class.php'); 
  21.     $smarty=new Smarty(); 
  22.     //注册预过滤器 
  23.     $smarty->registerFilter('pre', 'display1');  
  24.     //注册后过滤器 
  25.     $smarty->registerFilter('post', 'display2');     
  26.     //注册输出过滤器 
  27.     $smarty->registerFilter('output', 'display3'); 
  28.     $smarty->display('demo05.htpl'); 
  预过滤器和后过滤器只要模板没有改变,只能执行一次 输出过滤器可以每次执行

www.phpzy.comtrue/phpkj/8612.htmlTechArticlephp-smarty模板使用教程(四)-程序员篇 Smarty程序设计篇 模板设计-à前台人员;程序设计-àphp开发人员一、Smarty模板常量SMARTY_DIR Smarty框架的路径代码:$smarty=newSmarty();//在这里,我们输出了...

相关文章

相关频道:

PHP之友评论

今天推荐