<?php
class templete
{
var $fileStr="" ;//html文件内容
var $beforeblockTemplate = "";//始终保存没有替换的结果
var $afterblockTemplate = "";//存储循环改变替换后的结果
var $blockTemplate = "";
//$htmlpath:设置html文件和文件路径
function templete($htmlpath="")
{
if($htmlpath=="")
{
$path=$_SERVER['DOCUMENT_ROOT'];
$filename=$_SERVER['SCRIPT_NAME'];
$htmlfilename=str_replace("php","html",$filename);
$htmlfilepath=$path."/html".$htmlfilename;
$str=@file_get_contents($htmlfilepath);
if($str==false)
{
$this->fileStr="没有找到模板";
}else
{
$this->fileStr=$str;
}
}else
{
$str=file_get_contents($htmlpath);
if($str==false)
{
$this->fileStr="没有找到模板";
}else
{
$this->fileStr=$str;
}
}
}
//设置其值
function SETVAR($var,$value)
{
$this->fileStr= str_replace("VAR_PHP{".$var."}",$value,$this->fileStr);
}
//设置循环-->获取循环
function processBlock($BlockName)
{
$reg = "/<!--\\s+BEGIN {$BlockName}\\s+-->(.*)\n\\s*<!--\\s+END {$BlockName}\\s+-->/sm";
preg_match_all( $reg, $this->fileStr,$Result );
$this->beforeblockTemplate=trim($Result[1][0]);//要重复的内容
$this->afterblockTemplate=trim($Result[1][0]);//要重复的内容
}
//设置循环变量
function setBvar($var,$value)
{
$this->afterblockTemplate = str_replace("VAR_PHP{".$var."}",$value,$this->afterblockTemplate);
}
//处理循环变量
function parseBlock()
{
$this->blockTemplate.=$this->afterblockTemplate;
$this->afterblockTemplate=$this->beforeblockTemplate;
}
//循环结束
function blockfiash($BlockName)
{
$reg = "/<!--\\s+BEGIN {$BlockName}\\s+-->(.*)\n\\s*<!--\\s+END {$BlockName}\\s+-->/sm";
$this->fileStr=preg_replace($reg,$this->blockTemplate,$this->fileStr);
$this->afterblockTemplate="";
$this->beforeblockTemplate="";
$this->blockTemplate="";
$this->fileStr = preg_replace("/<!--\\s+BEGIN {$BlockName}\\s+-->(.*)\n\\s*<!--\\s+END {$BlockName}\\s+-->/sm","", $this->fileStr);//处理未用到的变量
}
//对结果进行输出
function OUT()
{
$this->fileStr = preg_replace("/VAR_PHP{\w+\\}/","", $this->fileStr);//处理未用到的变量
echo($this->fileStr);
}
}
?>