認識smarty

smarty就是一個類php

p_w_picpath

p_w_picpath

p_w_picpath

例1:自定義smarty搜索引擎,認識smartyhtml

p_w_picpath

p_w_picpath

p_w_picpath

這段正則表達式:\s*表空格,{}須要轉義  ([a-zA-Z_][a-zA-Z0-9_]*)  以字母或者下劃線開始,後面能夠是字母數字下劃線,*0或多個字符正則表達式

生成的組合文件:數據庫

p_w_picpath

顯示的結果:ide

p_w_picpath

源代碼:以下ui

sm.htmlthis

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
<html>    
<head>    
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
<title>{$title}</title>    
</head>    
<body>    
<div> {$xx}</div>    
<div> {$xx}</div>    
  <div> {$title}</div>    
  <div> {$xx}</div>    
<div> {$xx}</div>    
  <div> {$xx}</div>搜索引擎

</body>  
</html>3d

sm.phphtm

<?php  
include "smarty.class.php";

$sm = new smarty();

//例如:鏈接數據庫,獲取如下內容;  
$tt = " this is article title";    
$xinxi = "this is the article content";

$sm ->assign("title",$tt);  
$sm ->assign("xx",$xinxi);    
$sm ->display("sm.html");    
//var_dump($sm);

smarty.class.php

<?php   class  smarty{        private $vars=array();        function assign($key,$value)        {            $this->vars[$key] = $value;        }        function display($tplfile)        {             $tfile ="./tpls/".$tplfile;              $comfile = "./coms/com_".$tplfile.".php";              if(!file_exists($comfile) || filemtime($comfile) < filemtime($tfile) )              {                $content = file_get_contents($tfile);                $pattern =array(                      '/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/'                      );                $replacement = array(                      '<?php  echo $this->vars["${1}"];  ?>'                  );                $newcontent = preg_replace($pattern, $replacement, $content);                file_put_contents($comfile,$newcontent);             }              include $comfile;        }    }

相關文章
相關標籤/搜索