45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:php的简单模板类的详细介绍

php的简单模板类的详细介绍

2016-09-04 18:21:30 来源:www.45fan.com 【

php的简单模板类的详细介绍

我就喜欢简单:)

<?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(); //显示替换后的模板

?>

 

本文地址:http://www.45fan.com/a/question/72340.html
Tags: PHP 简单 模板
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部