喜悦国际村 » 喜悦原创 » Just template---发一个自己编写的模板

页: [1]
streen0032008-3-24 04:50 PM
Just template---发一个自己编写的模板

本人最近研究了PHPLIB等模板之后,乘兴编写了this template class 。之所以命名为:Just template,就是告诫自己这仅仅是一个模板,真正PHP世界的迷人风光我还没有领略到。所以,我对自己说:“streen003向前行,streen003一定赢”。
       Just template 作为分析型的模板,采用了与PHPLIB相似的运行原理。功能不算强大,不过速度高效,操作简单,依然不失为template class里的实用之选。
模板代码如下:
[php]
<?php
/******************************************************************
*
*                        Just template
*                       ---------------
*            Written by streen003 <[email]streen003@gmail.com[/email]>
*                     [url]http://www.bc263.com[/url]
*                Version: Just template 2008.3.19.
*
******************************************************************/
/**
*  The interface of this Template class was originally inspired
*  by PHPLib templates,and the template file formats are quite
*  similar. Thanks for flyinghail's and anran's help.
*/

class Template {
   
// 声明模板变量
var $classname = 'Template';
var $files = array();  
var $root = '';
var $varkey = array();
var $varval = array();
var $varblock = array();
var $varblockkey = array();
var $varblockval = 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 exists', 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;
}
   
// 设置数值 set_var(变量名或是数组,设置数值[数组不设置此值]);
function set_var($key, $value = '', $inside = false)
{
  if(!$inside)
  {
   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;
    }
   }
  }
  else
  {
   if(!is_array($key))
   {
    $this -> varblockkey[$key] = '{' . $key . '}';
    $this -> varblockval[$key] = $value;
   }
   else
   {
    foreach($key as $k => $v)
    {
     $this -> varblockkey[$k] = '{' . $k . '}';
     $this -> varblockval[$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);
}
    // 设置BLOCK数据。
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[ ]+(.*?)[ ]+-->/";
  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;
}
    // 处理BLOCK循环。
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($handle . '.' . $key, $value, true);
  }
  else
  {
   foreach($key as $k => $v)
   {
    $this -> set_var($handle . '.' . $k, $v, true);
   }
  }
  $str = str_replace($this -> varblockkey, $this -> varblockval, $str);
  $this -> varblock[$handle] .= $str;
  return $this -> varblock[$handle];
}
   
    // 处理BLOCK二重循环时,内部BLOCK数据与外部BLOCK数据的替换。
    function parse($main, $handle)
{
  if(!isset($this -> varblock[$main]) || !isset($this -> varblock[$handle]) || !isset($this -> varkey[$handle]))
  {
   trigger_error('Template -> parse(): Unable to load' . $main . ' or ' . $handle, E_USER_ERROR);
  }
  else
  {
   $this -> varblock[$main] = str_replace($this -> varkey[$handle], $this -> varblock[$handle], $this -> varblock[$main]);
   unset($this -> varblock[$handle]);
   return true;
  }
}
   
// 输出模板内容。
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;
}
}
?>

[/php]

chinahtml2008-3-25 10:58 AM
看起来跟Phplib长得很像。

可酷可乐2008-3-26 02:53 AM
the patch  
应该是The path
patch是补丁,path是路径。

qh6632008-3-28 05:19 PM
高手就是这样成长的! 在不断探索与实践中进步!

mdy_jun2008-4-3 12:43 AM
学习一下。


查看完整版本: Just template---发一个自己编写的模板


Powered by Discuz! Archiver 6.1.0  © 2001-2006 Comsenz Inc.
Processed in 0.051125 second(s), 2 queries