<?php
/*
* Created on 2011-10-08
* @author xyl
* This is simple template engine for your site
*/
class XT{
var $folder="template";//模板文件目录
var $t_type=".html";//模板文件类型
var $data=array();
var $subtp=array();//字模板
var $t_cache="./tcache";//模板缓存目录
var $compile=false;//调试
var $content_path="";
function XT(){
if(!file_exists($this->t_cache)){
mkdir($this->t_cache) or die("Failed to create cache directory:".$this->t_cache);
}
}
function setdata($k,$v){
$this->data[$k]=$v;
}
function subtemplate($filename){
if(is_array($filename)){
foreach($filename as $v){
$this->parse($v);
}
}else{
$this->parse($filename);
}
}
function __parsedata($content){
//模板包含
$this->subtp=array();
preg_match_all("/<!--\s+include\((.+)\)\s+-->/iu",$content,$rt);
if(!empty($rt[1][0])){
foreach($rt[1] as $v){
if(!empty($v)){
$this->subtp[$v]=$v;
}
}
if(!empty($this->subtp)){
$this->subtemplate($this->subtp,true);
}
}
$content = preg_replace("/<!--\s+include\((.+)\)\s+-->/iu", "<?php include('$this->t_cache/\\1.php');?>", $content);
//变量
$content = preg_replace("/\{(\\\$[a-z0-9_]+)\}/s", "<?=\\1?>", $content);
//逻辑
$content = preg_replace("/<!--\s+if\((.+)\):\s+-->/iu", "<?php if(\\1) { ?>", $content);
$content = preg_replace("/<!--\s+endif\s+-->/iu", "<?php } ?>", $content);
//循环
$content = preg_replace("/<!--\s+foreach\((.+)\):\s+-->/iu", "<?php foreach(\\1) { ?>", $content);
$content = preg_replace("/<!--\s+endforeach\s+-->/iu", "<?php } ?>", $content);
return $content;
}
function parse($filename=null,$sub=false){
$filename=(empty($filename)?basename($_SERVER["PHP_SELF"],".php"):$filename);
$content_path=$this->t_cache."/".$filename.".php";
if(!file_exists($content_path)||$this->compile){
$content=implode("",file($this->folder.'/'.$filename.$this->t_type));
$content=$this->__parsedata($content);
file_put_contents($content_path,$content);
}
!$sub?$this->content_path=$content_path:'';
}
function out(){
extract($this->data);
include_once($this->content_path);
}
}
?>
没有合适的资源?快使用搜索试试~ 我知道了~
一个最轻量级的PHP模板引擎
共14个文件
php:6个
html:4个
25304~:1个
4星 · 超过85%的资源 需积分: 17 87 下载量 154 浏览量
2011-10-09
14:02:34
上传
评论 1
收藏 3KB RAR 举报
温馨提示
PHP模板引擎很多,但要么很庞大,要么效率很低,如果只是要实现PHP代码与程序分离,就只要一个简单的模板引擎就行了。今天抽空写了一个。
资源推荐
资源详情
资源评论
收起资源包目录
xt.rar (14个子文件)
xt.class.php 2KB
template
test.html 310B
.tmp_header.html.2659~ 14B
sub.html 29B
footer.html 38B
header.html 14B
.tmp_test.html.25304~ 311B
.tmp_footer.html.72220~ 38B
.tmp_sub.html.26562~ 3B
test.php 451B
tcache
footer.php 56B
sub.php 44B
test.php 333B
header.php 17B
共 14 条
- 1
xylweb
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页