本文實例講述了smarty模板引擎基礎知識。分享給大家供大家參考。具體如下:
一、基本概念
1.什么是mvc?
mvc是一種開發模式,核心思想是:數據的輸入、數據的處理、數據顯示的強制分離。
2.什么是smarty?
smarty是一個php的模板引擎。更明確的來說,它可以幫助開發者更好的分離程序邏輯和頁面顯示。
3.smarty運行原理
模板文件,就是一個顯示數據的模板,其中需要顯示的數據用占位符代替。
smarty運行時,會讀取模板文件,將模板文件中的占位符替換成真正的數據,并輸出一個處理后的php文件,交由服務器運行。
二、自己寫一個smarty模板
為了更好的理解smarty模板,現在自己先寫一個自己的smarty模板-minismarty,讓自己更加深入的了解smarty運行原理。
1.新建項目minismarty
新建模板文件路徑:templates
新建模板文件被編譯后的文件路徑:templates_c
新建模板文件:intro.tpl
新建運行的文件:index.php
新建自己的smarty,即處理模板的文件:cls_MiniSmarty.php
2.編寫index.php文件
1
2
3
4
5
6
7
8
9
|
<?php require_once './cls_MiniSmarty.php' ; $miniSmarty = new MiniSmarty(); //傳遞數據 $miniSmarty ->assign( "title" , "hello minismarty!" ); $miniSmarty ->assign( "content" , "<font color='red'>this is content!</font>" ); //傳遞數據到哪個頁面顯示 $miniSmarty ->display( "intro.tpl" ); ?> |
3.編寫intro.tpl文件
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--這是個模板文件--> <html> <head> <meta http-equiv= "Content-Language" content= "en" /> <meta name= "GENERATOR" content= "PHPEclipse 1.0" /> <meta http-equiv= "Content-Type" content= "text/html; charset=iso-8859-1" /> <title>{ $title }</title> </head> <body bgcolor= "#FFFFFF" text= "#000000" link= "#FF9966" vlink= "#FF9966" alink= "#FFCC99" > { $content } </body> </html> |
這里面的內容是用占位符的形式,smarty的作用就是將占位符的內容替換成真正的數據。
這樣就可以實現模板文件和數據文件強制分離,通過smarty進行數據的傳遞。
4.編寫cls_MiniSmarty.php文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
<?php /** * * 原本是通過smarty模板引擎給模板提供數據的 * 現在自己模仿寫一個模板,給模板提供數據的類 * smarty運行時,讀取模板文件,將模板文件替換成可運行的php文件 * 服務器真正運行的文件是處理后的文件 */ class MiniSmarty { //模板文件路徑 var $template_dir = "./templates/" ; //模板文件被替換后的文件路徑 var $templates_c_dir = "./templates_c/" ; //存放變量值 var $tpl_vars = array (); //主要模擬2個方法 /** * 添加數據 * 參數1:鍵 * 參數2:值,默認為null */ function assign( $tpl_var , $var = null) { if ( $tpl_var != '' ) { $this ->tpl_vars[ $tpl_var ] = $var ; //將數據添加到數組中 } } /** * 顯示數據 * 參數1:顯示到哪個模板文件中 */ function display( $tpl_file ) { //獲得模板文件的路徑 $tpl_file_path = $this ->template_dir . $tpl_file ; //獲得模板文件被編譯后的文件路徑 $compile_file_path = $this ->templates_c_dir . "com_" . $tpl_file . ".php" ; //判斷文件是否存在 if (! file_exists ( $tpl_file_path )) { return false; } //不用每次都生成編譯文件,只有編譯文件不存在或者模板文件被修改了才生成新的編譯文件 //相當于緩存了編譯文件 //filemtime函數:獲得文件的生成時間 if (! file_exists ( $compile_file_path ) || filemtime ( $tpl_file_path ) > filemtime ( $compile_file_path )) { //讀取模板文件的內容 $fpl_file_content = file_get_contents ( $tpl_file_path ); $newStr = myReplace( $fpl_file_content ); //將替換后的字符串生成新的文件,也就是編譯后的文件 file_put_contents ( $compile_file_path , $newStr ); } //引入編譯后的文件 include $compile_file_path ; } /** * 對模板文件中的內容進行替換,獲得新的字符串 */ function myReplace( $fpl_file_content ) { $pattern = array ( '/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i' ); $replace = array ( '<?php echo $this->tpl_vars["${1}"] ?>' ); $newStr = preg_replace( $pattern , $replace , $fpl_file_content ); return $newStr ; } } ?> |
preg_replace方法介紹:
參數1:替換的規則
參數2:替換成的內容
參數3:替換操作的內容
5.運行結果
標題和內容都顯示出來了:
結論:
真正運行的文件,既不是index.php,也不是intro.php,而是二者通過smarty作用后的文件:
com_intro.tpl.php。這個文件中數據來源于index.php,顯示的布局來自intro.tpl,中間的橋梁是smarty。
smarty的作用是接受數據、填充數據(替換模板中的占位符)、并加載替換后的文件。
三、講解smarty使用細節
1.如何配置smarty?
解壓后,將libs文件夾拷貝到項目目錄下即可,然后再創建2個文件夾templates和templates_c,分別放模板文件和模板編譯后文件。
2.使用smarty注意事項
①替換變量的標示符。
因為默認的表示符是{}這個和style樣式中的{}發生沖突,所以需要修改一下默認的標識符,一般修改為:{<>}
②修改標識符的方法。
方法一:直接修改smarty類源碼:不推薦。
方法二:使用smarty提供的方法進行修改。
1
2
|
$smarty ->left_delimiter= "{<" ; $smarty ->right_delimiter= ">}" ; |
③smarty的一些基本配置
1
2
3
4
|
$smarty ->template_dir= "./templates" ; //模板路徑 $smarty ->compile_dir= "./templates_c" ; //編譯路徑 $smarty ->caching=false; //是否使用緩存 $smarty ->cache_dir= "./smarty_cache" ; //如果使用緩存的話:緩存的路徑 |
3.smarty模板技術分配變量的細節問題
一句話:可以分配php支持的各種數據。
php基本數據:int double string bool
復合數據類型:array object
特殊數據類型:resource null
希望本文所述對大家的php程序設計有所幫助。