UCHome二次開發(fā)】模板解析
來源:
發(fā)布時間:2010/4/24
瀏覽次數(shù):671
UCHome模板文件位于/template文件夾下,每個模板文件單獨(dú)一個文件夾,默認(rèn)模板文件夾為default。
1、模板的使用配置
在根目錄下的config.php中進(jìn)行配置,確定系統(tǒng)使用的模板,如下:
1 |
$_SC [ 'template' ] = 'default' ; |
2、模板的處理
程序中使用到模板文件時,先去模板緩存目錄/data/tpl_cache/下查找是否存儲模板緩存文件。模板緩存文件命名合適為“template_模板目錄名_模板文件名.php”。如存在則直接使用該緩存的模板文件;如不存在,則先解析對應(yīng)的模板文件,生成模板緩存文件再進(jìn)行使用。
3、模板的解析
模板解析是調(diào)用/source目錄下的function_template.php文件中的parse_template函數(shù)來實(shí)現(xiàn)的。
解析過程并不復(fù)雜,主要是讀取模板文件(.htm),用正則表達(dá)式替換標(biāo)記為對應(yīng)的PHP代碼,最終生成一個標(biāo)準(zhǔn)的PHP文件,保存到模板緩存目錄/data/tpl_cache/供后續(xù)使用。
具體的模板解析過程不做說明,直接查看代碼即可。
01 |
function parse_template( $tpl ) { |
05 |
$_SGLOBAL [ 'sub_tpls' ] = array ( $tpl ); |
07 |
$tplfile = S_ROOT. './' . $tpl . '.htm' ; |
08 |
$objfile = S_ROOT. './data/tpl_cache/' . str_replace ( '/' , '_' , $tpl ). '.php' ; |
11 |
$template = sreadfile( $tplfile ); |
12 |
if ( empty ( $template )) { |
13 |
exit ( "Template file : $tplfile Not found or have no access!" ); |
17 |
$template = preg_replace( "/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie" , "readtemplate('\\1')" , $template ); |
19 |
$template = preg_replace( "/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie" , "readtemplate('\\1')" , $template ); |
21 |
$template = preg_replace( "/\<\!\-\-\{block\/(.+?)\}\-\-\>/ie" , "blocktags('\\1')" , $template ); |
23 |
$template = preg_replace( "/\<\!\-\-\{ad\/(.+?)\}\-\-\>/ie" , "adtags('\\1')" , $template ); |
25 |
$template = preg_replace( "/\<\!\-\-\{date\((.+?)\)\}\-\-\>/ie" , "datetags('\\1')" , $template ); |
27 |
$template = preg_replace( "/\<\!\-\-\{avatar\((.+?)\)\}\-\-\>/ie" , "avatartags('\\1')" , $template ); |
29 |
$template = preg_replace( "/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies" , "evaltags('\\1')" , $template ); |
33 |
$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)" ; |
34 |
$template = preg_replace( "/\<\!\-\-\{(.+?)\}\-\-\>/s" , "{\\1}" , $template ); |
35 |
$template = preg_replace( "/([\n\r]+)\t+/s" , "\\1" , $template ); |
36 |
$template = preg_replace( "/(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s" , "\\1['\\2']" , $template ); |
37 |
$template = preg_replace( "/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s" , "<?=\\1?>" , $template ); |
38 |
$template = preg_replace( "/$var_regexp/es" , "addquote('<?=\\1?>')" , $template ); |
39 |
$template = preg_replace( "/\<\?\=\<\?\=$var_regexp\?\>\?\>/es" , "addquote('<?=\\1?>')" , $template ); |
41 |
$template = preg_replace( "/\{elseif\s+(.+?)\}/ies" , "stripvtags('<?php } elseif(\\1) { ?>','')" , $template ); |
42 |
$template = preg_replace( "/\{else\}/is" , "<?php } else { ?>" , $template ); |
44 |
for ( $i = 0; $i < 5; $i ++) { |
45 |
$template = preg_replace( "/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies" , "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\\3<?php } } ?>')" , $template ); |
46 |
$template = preg_replace( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies" , "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','\\4<?php } } ?>')" , $template ); |
47 |
$template = preg_replace( "/\{if\s+(.+?)\}(.+?)\{\/if\}/ies" , "stripvtags('<?php if(\\1) { ?>','\\2<?php } ?>')" , $template ); |
50 |
$template = preg_replace( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s" , "<?=\\1?>" , $template ); |
53 |
if (! empty ( $_SGLOBAL [ 'block_search' ])) { |
54 |
$template = str_replace ( $_SGLOBAL [ 'block_search' ], $_SGLOBAL [ 'block_replace' ], $template ); |
58 |
$template = preg_replace( "/ \?\>[\n\r]*\<\? /s" , " " , $template ); |
61 |
$template = "<?php if(!defined('IN_UCHOME')) exit('Access Denied');?><?php subtplcheck('" .implode('| ', $_SGLOBAL[' sub_tpls '])."' , '$_SGLOBAL[timestamp]' , '$tpl' );?> $template <?php ob_out();?>"; |
64 |
if (!swritefile( $objfile , $template )) { |
65 |
exit ( "File: $objfile can not be write!" ); |