<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                下載[https://github.com/smarty-php/smarty/releases/](https://github.com/smarty-php/smarty/releases/) ## **smarty原理:** php加載帶有特定規則的html,然后通過正則匹配出特定規則,最后將匹配出的特定規則替換上真實數據 **smarty原理 例子1:** 1.html ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3>{$username}</h3> <h3>{$age}</h3> </body> </html> ``` 1.php ``` <?php // 將1.html頁面的具有特殊標示符的變量提取出來,并進行替換操作 // 1.讀取1.html文件的內容(將1.html的內容作為字符串讀取出來) $info = file_get_contents('1.html');//將html模板文件的內容讀取出來 // echo $info; // 2.替換操作{$username}即替換出加載1,html內容的{$username} $preg = '/\{\$[a-zA-Z_][a-zA-Z_0-9]*\}/';匹配出{username}和{$age} $str = preg_replace($preg, 'zhangsan', $info);//將{username}和{$age}都替換成了zhangsan echo $str; ?> ``` **例子2:** Engine.class.php ``` // 模板引擎 class Engine { // 定義存儲數據的屬性 private $fields; private $template_dir = 'templates/'; // 模板目錄 private $compile_dir = 'compile/'; // 編譯目錄 private $cache_dir = 'cache/'; // 緩存目錄 // 分配變量 如:$eng->assign('username','zhangsan'); public function assign($key, $value) { $this->fields[$key] = $value; } /** * 編譯模板(template) * 將編譯文件放置在compile目錄下 * 將模板文件放置在templates目錄下 */ public function display($tpl) //傳入$tpl模板頁的地址 { // 1.讀取模板數據 $info = file_get_contents($this->template_dir.$tpl);//讀取模板內容里面有準備替換的{$username} // 2.數據替換操作preg_replace($patterns, $replace, $str); $preg = '/\{\$([a-zA-Z_][a-zA-Z_0-9]*)\}/'; //$preg可能會匹配很多[模版變量 ]他是一個數組{$username} //echo preg_replace($preg,'\\1', $info); 替換成了username $replace = "<?php echo \$this->fields['\\1']; ?>"; // \\1 匹配出()里的內容即username $str = preg_replace($preg,$replace, $info); //在$info里用$preg 匹配出的內容用$replace 替換掉上面替換成了<?php echo $this->fields['username']; ?> // //1表示獲取()里的第一個分組內容 // echo $str;替換的內容是字符串不能當代碼使用這時需要存入一個文件在加載進來就可以當代嗎使用了; // 3.將新替換的內容存儲到新文件中 file_put_contents($this->compile_dir.$tpl, $str); // 將生成的靜態文件緩存下來 ob_start(); // 4.再將編譯文件引入進來 include $this->compile_dir.$tpl; // 獲取緩存內容 $a = ob_get_contents(); // 將緩存的數據存入cache文件中 file_put_contents($this->cache_dir.$tpl, $a); // 關閉緩存 ob_end_flush(); } } ``` 1.php ``` // 1.引入模板引擎 include 'Engine.class.php'; // 2.實例化模板引擎 $eng = new Engine; // 3.分配變量 $eng->assign('username','zhangsan'); // 4.編譯模板并輸出 $eng->display('1.html'); ``` ThinkPHP/Extend/Vendor/Smarty ThinkPHP\Library\Vendor\Smarty templates/1.html //模板文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3>{$username}</h3> </body> </html> ``` compile/1.html 編譯文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3><?php echo $this->fields['username']; ?></h3> </body> </html> ``` cache/1.html //緩存文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3>zhangsan</h3> </body> </html> ``` preg_replace用法: preg_replace — 執行一個正則表達式的搜索和替換preg_replace (要搜索的模式。可以使一個字符串或字符串數組, 用于替換的字符串或字符串數組, 要進行搜索和替換的字符串或字符串數組) ``` <?php $patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '/^\s*{(\w+)}\s*=/'); $replace = array ('\3/\4/\1\2', '$\1 ='); echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27'); ``` 輸出:$startDate = 5/27/1999 preg_replace ($pattern ,$replacement ,$subject):用replacement替換掉 $pattern 匹配出來的$subject里的內容 如果$replacement是字符串 $pattern是數組那么所有的模式都使用replacement這個字符串進行替換 如果pattern和replacement 都是數組,每個pattern使用replacement中對應的 元素進行替換。如果replacement中的元素比pattern中的少, 多出來的pattern使用空字符串進行替換
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看