<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免费观看| 国产精品免费久久久久久久久| 精品亚洲永久免费精品| 国产亚洲日韩在线三区| 国产精品偷伦视频免费观看了| 成人亚洲综合天堂| 我们的2018在线观看免费高清| 亚洲免费视频在线观看| 99热这里只有精品免费播放| 亚洲精品高清视频| 波多野结衣在线免费观看| 亚洲喷奶水中文字幕电影| 色妞WWW精品免费视频| 亚洲狠狠婷婷综合久久蜜芽| 国产一区二区视频免费| 国产免费牲交视频免费播放 | 天天操夜夜操免费视频| 日韩亚洲人成网站| 国产亚洲情侣一区二区无码AV| 国内精品免费在线观看| 亚洲熟妇av一区二区三区下载| 特级做A爰片毛片免费69| 亚洲s码欧洲m码吹潮| 亚洲国产专区一区| 无码av免费一区二区三区试看| 亚洲精品视频在线免费| 四色在线精品免费观看| jizz免费在线影视观看网站| 久久亚洲伊人中字综合精品| 男女免费观看在线爽爽爽视频 | 日韩一区二区a片免费观看| jzzijzzij在线观看亚洲熟妇| 亚洲国产电影av在线网址| 日本不卡免费新一区二区三区| avtt天堂网手机版亚洲| 亚洲av麻豆aⅴ无码电影| 人妻无码久久一区二区三区免费| 亚洲欧美中文日韩视频| 曰韩亚洲av人人夜夜澡人人爽| 黄页网站在线看免费| 国产精品极品美女自在线观看免费|