蝙蝠岛资源网 Design By www.hbtsch.com
本文实例讲述了PHP模板解析类。分享给大家供大家参考。具体如下:
<"/".$this->template_tag_left.$pattern.$this->template_tag_right."/is", $content, $match);
return $match;
}
/**
* 模板文件后缀
*/
public function tpl_suffix() {
$tpl_suffix = empty($this->conf['template_config']['template_suffix']) "public/left.html"}> 当修改include里的文件,非DEBUG模式时 如果不更改主文件 目前是不重新编译include里的文件,我在考虑是否也要更改,没想好,暂时这样,所以在开发阶段一定要开启DEBUG=1模式 要不然修改include文件无效 。 有点罗嗦,不知道表述清楚没
if($tpl_filemtime > $compile_filemtime || DEBUG) {
$ret_file = $this->compile_file($vars_template_c_name, $str, $compile_dirpath);
} else {
$ret_file = $compile_path;
}
} else {//编译文件不存在 创建他
$ret_file = $this->compile_file($vars_template_c_name, $str, $compile_dirpath);
}
return $ret_file;
}
/**
* 模板文件主体
* @param string $str 内容
* @return html
*/
private function body_content($str) {
//解析
$str = $this->parse($str);
$header_comment = "Create On##".time()."|Compiled from##".$this->template_path.$this->template_name;
$content = "<";
return $content;
}
/**
* 开始解析相关模板标签
* @param $content 模板内容
*/
private function parse($content) {
//foreach
$content = $this->parse_foreach($content);
//include
$content = $this->parse_include($content);
//if
$content = $this->parse_if($content);
//elseif
$content = $this->parse_elseif($content);
//模板标签公用部分
$content = $this->parse_comm($content);
//转为PHP代码
$content = $this->parse_php($content);
return $content;
}
/**
* echo 如果默认直接<{$config['domain']}> 转成 <"/".$this->template_tag_left."(.+".$this->template_tag_right."/is", "<", $content);
return $content;
}
/**
* if判断语句
* <{if empty($zhang)}>
* zhang
* <{elseif empty($liang)}>
* liang
* <{else}>
* zhangliang
* <{/if}>
*/
private function parse_if($content) {
if(empty($content)) return false;
//preg_match_all("/".$this->template_tag_left."if\s+(.*".$this->template_tag_right."/is", $content, $match);
$match = $this->preg_match_all("if\s+(.*", $content);
if(!isset($match[1]) || !is_array($match[1])) return $content;
foreach($match[1] as $k => $v) {
//$s = preg_split("/\s+/is", $v);
//$s = array_filter($s);
$content = str_replace($match[0][$k], "<", $content);
}
return $content;
}
private function parse_elseif($content) {
if(empty($content)) return false;
//preg_match_all("/".$this->template_tag_left."elseif\s+(.*".$this->template_tag_right."/is", $content, $match);
$match = $this->preg_match_all("elseif\s+(.*", $content);
if(!isset($match[1]) || !is_array($match[1])) return $content;
foreach($match[1] as $k => $v) {
//$s = preg_split("/\s+/is", $v);
//$s = array_filter($s);
$content = str_replace($match[0][$k], "<", $content);
}
return $content;
}
/**
* 解析 include include标签不是实时更新的 当主体文件更新的时候 才更新标签内容,所以想include生效 请修改一下主体文件
* 记录一下 有时间开发一个当DEBUG模式的时候 每次执行删除模版编译文件
* 使用方法 <{include file="www.phpddt.com"}>
* @param $content 模板内容
* @return html
*/
private function parse_include($content) {
if(empty($content)) return false;
//preg_match_all("/".$this->template_tag_left."include\s+(.*".$this->template_tag_right."/is", $content, $match);
$match = $this->preg_match_all("include\s+(.*", $content);
if(!isset($match[1]) || !is_array($match[1])) return $content;
foreach($match[1] as $match_key => $match_value) {
$a = preg_split("/\s+/is", $match_value);
$new_tag = array();
//分析元素
foreach($a as $t) {
$b = explode('=', $t);
if(in_array($b[0], $this->tag_include)) {
if(!empty($b[1])) {
$new_tag[$b[0]] = str_replace("\"", "", $b[1]);
} else {
core::show_error('模板路径不存在!');
}
}
}
extract($new_tag);
//查询模板文件
foreach($this->conf['view_path'] as $v){
$conf_view_tpl = $v.$file;//include 模板文件
if(is_file($conf_view_tpl)) {
$c = $this->read_file($conf_view_tpl);
$inc_file = str_replace($this->_tpl_suffix, '', basename($file));
$this->view_path_param = dirname($file).'/';
$compile_dirpath = $this->check_temp_compile();
$include_file = $this->template_replace($c, $compile_dirpath, $inc_file);//解析
break;
} else {
core::show_error('模板文件不存在,请仔细检查 文件:'. $conf_view_tpl);
}
}
$content = str_replace($match[0][$match_key], '<"'.$include_file.'")"/".$this->template_tag_left."foreach\s+(.*".$this->template_tag_right."/is", $content, $match);
$match = $this->preg_match_all("foreach\s+(.*", $content);
if(!isset($match[1]) || !is_array($match[1])) return $content;
foreach($match[1] as $match_key => $value) {
$split = preg_split("/\s+/is", $value);
$split = array_filter($split);
$new_tag = array();
foreach($split as $v) {
$a = explode("=", $v);
if(in_array($a[0], $this->tag_foreach)) {//此处过滤标签 不存在过滤
$new_tag[$a[0]] = $a[1];
}
}
$key = '';
extract($new_tag);
$key = ($key) "/".$this->template_tag_left."\/foreach".$this->template_tag_right."/is",
"/".$this->template_tag_left."\/if".$this->template_tag_right."/is",
"/".$this->template_tag_left."else".$this->template_tag_right."/is",
);
$replace = array(
"<",
"<",
"<"
);
$content = preg_replace($search, $replace, $content);
return $content;
}
/**
* 检查编译目录 如果没有创建 则递归创建目录
* @param string $path 文件完整路径
* @return 模板内容
*/
private function check_temp_compile() {
//$paht = $this->template_c.
$tpl_path = ($this->view_path_param) "{$filename} Creation failed");
$content = $this->body_content($content);//对文件内容操作
//echo '开始编译了=====';
$f = $dir.$filename.$this->tpl_compile_suffix;
//$this->check_file_limits($f, 'w');
if(($fp = @fopen($f, 'wb')) === false) {
core::show_error($f.'<br/>编译文件失败,请检查文件权限.');
}
//开启flock
flock($fp, LOCK_EX + LOCK_NB);
fwrite($fp, $content, strlen($content));
flock($fp, LOCK_UN + LOCK_NB);
fclose($fp);
return $f;
}
/**
* 这个检查文件权限函数 暂时废弃了
* @param [$path] [路径]
* @param [status] [w=write, r=read]
*/
public function check_file_limits($path , $status = 'rw') {
clearstatcache();
if(!is_writable($path) && $status == 'w') {
core::show_error("{$path}<br/>没有写入权限,请检查.");
} elseif(!is_readable($path) && $status == 'r') {
core::show_error("{$path}<br/>没有读取权限,请检查.");
} elseif($status == 'rw') {//check wirte and read
if(!is_writable($path) || !is_readable($path)) {
core::show_error("{$path}<br/>没有写入或读取权限,请检查");
}
}
}
/**
* 读取编译后模板的第一行 并分析成数组
* @param string $filepath 文件路径
* @param number $line 行数
* @return 返回指定行数的字符串
*/
/*
private function get_compile_header($filepath, $line = 0) {
if(($file_arr = @file($filepath)) === false) {
core::show_error($filepath.'<br/>读取文件失败,请检查文件权限!');
}
return $file_arr[0];
}
*/
/**
* 分析头部注释的日期
* @param string $cotnent 编译文件头部第一行
* @return 返回上一次日期
*/
/*
private function get_compile_header_comment($content) {
preg_match("/\/\*(.*", $content, $match);
if(!isset($match[1]) || empty($match[1])) core::show_error('编译错误!');
$arr = explode('|', $match[1]);
$arr_date = explode('##', $arr[0]);
return $arr_date[1];
}
*/
/**
* 获取模板完整路径 并返回已存在文件
* @param string $filename 文件名
* @param string $view_path 模板路径
* @return
*/
private function get_tpl($filename, $view_path) {
empty($filename) && $filename = $this->tpl_name;
//遍历模板路径
foreach($this->conf['view_path'] as $path) {
if($view_path) {//直接从tpl跟目录找文件
$tpl_path = $path.$view_path;
$view_file_path = $tpl_path.$filename.$this->_tpl_suffix;
} else {//根据目录,控制器,方法开始找文件
$view_file_path = ($tpl_path = $this->get_tpl_path($path)) ? $tpl_path.$filename.$this->_tpl_suffix : exit(0);
}
if(is_file($view_file_path)) {
//向指针传送模板路径和模板名称
$this->template_path = $tpl_path;//
$this->template_name = $filename.$this->_tpl_suffix;
return true;
} else {
core::show_error($filename.$this->_tpl_suffix.'模板不存在');
}
}
}
/**
* 获取模板路径
* @param string $path 主目录
* @return URL D和M的拼接路径
*/
private function get_tpl_path($path = '') {
core::get_directory_name() && $path_arr[0] = core::get_directory_name();
core::get_controller_name() && $path_arr[1] = core::get_controller_name();
(is_array($path_arr)) ? $newpath = implode('/', $path_arr) : core::show_error('获取模板路径失败!') ;
return $path.$newpath.'/';
}
/**
* 创建目录
* @param string $path 目录
* @return
*/
private function create_dir($path, $mode = 0777){
if(is_dir($path)) return false;
$dir_arr = explode('/', $path);
$dir_arr = array_filter($dir_arr);
$allpath = '';
$newdir = $this->template_c;
foreach($dir_arr as $dir) {
$allpath = $newdir.'/'.$dir;
if(!is_dir($allpath)) {
$newdir = $allpath;
if(!@mkdir($allpath, $mode)) {
core::show_error( $allpath.'<br/>创建目录失败,请检查是否有可都写权限!');
}
chmod($allpath, $mode);
} else {
$newdir = $allpath;
}
}
return true;
}
public function __destruct(){
$this->vars = null;
$this->view_path_param = null;
}
}
希望本文所述对大家的php程序设计有所帮助。
标签:
PHP,模板,解析类
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无PHP模板解析类实例的评论...
更新日志
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]