最近接手一個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 閱讀(3527) 評論(0)  編輯  收藏 所屬分類: PHP

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914066
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 外汇| 应城市| 大同县| 洪雅县| 肥西县| 新沂市| 喀喇沁旗| 苏尼特右旗| 文水县| 彭山县| 曲阳县| 微山县| 广西| 天长市| 郯城县| 平塘县| 策勒县| 岢岚县| 徐汇区| 黄浦区| 葫芦岛市| 五华县| 剑河县| 岳阳市| 马山县| 博爱县| 乐平市| 宿迁市| 大港区| 理塘县| 揭东县| 梅河口市| 海兴县| 岐山县| 荥阳市| 城步| 寻乌县| 韩城市| 黄浦区| 湘潭县| 泰来县|