加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_宿迁站长网 (https://www.0527zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php格式书写,php模板简单写法

发布时间:2022-11-01 15:00:53 所属栏目:PHP教程 来源:转载
导读: 这是一个简单的用php 实现的模板教程物。
class TEMPLATE
{
private $path = "." ; #根目录
private $var;
private $tplDir = "template"; #模板存储目录
private $tplExt = "tpl"; #模板文

这是一个简单的用php 实现的模板教程物。

class TEMPLATE

{

private $path = "." ; #根目录

private $var;

private $tplDir = "template"; #模板存储目录

private $tplExt = "tpl"; #模板文件的后缀名

private $tplId = 0 ; #模板的ID号

private $compileDir = "template_c"; #编译后的php文件存放目录

private $isCache=false ; #是否用缓存 (默认不启动)

private $cacheId = 1; #缓存文件ID号

private $cacheLeftTime=3600; #缓存有效期 (默认保存3600秒)

private $cacheDir = "cache"; #缓存文件存储目录

private $autoRefresh = false ; #是否自动刷新

private $pattern = array(

"/({dw:)s*includes*filename=s*"(. .. )s*"s*(/})/i",#包含文件

"/({dw:)s*field.(. )s*(/})/i",#局部变量

"/({dw:)s*global.(. )s*(/})/i",#全局变量

"/({dw:)s*foreachs*(. )s*ass*(. )s*(/})/i",#foreach 语句

"/({dw:)s*ends*foreachs*(/})/i", #foreach 结束

"/({dw:)s*ifs*((. ))(/})/i",

"/({dw:)s*elseifs*((. ))(/})/i",

"/({dw:)s*elses*(/})/i",

"/({dw:)s*ends*ifs*(/})/i",

);

private $replacement = array(

'',

"",

"",

"",

"",

"" ,

"",

"",

"",

);

#构造函数

function __construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="")

{

$this->path = $path ? $path : $this->path ;

$this->tplDir = $tplDir ? $tplDir : $this->tplDir ;

$this->compileDir = $compileDir ? $compileDir : $this->compileDir ;

$this->isCache = is_bool($isCache) ? $isCache : $this->isCache ;

$this->cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime ;

$this->cacheDir = $cacheDir ? $cacheDir : $this->cacheDir ;

$this->autoRefresh = is_bool($autoRefresh) ? $autoRefresh : $this->autoRefresh ;

}

#兼容php4

function TEMPLATE($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="")

{

$this->__construct($path = "", $tplDir="", $compileDir="",$isCache="",$cacheLeftTime="",$cacheDir="" ,$autoRefresh="");

}

function __get($property)

{

return $this->$property ;

}

function __set($property,$value)

{

return $this->$property = $value ;

}

#给模板中的变量赋值

# $tplVal 模板中的变量名

function assign($tplVal ,$value="")

{

if (is_array($tplVal))

{

foreach ($tplVal as $key => $val)

{

if (!empty($key))

$this->var[$key] = $val ;

}

}

else {

if (!empty($tplVal))

$this->var[$tplVal] = $value ;

}

}

#输出文件内容函数

function display($tplFile,$tplId=0,$cacheId = 1,$cacheLeftTime="")

{

if (empty($tplFile)) die("Template "{$tplFile}" not exist !");

$this->cacheId = $cacheId ? md5($cacheId) : md5($this->cacheId);

$cacheFile = $this->path. "/".$this->cacheDir."/".$tplFile.$this->cacheId ;

if ($this->check_cache($cacheFile,$cacheLeftTime)) #当缓存文件存在且不过期时直接从缓存文件读取内容

{

echo $this->read_file($cacheFile);

}else {

$tpl = $this->path."/".$this->tplDir."/".$tplFile.".".$this->tplExt;

$tplContent = $this->read_file($tpl); #读取模板文件的内容

$compileContent= $this->compile_file($tplContent); #对读取出来的文件进行编译

$this->tplId = $tplId ? $tplId : $this->tplId ;

$compileFile = $this->path."/".$this->compileDir."/".md5($this->tplId)."".$tplFile.".php";

$this->write_file($compileFile,$compileContent);#将编译后的内容写入相应的文件中;

@extract($this->var);

ob_start();

include_once($compileFile);

$content = ob_get_contents() ;

ob_end_clean() ;

if ($this->isCache){

$this->write_file($cacheFilephp格式,$content) ;# 帮编译好的内容写入缓存文件

}

echo $content ;

}

}

/* function trim_tag($content)

{

$content = str_replace($this->startTag,"",$content);

$content = str_replace($this->endTag,"",$content);

//$content = trim($content);

return $content ;

}*/

# 编译文件函数

function compile_file($content=null)

{

$content = $content ? $content :die("Compile fail!") ;

//$content = $this->trim_tag($content);

$content = preg_replace($this->pattern,$this->replacement,$content);

return $content;

}

#解析包含文件

function inc_file($filename,$tplId="",$cacheId="",$cacheLeftTime="")

{

$file = $this->path."/".$this->tplDir."/".$filename ;

if (file_exists($file))

{

$filename = str_replace(".".$this->tplExt,"",$filename);

return $this->display($filename,$tplId,$cacheId,$cacheLeftTime);

}

else die("Template "{$filename}" not exist");

}

#读取文件内容函数

function read_file($filename)

{

if (!file_exists($filename)) die("Read file fail") ;

return file_get_contents($filename);

}

php 脚本格式_php格式_php word格式

#内容写入函数

function write_file($filename,$content,$mode="wb")

{

$filename = trim($filename);

$content = $content ? stripslashes(trim($content)) : exit();

if (!file_exists($filename))

{

$array = explode("/",$filename);

$count = count($array);

$path = "";

for ($i = 0 ; $i

{

if(!file_exists($path .= $array[$i]."/"))

{

mkdir($path,0777);

}

}

}

$handle = fopen($filename,$mode) ;

fwrite($handle,$content);

fclose($handle);

return true;

}

# 清除缓存

function clear_dir($dir="")

{

$dir = $this->path."/".$dir;

$handle = opendir($dir);

if (file_exists($dir))

{

while ($file = readdir($handle))

{

if ($file !="." && $file != "..")

unlink($dir."/".$file);

}

closedir($handle);

return true;

}

else {

return false;

}

}

#清除所有缓存

function clear_all_cache()

{

if ($this->clear_dir($this->cacheDir) && $this->clear_dir($this->compileDir))

return true;

}

#检查缓存是否过期

function check_cache($cacheFile,$cacheLeftTime="")

{

$cacheLeftTime = $cacheLeftTime ? $cacheLeftTime : $this->cacheLeftTime;

if (!file_exists($cacheFile)) return false ;

$time = $this->get_time($cacheFile) $cacheLeftTime ;

if ($time

{

unlink($cacheFile);

return false;

}

return true;

}

# 获取文件最后编辑时间

function get_time($filename)

{

if (!file_exists($filename)) return false;

return filemtime($filename);

}

}

(编辑:云计算网_宿迁站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!