欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

PHP自定義Smarty模板引擎類高洛峰細說PHP

smarty模板引擎類簡單工作原理

站在用戶的角度思考問題,與客戶深入溝通,找到永新網(wǎng)站設(shè)計與永新網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋永新地區(qū)。

利用Smarty 模板引擎類對模板文件中的變量進行編譯,編譯過程其實就是利用正則表達式翻譯成PHP文件。例如 模板文件中{$title}利用正則表達式找到并替換成  <?php echo $this->vars['title'];?>

自定義 Smarty 模板引擎類 smarty.class.php頁面

<?php
/*
 * 自定義Smarty模板引擎類
 */
        class Smarty{
            private $vars = array();
            //第一個向模板中分配變量 
            //有兩個參數(shù) 一個參數(shù)是模板中的變量名,一個時分配給它的變量值
            public function assign($name,$value=null){
                        if($name != ' ')
                                $this->vars[$name]=$value;
            }
            
            //加載指定的模板 并顯示
            //有一個參數(shù)是模板的文件名
            public function display($tplname){
                        $comfile = "./comps/".$tplname."_com.php";
                        $tplname = "./templates/".$tplname;
                        //編譯文件不存在 或者模板文件有變化 才需要編譯
                        if(!file_exists($comfile) || filemtime($tplname)>filemtime($comfile)){
                        $html = file_get_contents($tplname);
                        
                        //要替換的部分{title}
                        $reg = '/\{\s*\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*\}/';
                        //變量正則表達式[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* 
                        
                        //替換后的部分<?php echo $this->vars['title'];?>
                        $rep = "<?php echo \$this->vars['\\1'];?>";
                        
                        $newhtml = preg_replace($reg, $rep, $html);    
                        file_put_contents($comfile, $newhtml);
                        }
                        include $comfile;
            }
            
        }

調(diào)用模板頁面 index.php

<?php
header('content-type:text/html;charset=utf-8');
/*
 * 模版引擎
 * PHP 超文本預(yù)處理腳本語言
 * 自定義模板引擎
 * 
 */
 //包含模板引擎類
 include 'smart.class.php';
 //創(chuàng)建模板引擎對象
 $smarty = new Smarty();
 // 連接數(shù)據(jù)庫
 //執(zhí)行SQL語句
 // 這是從數(shù)據(jù)庫獲取的動態(tài)數(shù)據(jù),需要在模板中顯示
 $title = "This is a test";
 $content = "This is content ......";
 
 //第一個向模板中分配變量
 $smarty->assign('title', $title);
 $smarty->assign('content', $content);
 var_dump($smarty);
 //加載指定的模板 并顯示
 $smarty->display('c.php');

模板文件頁  c.php頁面

<html>
<head>
<title>{$title}</title>
</head>
<body>
<h2>{$title}</h2>
<div>
{$content}
</div>
</body>
</html>

輸出結(jié)果

object(Smarty)[1]  private 'vars' => 
    array (size=2)
      'title' => string 'This is a test' (length=14)
      'content' => string 'This is content ......' (length=22)
This is a test
This is content ......

本文名稱:PHP自定義Smarty模板引擎類高洛峰細說PHP
分享網(wǎng)址:http://www.chinadenli.net/article20/iiiico.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站手機網(wǎng)站建設(shè)網(wǎng)站制作網(wǎng)站策劃網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)