| phptpl是一個輕便的PHP模板引擎。不需要什么學(xué)習(xí)成本就能輕松掌握,簡潔就是美。 最近想寫一個項目管理平臺,原來想用自己搭建的LAPC/F平臺來開發(fā),考慮到推廣使用的便捷性,最后決定重拾多年未用的PHP(不用編譯就是便捷)。搜了下發(fā)現(xiàn)現(xiàn)在的PHP開發(fā)已經(jīng)不是我讀大學(xué)時的原始了,模板、MVC啥啥的滿天飛,PHP級別的語言用MVC還是算了吧,模板倒是個好東西,神馬規(guī)模的工程都能用,最主要是分離PHP和HTML代碼,我大學(xué)時都是把PHP和HTML混雜的寫,雖然直觀但看的眼睛疼。其實模板實現(xiàn)的原理并不復(fù)雜,但網(wǎng)上搜到的要不都是大象級別的夠我學(xué)一學(xué)期了,要不就是太簡單很多功能都沒有,最后就順手自己寫了個,實際使用中效果不錯,放出來給大家也玩玩 ^_^ phptpl設(shè)計目標(biāo): ·PHP模板說穿了其實就是加載一個HTML,把其中一些字符串替換后按HTML輸出,比如把"<head>$TITLE$</head>"替換成"<head>test phptpl</head>"。 ·網(wǎng)頁里面難免會有大量表格,PHP模板還要處理可重復(fù)出現(xiàn)的明細(xì)。 ·剛才看了誰實現(xiàn)的PHP模板引擎,擁有判斷條件影響某HTML區(qū)域出現(xiàn)的功能,好吧,我也要支持它! 未寫實現(xiàn)先寫測試案例 PHP模板文件 test_phptpl.html [code] <!-- template for testing phptpl --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$TITLE$</title> </head> <body> <center>$TABLE_HEADER$</center> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <!-- BEGIN $DETAIL$ SECTION --> <tr> <td> $USER_ID$ </td> <td> $USER_NAME$ </td> </tr> <!-- END $DETAIL$ SECTION --> </table> <!-- IF $LOGIN$ --> <p>somebody login</p> <!-- ELSE $LOGIN$ --> <p>no user login</p> <!-- ENDIF $LOGIN$ --> </body> </html> [/code] 測試phptpl文件 test_phptpl.php [code] <?php /** * test phptpl */ require "phptpl.php" ; // 字符串替換配置 $str_replace_array['$TITLE$'] = "test_phptpl" ; $str_replace_array['$TABLE_HEADER$'] = "MY_TABLE_HEADER" ; // 明細(xì)替換配置 function detail_function( $in_str = null , $print_flag = false ) { if( $in_str == null ) return null; $out_str = "" ; for( $i = 1 ; $i <= 3 ; $i++ ) { $str_replace_array = array() ; $str_replace_array['$USER_ID$'] = "MY_TITLE_" . (string)$i ; $str_replace_array['$USER_NAME$'] = "MY_USER_NAME_" . (string)$i ; $out_str .= phptpl_str( $in_str , $str_replace_array , null , null , null , false ) ; } if( $print_flag == true ) print $out_str ; return $out_str; } $section_replace_array['$DETAIL$'] = "detail_function" ; // 區(qū)域存在配置 $if_exist_array['$LOGIN$'] = true ; // 執(zhí)行模板處理并立即輸出 phptpl_file( "test_phptpl.html" , $str_replace_array , null , null , $if_exist_array , $section_replace_array , true ); ?> [/code] 天馬行空一番后開始認(rèn)真寫phptpl實現(xiàn) phptpl.php [code] <?php /** * phptpl - PHP Cute Template Engine * AUTHOR : calvin(calvinwilliams.c@gmail.com) * COPYRIGHT : by calvin * LICENSE : LGPL (http://www./licenses/lgpl.html) * VERSION : v1.0.0 2014-02-16 create */ // PHP模板引擎,輸入源為字符串 function phptpl_str( $in_str = null , $str_replace_array = null , $ereg_replace_array = null , $preg_replace_array = null , $if_exist_array = null , $section_replace_array = null , $print_flag = false ) { ... } ?> [/code] 在apache里建了個虛擬主機(jī)跑test_phptpl.php,運行一次通過,看來十年前學(xué)的PHP功底還是那么扎實,吼吼 為了展示模板處理前后對比,我再把模板HTML貼一遍 [code] <!-- template for testing phptpl --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$TITLE$</title> </head> <body> <center>$TABLE_HEADER$</center> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <!-- BEGIN $DETAIL$ SECTION --> <tr> <td> $USER_ID$ </td> <td> $USER_NAME$ </td> </tr> <!-- END $DETAIL$ SECTION --> </table> <!-- IF $LOGIN$ --> <p>somebody login</p> <!-- ELSE $LOGIN$ --> <p>no user login</p> <!-- ENDIF $LOGIN$ --> </body> </html> [/code] 用phptpl模板引擎處理后輸出 [code] <!-- template for testing phptpl --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test_phptpl</title> </head> <body> <center>MY_TABLE_HEADER</center> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td> MY_TITLE_1 </td> <td> MY_USER_NAME_1 </td> </tr> <tr> <td> MY_TITLE_2 </td> <td> MY_USER_NAME_2 </td> </tr> <tr> <td> MY_TITLE_3 </td> <td> MY_USER_NAME_3 </td> </tr> </table> <!-- IF $LOGIN$ --> <p>somebody login</p> </body> </html> [/code] 輸出太多,整理如下 [code] <title>$TITLE$</title> [/code] 替換成了 [code] <title>test_phptpl</title> [/code] 了 [code] <center>$TABLE_HEADER$</center> [/code] 替換成了 [code] <center>MY_TABLE_HEADER</center> [/code] 了 [code] <!-- BEGIN $DETAIL$ SECTION --> <tr> <td> $USER_ID$ </td> <td> $USER_NAME$ </td> </tr> <!-- END $DETAIL$ SECTION --> [/code] 替換成了 [code] <tr> <td> MY_TITLE_1 </td> <td> MY_USER_NAME_1 </td> </tr> <tr> <td> MY_TITLE_2 </td> <td> MY_USER_NAME_2 </td> </tr> <tr> <td> MY_TITLE_3 </td> <td> MY_USER_NAME_3 </td> </tr> [/code] 了 [code] <!-- IF $LOGIN$ --> <p>somebody login</p> <!-- ELSE $LOGIN$ --> <p>no user login</p> <!-- ENDIF $LOGIN$ --> [/code] 過濾成了 [code] <!-- IF $LOGIN$ --> <p>somebody login</p> [/code] 了 大功告成 最后來解釋下測試代碼 [code] <?php /** * test phptpl */ require "phptpl.php" ; // 字符串替換配置 $str_replace_array['$TITLE$'] = "test_phptpl" ; $str_replace_array['$TABLE_HEADER$'] = "MY_TABLE_HEADER" ; // 明細(xì)替換配置 function detail_function( $in_str = null , $print_flag = false ) { if( $in_str == null ) return null; $out_str = "" ; for( $i = 1 ; $i <= 3 ; $i++ ) { $str_replace_array = array() ; $str_replace_array['$USER_ID$'] = "MY_TITLE_" . (string)$i ; $str_replace_array['$USER_NAME$'] = "MY_USER_NAME_" . (string)$i ; $out_str .= phptpl_str( $in_str , $str_replace_array , null , null , null , false ) ; } if( $print_flag == true ) print $out_str ; return $out_str; } $section_replace_array['$DETAIL$'] = "detail_function" ; // 區(qū)域存在配置 $if_exist_array['$LOGIN$'] = true ; // 執(zhí)行模板處理并立即輸出 phptpl_file( "test_phptpl.html" , $str_replace_array , null , null , $if_exist_array , $section_replace_array , true ); ?> [/code] 函數(shù)phptpl_file第一個參數(shù)是要裝載的外部HTML模板文件,第二個參數(shù)是字符串替換數(shù)組配置,賦值格式為 [code] $str_replace_array['(源字符串)'] = "(目標(biāo)字符串)" ; [/code] 對應(yīng)HTML模板里出現(xiàn)"(源字符串)" 這樣的格式在代碼中指明,也可以從配置文件讀入,第三個參數(shù)和第四個參數(shù)也是用于字符串替換配置,只不過是用作兩種正則表達(dá),第五個參數(shù)是判斷布爾型變量決定模板中一個區(qū)域是否出現(xiàn),賦值格式為 [code] $if_exist_array['(區(qū)域名)'] = true ; [/code] 對應(yīng)HTML模板里 [code] <!-- IF (區(qū)域名) --> 區(qū)域1 <!-- ELSE (區(qū)域名) --> 區(qū)域2 <!-- ENDIF (區(qū)域名) --> [/code] 第六個參數(shù)是可重復(fù)明細(xì)區(qū)域的回調(diào)函數(shù)指針,賦值格式為 [code] $section_replace_array['$DETAIL$'] = "detail_function" ; [/code] 對應(yīng)HTML模板里 [code] <!-- BEGIN $DETAIL$ SECTION --> <tr> <td> $USER_ID$ </td> <td> $USER_NAME$ </td> </tr> <!-- END $DETAIL$ SECTION --> [/code] 最后一個參數(shù)表示最后實例化的模板是否立即輸出到標(biāo)準(zhǔn)輸出,即瀏覽器,當(dāng)設(shè)置為false時作為函數(shù)返回值返回到上層。 整個phptpl模板引擎只有兩個函數(shù) [code] // PHP模板引擎,輸入源為字符串 function phptpl_str( $in_str = null , $str_replace_array = null , $ereg_replace_array = null , $preg_replace_array = null , $if_exist_array = null , $section_replace_array = null , $print_flag = false ) // PHP模板引擎,輸入源為模板文件名 function phptpl_file( $in_pathfilename = null , $str_replace_array = null , $ereg_replace_array = null , $preg_replace_array = null , $if_exist_array = null , $section_replace_array = null , $print_flag = false ) [/code] 很簡潔吧 是不是越看越心動了?,那就趕緊下載來玩玩吧 首頁傳送門 : http://git.oschina.net/calvinwilliams/phptpl 
  
  
    
    
    更多
    0
  
  分享到:
  
  
  
  
 | 
|  |