<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

        最近接手一個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代碼;}-->
    posted on 2011-11-28 23:20 -274°C 閱讀(3529) 評論(0)  編輯  收藏 所屬分類: PHP

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網站

    關注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914346
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 最新国产乱人伦偷精品免费网站| 亚洲AV成人一区二区三区观看| 精品乱子伦一区二区三区高清免费播放| 欧洲美熟女乱又伦免费视频| 亚洲中文字幕精品久久| 成年18网站免费视频网站| 亚洲成人激情小说| 欧洲精品免费一区二区三区| 国产大陆亚洲精品国产| 亚洲成A∨人片天堂网无码| a高清免费毛片久久| 亚洲国产精品嫩草影院在线观看| 成人毛片100免费观看| 亚洲av无码国产精品色午夜字幕| 久久久久国色av免费看| 亚洲午夜成激人情在线影院| 一区二区无码免费视频网站| 国产精品亚洲а∨无码播放麻豆| 免费人成年激情视频在线观看 | 国产无遮挡无码视频免费软件| 亚洲VA中文字幕不卡无码| 日韩内射激情视频在线播放免费 | 亚洲一区二区三区在线观看网站| 日韩黄色免费观看| 四虎永久在线精品免费一区二区| 亚洲欧洲日产国码无码网站| 30岁的女人韩剧免费观看| 亚洲日韩看片无码电影| 国产亚洲人成A在线V网站| 最近2019中文字幕免费大全5| 亚洲JIZZJIZZ妇女| 国产亚洲精品一品区99热| 一本无码人妻在中文字幕免费| 曰批全过程免费视频观看免费软件| 亚洲深深色噜噜狠狠爱网站| 丁香花免费完整高清观看| 一道本不卡免费视频| 亚洲欧洲国产经精品香蕉网| 免费国产小视频在线观看| 久久免费视频99| 免费亚洲视频在线观看|