免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2153 | 回复: 4
打印 上一主题 下一主题

高手进来看看CLASS template代码如何不能实现二级BLOCK套用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-16 23:14 |只看该作者 |倒序浏览
高手是相对而言的,也许我的问题十分的简单

看了PHPLIB,ANRAN TEMPLATE等模板
自己也继承这些模板的某些思想,模仿写了一个template class
在测试的时候发现处理二级BLOCK套用时,功能不尽人意。
费尽心思,还是久攻不下。
把代码帖出来,高手帮着分析下。我错在哪里?哪里要改进。
如要实现内嵌的BLOCK功能(现在的只能完成一级BLOCK的循环,如果在这个BLOCK里再加
一个BLOCK,就不能实现其循环了。)如要解决这个问题,为之奈何?
代码如下:


  1. <?
  2. /************************************
  3. *
  4. *       streen003 template  
  5. *      --------------------
  6. *       2008.3.15
  7. *      [email]streen003@gmail.com[/email]
  8. *      copyright [url]www.bc263.com[/url]
  9. *
  10. *************************************/
  11. class Template {

  12.         var $classname = 'Template';

  13.         var $files = array();

  14.         var $root = '';

  15.         var $varkey = array();

  16.         var $varval = array();

  17.         var $varblock = array();


  18.         function Template($root = '.')
  19.         {
  20.                 $this -> set_root($root);

  21.                 return true;
  22.         }

  23.        
  24.         function set_root($dir)
  25.         {
  26.                 if(!file_exists($dir))
  27.                 {
  28.                         trigger_error('Template -> set_root():the patch could not be found', E_USER_ERROR);
  29.                 }

  30.                 $this -> root = $dir;

  31.                 return true;
  32.         }

  33.    
  34.         function set_file($handle, $filename = '')
  35.         {
  36.                 if(!is_array($handle))
  37.                 {
  38.                         if(empty($filename))
  39.                         {
  40.                                 trigger_error('Template -> set_file():' . $filename . 'is not esists', E_USER_ERROR);
  41.                         }

  42.                         $this -> files[$handle] = $this -> root . $filename;

  43.                         $this -> load_file($handle);

  44.                         $this -> set_block($handle);
  45.                 }
  46.                 else
  47.                 {
  48.                         foreach($handle as $h => $v)
  49.                         {
  50.                                 if(empty($v))
  51.                                 {
  52.                                         trigger_error('Template -> set_file():' . $v . 'is not exists', E_USER_ERROR);
  53.                                 }

  54.                                 $this -> files[$h] = $this -> root . $v;

  55.                                 $this -> load_file($h);

  56.                                 $this -> set_block($h);
  57.                         }
  58.                 }

  59.                 return true;
  60.         }


  61.         function set_var($key, $value = '')
  62.         {
  63.                 if(!is_array($key))
  64.                 {
  65.                         $this -> varkey[$key] = '{' . $key . '}';

  66.                         $this -> varval[$key] = $value;
  67.                 }
  68.                 else
  69.                 {
  70.                         foreach($key as $k => $V)
  71.                         {
  72.                                 $this -> varkey[$k] = '{' . $k . '}';

  73.                                 $this -> varval[$k] = $v;
  74.                         }
  75.                 }

  76.                 return true;
  77.         }


  78.         function load_file($handle)
  79.         {
  80.                 if(!isset($this -> files[$handle]))
  81.                 {
  82.                         trigger_error('Template -> load_file():No file specified for handle', E_USER_ERROR);
  83.                 }

  84.                 $filename = $this -> files[$handle];

  85.                 $str = function_exists('file_get_contents') ? file_get_contents($filename) : implode('', file($filename));

  86.                 if(empty($str))
  87.                 {
  88.                         trigger_error('Template -> load_file():File' . $filename . 'for handle' . $handle . 'is empty', E_USER_ERROR);
  89.                 }

  90.                 return $this -> set_var($handle, $str);
  91.         }


  92.         function set_block($handle)
  93.         {
  94.                 if(!isset($this -> varval[$handle]))
  95.                 {
  96.                         trigger_error('Template -> set_block():Unable to load' . $handle, E_USER_ERROR);
  97.                 }

  98.                 $str = $this -> varval[$handle];

  99.                 $reg = "/<!--[ ]+BEGIN[ ]+(\w*)[ ]+-->/";

  100.                 if(preg_match($reg, $str, $m))
  101.                 {
  102.                         $blockname = $m[1];

  103.                         $reg = "/<!--[ ]+BEGIN[ ]+{$blockname}[ ]+-->(.*)<!--[ ]+END[ ]+{$blockname}[ ]+\-->/sm";

  104.                         preg_match($reg, $str, $m);

  105.                         if(isset($m[1])) $this -> set_var($blockname, $m[1]);

  106.                         $this -> set_var($handle, preg_replace($reg, '{' . $blockname . '}', $str));

  107.                         $this -> set_block($blockname);

  108.                         $this -> set_block($handle);
  109.                 }

  110.                 return true;
  111.         }


  112.         function set_block_var($handle, $key, $value = '')
  113.         {
  114.                 if(!isset($this -> varval[$handle]))
  115.                 {
  116.                         trigger_error('Template -> set_block_var():Unable to load' . $handle , E_USER_ERROR);
  117.                 }

  118.                 $str = $this -> varval[$handle];

  119.                 if(!is_array($key))
  120.                 {
  121.                         $this -> set_var($key, $value);
  122.                 }
  123.                 else
  124.                 {
  125.                         foreach($key as $k => $v)
  126.                         {
  127.                                 $this -> set_var($k, $v);
  128.                         }
  129.                 }

  130.                 $str = str_replace($this -> varkey, $this -> varval, $str);

  131.                 $this -> varblock[$handle] .=$str;

  132.                 return $this -> varblock[$handle];
  133.        
  134.         }


  135.         function display($handle)
  136.         {
  137.                 if(!isset($this -> varval[$handle]))
  138.                 {
  139.                         trigger_error('Template -> display():Unable to load' . $handle, E_USER_ERROR);
  140.                 }

  141.                 $str = $this -> varval[$handle];

  142.                 $this -> varval = array_merge($this -> varval, $this -> varblock);

  143.                 $str = str_replace($this -> varkey, $this -> varval, $str);

  144.                 echo $str;

  145.                 return true;
  146.         }


  147. }

  148. ?>


复制代码

template.rar

979 Bytes, 下载次数: 21

代码原文件

论坛徽章:
0
2 [报告]
发表于 2008-03-18 20:00 |只看该作者
两天了,没有理,
心寒呀

论坛徽章:
0
3 [报告]
发表于 2008-03-20 10:27 |只看该作者
我想道理就像剥洋葱,一层层的剥不久可以了

论坛徽章:
0
4 [报告]
发表于 2008-03-21 00:46 |只看该作者
和‘剥衣服’也是一样的

论坛徽章:
0
5 [报告]
发表于 2008-03-21 11:20 |只看该作者
楼上大色狼
楼主还是慢慢剥吧,不好帮你了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP