1). 調用模板(記得先引入2的文件)
include_once template("test"); // 模板的名字 test
2). 模板調用邏輯
//模板調用
function template($name) {
global $_SCONFIG, $_SGLOBAL, $_SC;
if ($_SGLOBAL['mobile']) {
$objfile = S_ROOT . './api/mobile/tpl_' . $name . '.php';
if (!file_exists($objfile)) {
showmessage('m_function_is_disable_on_wap');
}
} else {
if (strexists($name, '/')) {
$tpl = $name;
} else {
$tpl = "template/$_SCONFIG[template]/$name";
}
$objfile = S_ROOT . './data/tpl_cache/' . str_replace('/', '_', $tpl) . '.php';
if ('1' === $_SC['debug']) {
include_once(S_ROOT . './core/function_template.php');
parse_template($tpl);
} else {
if (!file_exists($objfile)) {
include_once(S_ROOT . './core/function_template.php');
parse_template($tpl);
}
}
}
return $objfile;
}
//子模板更新檢查
function subtplcheck($subfiles, $mktime, $tpl) {
global $_SC, $_SCONFIG;
if ($_SC['tplrefresh'] && ($_SC['tplrefresh'] == 1 || mt_rand(1, $_SC['tplrefresh']) == 1)) {
$subfiles = explode('|', $subfiles);
foreach ($subfiles as $subfile) {
$tplfile = S_ROOT . './' . $subfile . '.htm';
if (!file_exists($tplfile)) {
$tplfile = str_replace('/' . $_SCONFIG['template'] . '/', '/default/', $tplfile);
}
@$submktime = filemtime($tplfile);
if ($submktime > $mktime) {
include_once(S_ROOT . './source/function_template.php');
parse_template($tpl);
break;
}
}
}
}
function template($name) {
global $_SCONFIG, $_SGLOBAL, $_SC;
if ($_SGLOBAL['mobile']) {
$objfile = S_ROOT . './api/mobile/tpl_' . $name . '.php';
if (!file_exists($objfile)) {
showmessage('m_function_is_disable_on_wap');
}
} else {
if (strexists($name, '/')) {
$tpl = $name;
} else {
$tpl = "template/$_SCONFIG[template]/$name";
}
$objfile = S_ROOT . './data/tpl_cache/' . str_replace('/', '_', $tpl) . '.php';
if ('1' === $_SC['debug']) {
include_once(S_ROOT . './core/function_template.php');
parse_template($tpl);
} else {
if (!file_exists($objfile)) {
include_once(S_ROOT . './core/function_template.php');
parse_template($tpl);
}
}
}
return $objfile;
}
//子模板更新檢查
function subtplcheck($subfiles, $mktime, $tpl) {
global $_SC, $_SCONFIG;
if ($_SC['tplrefresh'] && ($_SC['tplrefresh'] == 1 || mt_rand(1, $_SC['tplrefresh']) == 1)) {
$subfiles = explode('|', $subfiles);
foreach ($subfiles as $subfile) {
$tplfile = S_ROOT . './' . $subfile . '.htm';
if (!file_exists($tplfile)) {
$tplfile = str_replace('/' . $_SCONFIG['template'] . '/', '/default/', $tplfile);
}
@$submktime = filemtime($tplfile);
if ($submktime > $mktime) {
include_once(S_ROOT . './source/function_template.php');
parse_template($tpl);
break;
}
}
}
}
3).模板解析函數
if(!defined('IN_UChome')) {
exit('Access Denied');
}
$_SGLOBAL['i'] = 0;
$_SGLOBAL['block_search'] = $_SGLOBAL['block_replace'] = array();
function parse_template($tpl) {
global $_SGLOBAL, $_SC, $_SCONFIG;
//yoho:用配置變量替換默認模板
$newtpl = empty($_SCONFIG['yoho_'.$tpl.'_template']) ? $tpl : $_SCONFIG['yoho_'.$tpl.'_template'];
//包含模板
$_SGLOBAL['sub_tpls'] = array($newtpl);
$tplfile = S_ROOT.'./'.$newtpl.'.htm';
$objfile = S_ROOT.'./data/tpl_cache/'.str_replace('/','_',$newtpl).'.php';
//read
if(!file_exists($tplfile)) {
$tplfile = str_replace('/'.$_SCONFIG['template'].'/', '/default/', $tplfile);
}
$template = sreadfile($tplfile);
if(empty($template)) {
exit("Template file : $tplfile Not found or have no access!");
}
//模板
$template = preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie", "readtemplate('\\1')", $template);
//處理子頁面中的代碼
$template = preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie", "readtemplate('\\1')", $template);
//解析模塊調用
$template = preg_replace("/\<\!\-\-\{block\/(.+?)\}\-\-\>/ie", "blocktags('\\1')", $template);
//解析廣告
$template = preg_replace("/\<\!\-\-\{ad\/(.+?)\}\-\-\>/ie", "adtags('\\1')", $template);
//時間處理
$template = preg_replace("/\<\!\-\-\{date\((.+?)\)\}\-\-\>/ie", "datetags('\\1')", $template);
//頭像處理
$template = preg_replace("/\<\!\-\-\{avatar\((.+?)\)\}\-\-\>/ie", "avatartags('\\1')", $template);
//PHP代碼
$template = preg_replace("/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies", "evaltags('\\1')", $template);
//開始處理
//變量
$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
$template = preg_replace("/(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s", "\\1['\\2']", $template);
$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
$template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
$template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);
//邏輯
$template = preg_replace("/\{elseif\s+(.+?)\}/ies", "stripvtags('<?php } elseif(\\1) { ?>','')", $template);
$template = preg_replace("/\{else\}/is", "<?php } else { ?>", $template);
//循環
for($i = 0; $i < 6; $i++) {
$template = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies", "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\\3<?php } } ?>')", $template);
$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);
$template = preg_replace("/\{if\s+(.+?)\}(.+?)\{\/if\}/ies", "stripvtags('<?php if(\\1) { ?>','\\2<?php } ?>')", $template);
}
//常量
$template = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "<?=\\1?>", $template);
//替換
if(!empty($_SGLOBAL['block_search'])) {
$template = str_replace($_SGLOBAL['block_search'], $_SGLOBAL['block_replace'], $template);
}
//換行
$template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
//附加處理
$template = $template."<?php // echo debuginfo(); ?>";
$template = "<?php if(!defined('IN_KUAIPAN')) exit('Access Denied');?><?php subtplcheck('".implode('|', $_SGLOBAL['sub_tpls'])."', '$_SGLOBAL[timestamp]', '$tpl');?>$template<?php ob_out();?>";
//write
if(!swritefile($objfile, $template)) {
exit("File: $objfile can not be write!");
}
}
function addquote($var) {
return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}
function striptagquotes($expr) {
$expr = preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr);
$expr = str_replace("\\\"", "\"", preg_replace("/\[\'([a-zA-Z0-9_\-\.\x7f-\xff]+)\'\]/s", "[\\1]", $expr));
return $expr;
}
function evaltags($php) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--EVAL_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php ".stripvtags($php)." ?>";
return $search;
}
function blocktags($parameter) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--BLOCK_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php block(\"$parameter\"); ?>";
return $search;
}
function adtags($pagetype) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--AD_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php adshow('$pagetype'); ?>";
return $search;
}
function datetags($parameter) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--DATE_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php echo sgmdate($parameter); ?>";
return $search;
}
function avatartags($parameter) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--AVATAR_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php echo avatar($parameter); ?>";
return $search;
}
function stripvtags($expr, $statement='') {
$expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
$statement = str_replace("\\\"", "\"", $statement);
return $expr.$statement;
}
function readtemplate($name) {
global $_SGLOBAL, $_SCONFIG;
$tpl = strexists($name,'/')?$name:"template/$_SCONFIG[template]/$name";
$tplfile = S_ROOT.'./'.$tpl.'.htm';
$_SGLOBAL['sub_tpls'][] = $tpl;
if(!file_exists($tplfile)) {
$tplfile = str_replace('/'.$_SCONFIG['template'].'/', '/default/', $tplfile);
}
$content = sreadfile($tplfile);
return $content;
}
exit('Access Denied');
}
$_SGLOBAL['i'] = 0;
$_SGLOBAL['block_search'] = $_SGLOBAL['block_replace'] = array();
function parse_template($tpl) {
global $_SGLOBAL, $_SC, $_SCONFIG;
//yoho:用配置變量替換默認模板
$newtpl = empty($_SCONFIG['yoho_'.$tpl.'_template']) ? $tpl : $_SCONFIG['yoho_'.$tpl.'_template'];
//包含模板
$_SGLOBAL['sub_tpls'] = array($newtpl);
$tplfile = S_ROOT.'./'.$newtpl.'.htm';
$objfile = S_ROOT.'./data/tpl_cache/'.str_replace('/','_',$newtpl).'.php';
//read
if(!file_exists($tplfile)) {
$tplfile = str_replace('/'.$_SCONFIG['template'].'/', '/default/', $tplfile);
}
$template = sreadfile($tplfile);
if(empty($template)) {
exit("Template file : $tplfile Not found or have no access!");
}
//模板
$template = preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie", "readtemplate('\\1')", $template);
//處理子頁面中的代碼
$template = preg_replace("/\<\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\>/ie", "readtemplate('\\1')", $template);
//解析模塊調用
$template = preg_replace("/\<\!\-\-\{block\/(.+?)\}\-\-\>/ie", "blocktags('\\1')", $template);
//解析廣告
$template = preg_replace("/\<\!\-\-\{ad\/(.+?)\}\-\-\>/ie", "adtags('\\1')", $template);
//時間處理
$template = preg_replace("/\<\!\-\-\{date\((.+?)\)\}\-\-\>/ie", "datetags('\\1')", $template);
//頭像處理
$template = preg_replace("/\<\!\-\-\{avatar\((.+?)\)\}\-\-\>/ie", "avatartags('\\1')", $template);
//PHP代碼
$template = preg_replace("/\<\!\-\-\{eval\s+(.+?)\s*\}\-\-\>/ies", "evaltags('\\1')", $template);
//開始處理
//變量
$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
$template = preg_replace("/(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s", "\\1['\\2']", $template);
$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
$template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
$template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);
//邏輯
$template = preg_replace("/\{elseif\s+(.+?)\}/ies", "stripvtags('<?php } elseif(\\1) { ?>','')", $template);
$template = preg_replace("/\{else\}/is", "<?php } else { ?>", $template);
//循環
for($i = 0; $i < 6; $i++) {
$template = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}(.+?)\{\/loop\}/ies", "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\\3<?php } } ?>')", $template);
$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);
$template = preg_replace("/\{if\s+(.+?)\}(.+?)\{\/if\}/ies", "stripvtags('<?php if(\\1) { ?>','\\2<?php } ?>')", $template);
}
//常量
$template = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s", "<?=\\1?>", $template);
//替換
if(!empty($_SGLOBAL['block_search'])) {
$template = str_replace($_SGLOBAL['block_search'], $_SGLOBAL['block_replace'], $template);
}
//換行
$template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
//附加處理
$template = $template."<?php // echo debuginfo(); ?>";
$template = "<?php if(!defined('IN_KUAIPAN')) exit('Access Denied');?><?php subtplcheck('".implode('|', $_SGLOBAL['sub_tpls'])."', '$_SGLOBAL[timestamp]', '$tpl');?>$template<?php ob_out();?>";
//write
if(!swritefile($objfile, $template)) {
exit("File: $objfile can not be write!");
}
}
function addquote($var) {
return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}
function striptagquotes($expr) {
$expr = preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr);
$expr = str_replace("\\\"", "\"", preg_replace("/\[\'([a-zA-Z0-9_\-\.\x7f-\xff]+)\'\]/s", "[\\1]", $expr));
return $expr;
}
function evaltags($php) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--EVAL_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php ".stripvtags($php)." ?>";
return $search;
}
function blocktags($parameter) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--BLOCK_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php block(\"$parameter\"); ?>";
return $search;
}
function adtags($pagetype) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--AD_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php adshow('$pagetype'); ?>";
return $search;
}
function datetags($parameter) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--DATE_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php echo sgmdate($parameter); ?>";
return $search;
}
function avatartags($parameter) {
global $_SGLOBAL;
$_SGLOBAL['i']++;
$search = "<!--AVATAR_TAG_{$_SGLOBAL['i']}-->";
$_SGLOBAL['block_search'][$_SGLOBAL['i']] = $search;
$_SGLOBAL['block_replace'][$_SGLOBAL['i']] = "<?php echo avatar($parameter); ?>";
return $search;
}
function stripvtags($expr, $statement='') {
$expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
$statement = str_replace("\\\"", "\"", $statement);
return $expr.$statement;
}
function readtemplate($name) {
global $_SGLOBAL, $_SCONFIG;
$tpl = strexists($name,'/')?$name:"template/$_SCONFIG[template]/$name";
$tplfile = S_ROOT.'./'.$tpl.'.htm';
$_SGLOBAL['sub_tpls'][] = $tpl;
if(!file_exists($tplfile)) {
$tplfile = str_replace('/'.$_SCONFIG['template'].'/', '/default/', $tplfile);
}
$content = sreadfile($tplfile);
return $content;
}
4).模板語法
在模板里面,是可以直接顯示php的變量的,也可以使用if,else,循環等,模板解析函數會將這些變量替換成實際的變量值。
1.設置變量的值
<!-–{eval $_TPL['titles'] = array(’日志’, ‘隨便看看’);}–->
表示將$_TPL['titles']變量設置為array(’日志’, ‘隨便看看’);
2.變量的顯示
直接寫變量的名字 $_GET[aa]
3.調用另外一個模板文件進來[模板里面再調用模板]
<!–-{template header}-–>實際上相當于php中的
相當于template(”header”) 即解析/template/default/header.htm文件
4.條件判斷 <!–-{if xxx}-–>,<!–-{else}-–>,<!–-{/if}–->類
相當于php的if,else,但一定要<!–{/if}–>結束一個if語句
5.循環 <!–-{loop}–><!–-{/loop}-–>類
<!–-{loop $list $key $value}-–>
<!--{/loop}-->
相當于php的foreach($list $key=>$value)
6.在模板寫php代碼
<!--{eval php代碼;}-->
在模板里面,是可以直接顯示php的變量的,也可以使用if,else,循環等,模板解析函數會將這些變量替換成實際的變量值。
1.設置變量的值
<!-–{eval $_TPL['titles'] = array(’日志’, ‘隨便看看’);}–->
表示將$_TPL['titles']變量設置為array(’日志’, ‘隨便看看’);
2.變量的顯示
直接寫變量的名字 $_GET[aa]
3.調用另外一個模板文件進來[模板里面再調用模板]
<!–-{template header}-–>實際上相當于php中的
相當于template(”header”) 即解析/template/default/header.htm文件
4.條件判斷 <!–-{if xxx}-–>,<!–-{else}-–>,<!–-{/if}–->類
相當于php的if,else,但一定要<!–{/if}–>結束一個if語句
5.循環 <!–-{loop}–><!–-{/loop}-–>類
<!–-{loop $list $key $value}-–>
<!--{/loop}-->
相當于php的foreach($list $key=>$value)
6.在模板寫php代碼
<!--{eval php代碼;}-->