Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198509
  • 博文数量: 264
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 2740
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-03 13:25
文章分类

全部博文(264)

文章存档

2011年(1)

2009年(263)

我的朋友

分类:

2009-06-03 16:17:56

最小,最精悍,没有一个正则表达式的模版类
代码量只有1K,实际代码只有30行。
以前用了很多的模板,用得最多的是phplib
和smarty。
模版的好处显而易见,用于应用和显示分离,特别是对于大型程序,由于美工和程序员对互相的工作并不是十分熟悉,在进行合作的过程中需要用一种约定的"语言"进行交流。这种约定的语言就是标签。
  在项目的开发中我用了很长时间的smarty
和phplib,但是最近的一个项目使我放弃了这两种模版。
    原因有:
    1.smarty的标签对美工来说看起来很怪异,不接受,而实际上开发过程中,排版效果非常差。
    2.美工往往看不懂模版的逻辑,里面的逻辑太多了。
    3.对程序员来说,smarty也很复杂,以至于有一本smarty 手册。一段时间没有用,想重拾smarty,发现似乎是去学一门新语言。
    4.phplib简单,标签看起来一目了然。
不过实际用起来还是不怎么好用。
    鉴于以上缺点:写了一个非常非常简单的模版。既然简单,所以就不强调它的功能,而是易用易学,高效,稳定,并省下了启动正则的开销。不过广大phper增加额外的负担,
php本身也是一个模版语言,不想把复杂的逻辑写在模版里面,对于实际项目中你会发现用php这些会更简单。
   《注:转载本文章请保持完整性,作者:>
   使用教程
核心代码template.php
/* @author: */
class Template{
        var $code;
        function Template($template){
            $this->code = implode('', @file($template));
        }
        function assign($name,$var=null){
    if(is_string($name) && is_string($var)){
     $this->code = str_replace('{'.$name.'}', $var, $this->code);
    } else if(is_array($var)){
     list($this->code,$tmp,$end)=explode('',$this->code);
     while(list(,$v)=each($var)){
      $t=$tmp;$k2=$v2='';
      while(list($k2, $v2) = each($v)){
           $t = str_replace('{'.$k2.'}', $v2,$t);
      }
      $this->code .= $t;
     }
     $this->code .= $end;     
    } else {     
     while (list ($k2, $v2) = each($name)){
           $this->code = str_replace('{'.$k2.'}', $v2, $this->code);
     }
    }
        }
        function display(){
                echo $this->code;
        }
}
?>
最简单的hello_world
准备一个php模版文件hello_world.html



{title}


{title}




接下来就是模版的翻译工作了
include('../include/template.php'); //包含模版核心类文件
$tpl=new Template('hello_world.html'); //参数为模版路径和文件名,可以使用相对路径,也可以使用绝对路径
$tpl->assign('title',"hello world!"); //将标签{title} 替换成hello world
$tpl->display();
?>

模版中使用数组
test_array.html


{user} {email}


{user1} {email1}




模版处理文件
include('../include/template.php');
$tpl=new Template('test_array.html');
$user=array('user'=>'yubing','email'=>'test@sina.com');
$tpl->assign($user);
$tpl->assign('user1','jack');
$tpl->assign('email1','zhuyubing@gmail.com');
$tpl->display();
?>
简单的区块处理
block.html

  
   
   
  
  
  
   
   
  
  
User NameE-Mail
{name}{email}

区块处理程序
block.php
include('../include/template.php');
$tpl=new Template('block.html');

$users=array(
  array('name'=>'jack','email'=>'test@example.com'),
  array('name'=>'tom','email'=>'tom@sina.com')
  );   
$tpl->assign('users',$users);
$tpl->display();
?>
模版包含测试



{title}


{title}


   

{block}




模版包含处理程序
include('../include/template.php');
$tpl=new Template('block.html');
$users=array(
array('name'=>'jack','email'=>'test@example.com'),
array('name'=>'tom','email'=>'zhuyubing@gmail.com'),
);
$tpl->assign('users',$users);
$block=$tpl->code;
$tpl->Template('main.html');
$tpl->assign('block',$block);
$tpl->assign(array('title'=>'测试多模版文件'));
$tpl->display();
?>
阅读(161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~