- 论坛徽章:
- 0
|
高手是相对而言的,也许我的问题十分的简单
看了PHPLIB,ANRAN TEMPLATE等模板
自己也继承这些模板的某些思想,模仿写了一个template class
在测试的时候发现处理二级BLOCK套用时,功能不尽人意。
费尽心思,还是久攻不下。
把代码帖出来,高手帮着分析下。我错在哪里?哪里要改进。
如要实现内嵌的BLOCK功能(现在的只能完成一级BLOCK的循环,如果在这个BLOCK里再加
一个BLOCK,就不能实现其循环了。)如要解决这个问题,为之奈何?
代码如下:
- <?
- /************************************
- *
- * streen003 template
- * --------------------
- * 2008.3.15
- * [email]streen003@gmail.com[/email]
- * copyright [url]www.bc263.com[/url]
- *
- *************************************/
- class Template {
- var $classname = 'Template';
- var $files = array();
- var $root = '';
- var $varkey = array();
- var $varval = array();
- var $varblock = array();
- function Template($root = '.')
- {
- $this -> set_root($root);
- return true;
- }
-
- function set_root($dir)
- {
- if(!file_exists($dir))
- {
- trigger_error('Template -> set_root():the patch could not be found', E_USER_ERROR);
- }
- $this -> root = $dir;
- return true;
- }
-
- function set_file($handle, $filename = '')
- {
- if(!is_array($handle))
- {
- if(empty($filename))
- {
- trigger_error('Template -> set_file():' . $filename . 'is not esists', E_USER_ERROR);
- }
- $this -> files[$handle] = $this -> root . $filename;
- $this -> load_file($handle);
- $this -> set_block($handle);
- }
- else
- {
- foreach($handle as $h => $v)
- {
- if(empty($v))
- {
- trigger_error('Template -> set_file():' . $v . 'is not exists', E_USER_ERROR);
- }
- $this -> files[$h] = $this -> root . $v;
- $this -> load_file($h);
- $this -> set_block($h);
- }
- }
- return true;
- }
- function set_var($key, $value = '')
- {
- if(!is_array($key))
- {
- $this -> varkey[$key] = '{' . $key . '}';
- $this -> varval[$key] = $value;
- }
- else
- {
- foreach($key as $k => $V)
- {
- $this -> varkey[$k] = '{' . $k . '}';
- $this -> varval[$k] = $v;
- }
- }
- return true;
- }
- function load_file($handle)
- {
- if(!isset($this -> files[$handle]))
- {
- trigger_error('Template -> load_file():No file specified for handle', E_USER_ERROR);
- }
- $filename = $this -> files[$handle];
- $str = function_exists('file_get_contents') ? file_get_contents($filename) : implode('', file($filename));
- if(empty($str))
- {
- trigger_error('Template -> load_file():File' . $filename . 'for handle' . $handle . 'is empty', E_USER_ERROR);
- }
- return $this -> set_var($handle, $str);
- }
- function set_block($handle)
- {
- if(!isset($this -> varval[$handle]))
- {
- trigger_error('Template -> set_block():Unable to load' . $handle, E_USER_ERROR);
- }
- $str = $this -> varval[$handle];
- $reg = "/<!--[ ]+BEGIN[ ]+(\w*)[ ]+-->/";
- if(preg_match($reg, $str, $m))
- {
- $blockname = $m[1];
- $reg = "/<!--[ ]+BEGIN[ ]+{$blockname}[ ]+-->(.*)<!--[ ]+END[ ]+{$blockname}[ ]+\-->/sm";
- preg_match($reg, $str, $m);
- if(isset($m[1])) $this -> set_var($blockname, $m[1]);
- $this -> set_var($handle, preg_replace($reg, '{' . $blockname . '}', $str));
- $this -> set_block($blockname);
- $this -> set_block($handle);
- }
- return true;
- }
- function set_block_var($handle, $key, $value = '')
- {
- if(!isset($this -> varval[$handle]))
- {
- trigger_error('Template -> set_block_var():Unable to load' . $handle , E_USER_ERROR);
- }
- $str = $this -> varval[$handle];
- if(!is_array($key))
- {
- $this -> set_var($key, $value);
- }
- else
- {
- foreach($key as $k => $v)
- {
- $this -> set_var($k, $v);
- }
- }
- $str = str_replace($this -> varkey, $this -> varval, $str);
- $this -> varblock[$handle] .=$str;
- return $this -> varblock[$handle];
-
- }
- function display($handle)
- {
- if(!isset($this -> varval[$handle]))
- {
- trigger_error('Template -> display():Unable to load' . $handle, E_USER_ERROR);
- }
- $str = $this -> varval[$handle];
- $this -> varval = array_merge($this -> varval, $this -> varblock);
- $str = str_replace($this -> varkey, $this -> varval, $str);
- echo $str;
- return true;
- }
- }
- ?>
复制代码 |
|