SimpleT一个简单易用的模板类
时间:2008-10-20
来源:互联网
很早以前学过smarty,那时学的时候还要拿着本手册才可以使用它!可见他的复杂性!
本人为了使模板引擎变成一个只是实现数据和界面分离的工具,而不是一个庞大且复杂的语言努力着!!!
下面我发我的SimpleT,大家可以看到他真的很简单,不过我觉得这样已经足够了,编程的问题就交给程序员吧,不要让美工员来做了!!
[php]
<?php
class SimpleT {
private $t_vars;
private $templates_dir;
private $templates_c_dir;
public function __construct() {
$this->templates_dir = './templates/';
$this->templates_c_dir = './templates_c/';
}
public function setDir($dir, $comp = false) {
if(is_dir($dir)) {
if($comp == false)
$this->templates_dir = rtrim($dir, '/').'/';
else
$this->templates_c_dir = rtrim($dir, '/').'/';
return true;
} else {
return false;
}
}
public function assign($var, $value = NULL) {
if (is_array($var)) {
foreach ($var as $key => $val) {
$this->t_vars[$key] = $val;
}
} else {
$this->t_vars[$var] = $value;
}
}
private function comp($filename) {
try {
if(!$fp = fopen($filename, 'r')) {
throw new Exception('Can not open ' . $filename);
}
$filesize = filesize($filename);
if($filesize <= 0) {
throw new Exception('The file size must > 0 ' );
}
$content = fread($fp, $filesize);
fclose($fp);
unset($fp);
$content = preg_replace("/<%=([a-zA-Z0-9_]{1,})%>/","<?php echo \\$$1 ;?>", $content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\.loop%>/", "<?php foreach(\\$$1 as \\$$1_key => \\$$1_val) { ?>",$content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\.key%>/", "<?php echo \\$$1_key ;?>", $content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\.value%>/", "<?php echo \\$$1_val ;?>", $content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\\?%>/", "<?php if(\\$$1 == true) { ?>", $content);
$content = preg_replace("/<%end%>/","<?php } ?>", $content);
if (preg_match_all("/<%{([^(}%>)]{1,})}%>/", $content, $files)) {
$this->comp($this->templates_dir . $files[1][0]);
}
$content = preg_replace("/<%{([^(}%>)]{1,})}%>/", "<?php include '{$this->templates_c_dir}simplet_comp_$1'; ?>", $content);
$fp = fopen($this->templates_c_dir . 'simplet_comp_' . basename($filename), 'w');
if(!fwrite($fp, $content)) {
throw new Exception('Can not write content in the ' . $filename);
}
fclose($fp);
} catch (Exception $e) {
echo $e->getMessage();
}
return true;
}
public function display($filename) {
$filename = $this->templates_dir . $filename;
if(!file_exists($filename)) {
return false;
}
$t_filename_c = $this->templates_c_dir . 'simplet_comp_' . basename($filename);
if(!file_exists($t_filename_c) || filemtime($t_filename_c) < filemtime($filename)) {
$this->comp($filename);
}
foreach ($this->t_vars as $key => $val) {
$$key = $val;
}
include($t_filename_c);
}
}
?>
[/php]
例子1:打印变量
PHP代码:
<?php
require_once('SimpleT.php');
$t = new SimpleT();
$t->assign('name', 'song');
$t->display('index.php');
模板文件:index.php
<%=name%>
例子2:循环数组
<?php
require_once('SimpleT.php');
$t = new SimpleT();
$t->assign('arrays', array('1','2','3'));
$t->display('index.php');
模板文件:index.php
<%arrays.loop%>
数组的key是:<%arrays.key%>
数组的值是:<%arrays.value%>
<%end%>
例子3:判断条件
<?php
require_once('SimpleT.php');
$t = new SimpleT();
$t->assign('condition',false);
$t->display('index.php');
模板文件:index.php
<%condition?%>
<h1>条件为真</h1>
<%end%>
例子4:包含文件
<%{filename.php}%>
有疑问可以发站内短信息给我!!!
本人为了使模板引擎变成一个只是实现数据和界面分离的工具,而不是一个庞大且复杂的语言努力着!!!
下面我发我的SimpleT,大家可以看到他真的很简单,不过我觉得这样已经足够了,编程的问题就交给程序员吧,不要让美工员来做了!!
[php]
<?php
class SimpleT {
private $t_vars;
private $templates_dir;
private $templates_c_dir;
public function __construct() {
$this->templates_dir = './templates/';
$this->templates_c_dir = './templates_c/';
}
public function setDir($dir, $comp = false) {
if(is_dir($dir)) {
if($comp == false)
$this->templates_dir = rtrim($dir, '/').'/';
else
$this->templates_c_dir = rtrim($dir, '/').'/';
return true;
} else {
return false;
}
}
public function assign($var, $value = NULL) {
if (is_array($var)) {
foreach ($var as $key => $val) {
$this->t_vars[$key] = $val;
}
} else {
$this->t_vars[$var] = $value;
}
}
private function comp($filename) {
try {
if(!$fp = fopen($filename, 'r')) {
throw new Exception('Can not open ' . $filename);
}
$filesize = filesize($filename);
if($filesize <= 0) {
throw new Exception('The file size must > 0 ' );
}
$content = fread($fp, $filesize);
fclose($fp);
unset($fp);
$content = preg_replace("/<%=([a-zA-Z0-9_]{1,})%>/","<?php echo \\$$1 ;?>", $content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\.loop%>/", "<?php foreach(\\$$1 as \\$$1_key => \\$$1_val) { ?>",$content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\.key%>/", "<?php echo \\$$1_key ;?>", $content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\.value%>/", "<?php echo \\$$1_val ;?>", $content);
$content = preg_replace("/<%([a-zA-Z0-9_]{1,})\\?%>/", "<?php if(\\$$1 == true) { ?>", $content);
$content = preg_replace("/<%end%>/","<?php } ?>", $content);
if (preg_match_all("/<%{([^(}%>)]{1,})}%>/", $content, $files)) {
$this->comp($this->templates_dir . $files[1][0]);
}
$content = preg_replace("/<%{([^(}%>)]{1,})}%>/", "<?php include '{$this->templates_c_dir}simplet_comp_$1'; ?>", $content);
$fp = fopen($this->templates_c_dir . 'simplet_comp_' . basename($filename), 'w');
if(!fwrite($fp, $content)) {
throw new Exception('Can not write content in the ' . $filename);
}
fclose($fp);
} catch (Exception $e) {
echo $e->getMessage();
}
return true;
}
public function display($filename) {
$filename = $this->templates_dir . $filename;
if(!file_exists($filename)) {
return false;
}
$t_filename_c = $this->templates_c_dir . 'simplet_comp_' . basename($filename);
if(!file_exists($t_filename_c) || filemtime($t_filename_c) < filemtime($filename)) {
$this->comp($filename);
}
foreach ($this->t_vars as $key => $val) {
$$key = $val;
}
include($t_filename_c);
}
}
?>
[/php]
例子1:打印变量
PHP代码:
<?php
require_once('SimpleT.php');
$t = new SimpleT();
$t->assign('name', 'song');
$t->display('index.php');
模板文件:index.php
<%=name%>
例子2:循环数组
<?php
require_once('SimpleT.php');
$t = new SimpleT();
$t->assign('arrays', array('1','2','3'));
$t->display('index.php');
模板文件:index.php
<%arrays.loop%>
数组的key是:<%arrays.key%>
数组的值是:<%arrays.value%>
<%end%>
例子3:判断条件
<?php
require_once('SimpleT.php');
$t = new SimpleT();
$t->assign('condition',false);
$t->display('index.php');
模板文件:index.php
<%condition?%>
<h1>条件为真</h1>
<%end%>
例子4:包含文件
<%{filename.php}%>
有疑问可以发站内短信息给我!!!
作者: liexusong 发布时间: 2008-10-20
不错,支持一下。
不过想用简洁点的模板引擎的话也有很多啊,我不太愿意自己写这样的类。

不过想用简洁点的模板引擎的话也有很多啊,我不太愿意自己写这样的类。
作者: lizard21 发布时间: 2008-10-22
很多好的模板引擎,但同时不分存在不足,比如变量在循环中的作用域问题等
作者: pylong 发布时间: 2008-10-22
自己写也有自己写的好处!就是不用学就可以用了!!!!!!!!!!!
作者: liexusong 发布时间: 2008-10-22
楼主自己写的?
自己写当然有自己的好处,至少让自己知道模板引擎的工作原理,学别的也是容易简单
自己写当然有自己的好处,至少让自己知道模板引擎的工作原理,学别的也是容易简单
作者: pylong 发布时间: 2008-10-22
嗯!!!是我自己写的!有兴趣可以大家一起研究一下的!!!!!!!我随时欢迎的!
作者: liexusong 发布时间: 2008-10-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28