UChome PHP模板引擎

UChome PHP模板引擎
    最近接手一个WEB 产品,用PHP写的,发现里面使用了模板引擎从UChome 这类产品中抽取出来的。这个模板引擎的特点非常让我喜欢,简洁不冗余,原理简单明了。在以后的项目中真的可以考虑使用此类小巧而精致的模板引擎。

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;
            }
        }
    }
}

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;
}

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代码;}-->

你可能感兴趣的:(UChome PHP模板引擎)