igaofen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

总体来讲,就是正则替换之后生成标准的php文件,然后include该文件
1.模板配置 include/config.inc.php

1 <?php   
2 //模板相关配置   
3 define('TPL_ROOT', PHPCMS_ROOT.'templates/'); //模板保存物理路径   
4 define('TPL_NAME', 'default'); //当前模板方案目录   
5 define('TPL_CSS', 'default'); //当前样式目录   
6 define('TPL_CACHEPATH', PHPCMS_ROOT.'data/cache_template/'); //模板缓存物理路径   
7 define('TPL_REFRESH', 1); //是否开启模板缓存自动刷新  
8 

2.模板函数 global.func.php

Code

 

3.模板解释函数 (include/template.func.php)

 

  1<?php   
  2/**  
  3 * 模板编译  
  4 *  
  5 * @param string 模块名  
  6 * @param string 模板名  
  7 * @param int 是否含有tag  
  8 * @return 模板的字数  
  9 */  
 10function template_compile($module, $template, $istag = 0)   
 11{   
 12    $tplfile = TPL_ROOT.TPL_NAME.'/'.$module.'/'.$template.'.html';  //获取模板路径   
 13    $content = @file_get_contents($tplfile);  //得到模板内容   
 14    if($content === false) showmessage("$tplfile is not exists!");  //模板不存存   
 15    $compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php'//编译后的模板文件   
 16    $content = ($istag || substr($template, 0, 4== 'tag_'? '<?php function _tag_'.$module.'_'.$template.'($data, $number, $rows, $count, $page, $pages, $setting){ global $PHPCMS,$MODULE,$M,$CATEGORY,$TYPE,$AREA,$GROUP,$MODEL,$templateid,$_userid,$_username;@extract($setting);?>'.template_parse($content, 1).'<?php } ?>' : template_parse($content);  //如果含有tag_,则加载tag函数   
 17    $strlen = file_put_contents($compiledtplfile, $content);  //将编译后的内容放在php文件中   
 18    @chmod($compiledtplfile, 0777);   
 19    return $strlen;   
 20}   
 21/**  
 22 * 更新编译后的模板内容  
 23 *  
 24 * @param string 模板路径  
 25 * @param string 编译后的模板路径  
 26 * @return 编译后的模板内容长度  
 27 */  
 28function template_refresh($tplfile, $compiledtplfile)   
 29{   
 30    $str = file_get_contents($tplfile);   
 31    $str = template_parse($str);   
 32    $strlen = file_put_contents($compiledtplfile, $str);   
 33    @chmod($compiledtplfile, 0777);   
 34    return $strlen;   
 35}   
 36/**  
 37 * 生成某一个模块的模板内容  
 38 *  
 39 * @param string 模块名  
 40 * @return bool  
 41 */  
 42function template_module($module)   
 43{   
 44    $files = glob(TPL_ROOT.TPL_NAME.'/'.$module.'/*.html');   
 45    if(is_array($files))   
 46    {   
 47        foreach($files as $tpl)   
 48        {   
 49            $template = str_replace('.html', '', basename($tpl));   
 50            template_compile($module, $template);   
 51        }   
 52    }   
 53    return TRUE;   
 54}   
 55/**   
 56 * 更新所有模块的模板   
 57 *   
 58 * @return bool   
 59 */   
 60function template_cache()   
 61{   
 62    global $MODULE;   
 63    foreach($MODULE as $module=>$m)   
 64    {   
 65        template_module($module);   
 66    }   
 67    return TRUE;   
 68}   
 69function template_block($blockid)   
 70{   
 71    $tplfile = TPL_ROOT.TPL_NAME.'/phpcms/block/'.$blockid.'.html';   
 72    $compiledtplfile = TPL_CACHEPATH.'phpcms_block_'.$blockid.'.tpl.php';   
 73    if(TPL_REFRESH && (!file_exists($compiledtplfile|| @filemtime($tplfile> @filemtime($compiledtplfile)))   
 74    {   
 75        template_refresh($tplfile, $compiledtplfile);   
 76    }   
 77    return $compiledtplfile;   
 78}   
 79/**  
 80 * 模板解释  
 81 *  
 82 * @param string $str  
 83 * @param int $istag  
 84 * @return string  
 85 * @link http://www.tryer.net/post/1098.html  
 86 */  
 87function template_parse($str, $istag = 0)   
 88{   
 89    $str = preg_replace("/([\n\r]+)\t+/s","\\1",$str);  //用 \n\r 过滤掉 tab制表符,  \\1 是逆向引用了第一个括号里面\n换行\r换页匹配的文本    
 90    $str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str);// 以 {xx} 来替换 <!--{xx}--> {xx}在下面肯定还要进行第二次正则替换,要不是不能在PHP里面运行的。  .+? 和 .+ 一个是懒惰 一个是贪婪。  看名字就知道。   
 91    $str = preg_replace("/\{template\s+(.+)\}/","<?php include template(\\1); ?>",$str);/*把模板里面的 {template 'xx','jj'} 编译成PHP标准写法:<?php include template('xx','jj') ?>   大家可能一看就明白了: include template()  这个在那里见过。对了。这个在PHP里也可以运行的。因为 template() 函数不是定义了吗。*/  
 92    $str = preg_replace("/\{include\s+(.+)\}/","<?php include \\1; ?>",$str);/* 模板里面的 {include xx.php} 编译成 PHP文件里的 <?php include xx.php?>**/  
 93    $str = preg_replace("/\{php\s+(.+)\}/","<?php \\1?>",$str);/* 模板里面的 {php xxxx} 编译成 <?php xxxx?>  大家也应该明白了。 xxxx 肯定是PHP的标准语法拉。 所以phpcms模板语句: {php } 就是用来给你在模板里写要运行的PHP语句。在smarty 里也有这功能**/  
 94    $str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str);/* 这个就更简单了。 把模板里面的{if xxxx}  编译成 <?php if(){?>  看这样一步一步的把一些自己定义的语句编译成PHP的标准语法。这个就叫模板引擎了。**/  
 95    $str = preg_replace("/\{else\}/","<?php } else { ?>",$str); /* {else } 转 <?php } else {?>**/  
 96    $str = preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$str); /* {elseif } 转 <?php } elseif {?>**/  
 97    $str = preg_replace("/\{\/if\}/","<?php } ?>",$str);/* {/if} 转 <?php }?>  phpcms 模板语法有: {/if}的哦。大家别忘了,要不 php肯定运行不了**/  
 98    $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);/* 下面就是循环了。模板里用{loop xx jj} 其实编译成了PHP的 foreach(xx AS jj) 这样大家都会用了吧**/  
 99    $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str);/* 这句和上面也差不多。不过是多了个取出数组的标名  {loop xx jj yy}  成 foreach(xx as jj=> yy)**/  
100    $str = preg_replace("/\{\/loop\}/","<?php } ?>",$str); /* 循环结束别忘了 {/loop}  对应PHP的 <?php }?>**/  
101    $str = preg_replace("/\{\/get\}/","<?php } unset(\$DATA); ?>",$str);   
102    $str = preg_replace("/\{tag_([^}]+)\}/e", "get_tag('\\1')", $str);/* {tag_xx}  替换为 get_tag('xx')  get_tag()  函数是自己定义的函数,因为phpcms 的模板引擎应用了标签功能。这个函数就是为了调用标签的。**/  
103    $str = preg_replace("/\{get\s+([^}]+)\}/e", "get_parse('\\1')", $str);   
104    $str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);/* {xxx(jj)}  这么个奇怪的东西。因为奇怪所以我找了下PHPCMS的模板文件。找了几个文件都没发现这个怪物。大家有谁找到的说下我去看下。怕是我理解错了正则。先谢了**/  
105    $str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);/* {$xxx(wwsd)} 专换成 <?php echo xxx(wwsd)?>   当然了 xxx() 是程序中定义过的函数**/  
106    $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>",$str);/* 把{$xxjj} 转成 <?php echo $xxjj?>  当然了是把变量输出**/  
107    $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es""addquote('<?php echo \\1;?>')",$str);/* 主要是把{$xxx['jj']} 转成 <?php echo $xxx['jj']?>  addquote() 函数自己定义的看下面,二次过滤。有代验证,头昏了看太久的黄色字。我昏**/  
108    $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s""<?php echo \\1;?>",$str);/* {XXJJ}  <?php echo XXJJ?>  XXJJ 是我们定义的常量**/  
109    if(!$istag$str = "<?php defined('IN_PHPCMS') or exit('Access Denied'); ?>".$str;   
110    return $str;   
111}   
112////这个函数在  上面这个编译函数里面看到了。 其实就是获取对应标签的内容,头有点昏,下节再说标签吧。   
113function get_tag($tagname)   
114{   
115    global $TAG;   
116    if(!isset($TAG)) $TAG = cache_read('tag.inc.php', TPL_ROOT.TPL_NAME.'/');   
117    return isset($TAG[$tagname]) ? '<?php echo '.$TAG[$tagname].';?>' : '{tag_'.$tagname.'}';   
118}   
119function addquote($var)   
120{   
121    return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s""['\\1']"$var));   
122}   
123function get_parse($str)   
124{   
125    preg_match_all("/([a-z]+)\=\"([^\"]+)\"/i", stripslashes($str), $matches, PREG_SET_ORDER);   
126    foreach($matches as $v)   
127    {   
128        $r[$v[1]] = $v[2];   
129    }   
130    extract($r);   
131    if(!isset($dbsource)) $dbsource = '';   
132    if(!isset($dbname)) $dbname = '';   
133    if(!isset($sql)) $sql = '';   
134    if(!isset($rows)) $rows = 0;   
135    if(!isset($urlrule)) $urlrule = '';   
136    if(!isset($catid)) $catid = 0;   
137    if(!isset($distinctfield)) $distinctfield = '';   
138    if(!isset($return|| !preg_match("/^\w+$/i", $return)) $return = 'r';   
139    if(isset($page))   
140    {   
141        $str = "<?php \$ARRAY = get(\"$sql\"$rows$page, \"$dbname\", \"$dbsource\", \"$urlrule\",\"$distinctfield\",\"$catid\");\$DATA=\$ARRAY['data'];\$total=\$ARRAY['total'];\$count=\$ARRAY['count'];\$pages=\$ARRAY['pages'];unset(\$ARRAY);foreach(\$DATA AS \$n=>\${$return}){\$n++;?>";   
142    }   
143    else  
144    {   
145        $str = substr($str, -1== '/' ? "<?php \${$return} = get(\"$sql\", -1, 0, \"$dbname\", \"$dbsource\");?>" : "<?php \$DATA = get(\"$sql\"$rows, 0, \"$dbname\", \"$dbsource\");foreach(\$DATA AS \$n => \${$return}) { \$n++;?>";   
146    }   
147    return $str;   
148}   
149?>  
150


 

posted on 2009-09-05 07:02  igaofen  阅读(810)  评论(0编辑  收藏  举报