- 5
- 0
- 约1.03万字
- 约 17页
- 2017-12-22 发布于河南
- 举报
自己写的php模板引擎
自己写的php模板引擎
自己写的php模板引擎
2010-01-25 093327 分类: php 字号 订阅
一直在用smarty,总觉得它过于臃肿。所以自己写了一个。
在跟目录建index.php文件,内容
php
header ( Content-Typetexthtml;charset=utf-8 );
网站跟目录
define ( ROOT_PATH , $_SERVER [DOCUMENT_ROOT].zero );
存放模板文件夹
define(ZR_DIR , ROOT_PATH.templates);
编译文件夹
define(ZR_C_DIR , ROOT_PATH.templates_c);
require ROOT_PATH.classzero.class.php;
$ZR = new Zero();
$ZR-assign(name,潘泽学的模板);
$ZR-assign(if,true);
$array = array(1=1,2=2);
$ZR-assign(array,$array);
$ZR-debug(index.html);
$ZR-display(index.html);
在跟目录建templates文件夹,再在templates文件夹内建index.html文件,内容
html
head
titletitle
meta content=
stylestyle
head
body
{$name}
{#}........{#}
{if $if}
div当$if等于true时会显示这句话div
{if}
foreach语句br
{$array-foreach(key,value)}
{@key}={@value}br
{foreach}
body
html
在跟目录建templates_c文件夹,无内容但此文件夹要有写权限。
在跟目录建class文件夹。
在class文件夹内建zero.class.php文件,内容
php
class Zero{
保存模板变量
private $_zr_vars;
保存模板文件名
private $_zr_file;
保存ZoreParser对象(编译对象)
private $_parser;
保存ZoreDebug对象(调试对象)
private $_debugger;
构造函数
public function __construct(){
判断模板文件夹和编译文件夹是否存在
if(!is_dir(ZR_DIR) !is_dir(ZR_C_DIR)){
exit(错误请正确设置模板文件夹和编译文件夹);
}
}
设置模板变量
public function assign($var,$value){
判断$var是否有值
if(isset($var) trim($var) != ){
$this-_zr_vars[$var] = $value;
return true;
} else {
exit(错误请设置变量名);
}
}
检测模板是否更新,更新则再编译,没有更新就用原来编译后的文件
public function display($zr_file){
$template_file = ZR_DIR.$zr_file;
判断模板文件是否存在
if(!file_exists($template_file)){
exit(错误模板文件不存在);
}
$parsed_file = ZR_C_DIR.md5($zr_file)..php;
if(!file_exists($parsed_file) filemtime($parsed_file) filemtime($template_file)){
if(TRUE){
require_once ROOT_PATH.classzero_parser.class.php;
$this-_parser = new ZeroParser();
$this-_parser-compile($zr_file);
}
include $parsed_file;
}
public function debug($zr_file){
if(include_once(ROOT_PATH.classzero_debugger.class.php)){
$this-_debugger = new ZeroDebuger($zr_file);
$this-_debugger-start();
} else {
exit(错误Debuger类文件不存在);
原创力文档

文档评论(0)