include_once template("space_index");
function_common.php文件里有
function template($name) {
global $_SC, $_SGLOBAL;
//template/$name
$tpl = strexists($name,'/')?$name:"template/$_SC[template]/$name";
//data/tpl_cache/template_$name.php
$objfile = S_ROOT.'./data/tpl_cache/'.str_replace('/','_',$tpl).'.php';
//如果存在模板就parse_template()
if(!file_exists($objfile)) {
include_once(S_ROOT.'./source/function_template.php');
parse_template($tpl);
}
return $objfile;
}
function parse_template($tpl) {
global $_SGLOBAL, $_SC, $_SCONFIG;
//包含模板
$_SGLOBAL['sub_tpls'] = array($tpl);
$tplfile = S_ROOT.'./'.$tpl.'.htm';
$objfile = S_ROOT.'./data/tpl_cache/'.str_replace('/','_',$tpl).'.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+文件名}-->替换成'template/default/文件名的内容
$template = preg_replace("//</!/-/-/{template/s+([a-z0-9_//]+)/}/-/-/>/ie", "readtemplate('//1')", $template);
//处理子页面中的代码
$template = preg_replace("//</!/-/-/{template/s+([a-z0-9_//]+)/}/-/-/>/ie", "readtemplate('//1')", $template);
//解析模块调用"将<!--{block+内容}-->替换成<!--BLOCK_TAG_{$_SGLOBAL['i']}-->"
$template = preg_replace("//</!/-/-/{block//(.+?)/}/-/-/>/ie", "blocktags('//1')", $template);
//解析广告
//"将<!--{ad+内容}-->替换成<!--AD_TAG_{$_SGLOBAL['i']}-->
$template = preg_replace("//</!/-/-/{ad//(.+?)/}/-/-/>/ie", "adtags('//1')", $template);
//时间处理
//"将<!--{date+内容}-->替换成<!--DATE_TAG_{$_SGLOBAL['i']}-->
$template = preg_replace("//</!/-/-/{date/((.+?)/)/}/-/-/>/ie", "datetags('//1')", $template);
//头像处理
//"将<!--{avatar+内容}-->替换成<!--AVATAR_TAG_{$_SGLOBAL['i']}-->
$template = preg_replace("//</!/-/-/{avatar/((.+?)/)/}/-/-/>/ie", "avatartags('//1')", $template);
//PHP代码
//"将<!--{eval+内容}-->替换成<!--EVAL_TAG_{$_SGLOBAL['i']}-->
$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);
//逻辑
将<!--{if 内容}-->替换为<?php if(内容) ?>
$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++) {
将<!--{loop $list $key $value}-->内容<!--{/loop}-->
<?php if(is_array(//1)
}
$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 = "<?php if(!defined('IN_UCHOME')) 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!");
}
}