php的简单模板类

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

我就喜欢简单:)

<?php

// QuickTpl类,用于模板

// lovered (GV) 于2004年4月11日创建

class QuickTpl

{

var $filename; //模板文件

var $content; //返回内容

//初始化模板文件,将所有内容读入

function QuickTpl($tplfilename)

{

$this->filename=$tplfilename;

$fd = fopen( $this->filename, "r" );

$this->content = fread($fd, filesize($this->filename));

fclose( $fd );

}

//替换标志位内容

function Assign($key,$value)

{

$this->content=str_replace("{".$key."}",$value,$this->content);

}

//替换标志块内容

function BlockAssign($block_name,$values)

{

//获得替换块的子模板

if(is_array($values))

{

ereg("{".$block_name."}.*{/".$block_name."}",$this->content,$regs);

$str_block=substr($regs[0],2+strlen($block_name),-(strlen($block_name)+3));

$str_replace="";

$block_replace="";

foreach($values as $subarr)

{

$str_replace=$str_block;

while ( list( $key, $val ) = each( $subarr ) )

{

$str_replace=str_replace("{".$key."}",$val,$str_replace);

}

$block_replace.=$str_replace;

}

$this->content=ereg_replace ("{".$block_name."}.*{/".$block_name."}",$block_replace,$this->content);

}

else

$this->content=ereg_replace ("{".$block_name."}.*{/".$block_name."}","none",$this->content);

}

//输出模板内容

function Show()

{

echo $this->content;

}

}

?>

以下是一个演示用的模板文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>{title}</title>

<link href="{cssfile}" rel="stylesheet" type="text/css" />

<meta http-equiv='Cache-Control' content='max-age=0'/>

<meta http-equiv='Cache-Control' content='no-cache' forua='true'/>

</head>

<body bgcolor="{bgcolor}">

<table width="100%" border="0" cellpadding="0" cellspacing="0">

<tr width="100%">

<td align="center">

<img src="{top_logo}" />

</td>

</tr>

<tr width="100%">

<td align="center">

{content}

</td>

</tr>

<tr width="100%">

<td align="center">

<select name="select">

{select}

<option value="{value}">{name}</option>

{/select}

</select>

</td>

</tr>

<tr width="100%">

<td align="center">

<img src="{bottom_img}" />

</td>

</tr>

</table>

{bottom}

</body>

</html>

以下是对模板调用的演示

<?php

$qt=new QuickTpl("template/pic_view.tpl");

$qt->Assign("title","标题"); //替换{title}

$qt->Assign("cssfile","style.css"); //替换{cssfile}

$qt->Assign("bgcolor","#EEEEEE"); //替换{bgcolor}

$qt->Assign("top_logo","logo.gif"); //替换{top_logo}

$select[]=array("name"=>"test1","value"=>"1");

$select[]=array("name"=>"test2","value"=>"2");

$select[]=array("name"=>"test3","value"=>"3");

$qt->BlockAssign("select",$select);

$qt->Assign("content","大家Hello~啊!");

$qt->Assign("bottom_img","bottom.gif"); //替换{bottom_img}

$qt->ASsign("bottom","copyright ..."); //替换{bottom}

$qt->Show(); //显示替换后的模板

?>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航