热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP模板解析类实例【PHP】

后端开发|php教程PHP,模板,解析类后端开发-php教程本文实例讲述了PHP模板解析类。分享给大家供大家参考。具体如下:新闻系统源码,杀毒软件ubuntu,爬虫个人网银,php

后端开发|php教程PHP模板解析类实例【PHP】
PHP,模板,解析类
后端开发-php教程
本文实例讲述了PHP模板解析类。分享给大家供大家参考。具体如下:
新闻 系统 源码,杀毒软件 ubuntu,爬虫个人网银,php postapi,酒泉优化seolzw

cOnf= &$conf; $this->template_c = $this->conf['template_config']['template_c'];//编译目录 $this->_tpl_suffix = $this->tpl_suffix(); } private function str_replace($search, $replace, $content) { if(empty($search) || empty($replace) || empty($content)) return false; return str_replace($search, $replace, $content); } /** * preg_match_all * @param $pattern 正则 * @param $content 内容 * @return array */ private function preg_match_all($pattern, $content) { if(empty($pattern) || empty($content)) core::show_error('查找模板标签失败!'); preg_match_all("/".$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']) ? $this->tpl_suffix : $this->conf['template_config']['template_suffix'] ; return $tpl_suffix; } /** * 此处不解释了 * @return */ public function assign($key, $value) { $this->vars[$key] = $value; } /** * 渲染页面 * @param * 使用方法 1 * $this->view->display('error', 'comm/'); * 默认是指向TPL模版的跟目录,所以comm/就是 tpl/comm/error.html * 使用方法 2 * $this->view->display('errorfile'); * 默认指向控制器固定的文件夹 * 例如你的域名是 http://heartphp/admin/index, 那么正确路径就是tpl/admin/index/errorfile.html * @return */ public function display($filename = '', $view_path = '') { $tpl_path_arr = $this->get_tpl($filename, $view_path);//获取TPL完整路径 并且向指针传送路径以及名称 if(!$tpl_path_arr) core::show_error($filename.$this->_tpl_suffix.'模板不存在'); //编译开始 $this->view_path_param = $view_path;//用户传递过来的模版跟目录 $this->compile(); } /** * 编译控制器 * @param * @return */ private function compile() { $filepath = $this->template_path.$this->template_name; $compile_dirpath = $this->check_temp_compile(); $vars_template_c_name = str_replace($this->_tpl_suffix, '', $this->template_name); $include_file = $this->template_replace($this->read_file($filepath), $compile_dirpath, $vars_template_c_name);//解析 if($include_file) { $this->read_config() && $cOnfig= $this->read_config(); extract($this->vars, EXTR_SKIP); [url=home.php?mod=space&uid=48608]@include[/url] $include_file; } } /** * 读取当前项目配置文件 */ protected function read_config() { if(file_exists(SYSTEM_PATH.'conf/config.php')) { @include SYSTEM_PATH.'conf/config.php'; return $config; } return false; } /** * 解析模板语法 * @param $str 内容 * @param $compile_dirpath 模版编译目录 * @param $vars_template_c_name 模版编译文件名 * @return 编译过的PHP模板文件名 */ private function template_replace($str, $compile_dirpath, $vars_template_c_name) { if(empty($str)) core::show_error('模板内容为空!'); //处理编译头部 $compile_path = $compile_dirpath.$vars_template_c_name.$this->tpl_compile_suffix;//编译文件 if(is_file($compile_path)) { //$header_cOntent= $this->get_compile_header($compile_path); //$compile_date = $this->get_compile_header_comment($header_content); $tpl_filemtime = filemtime($this->template_path.$this->template_name); $compile_filemtime = filemtime($compile_path); //echo $tpl_filemtime.'=='.date('Y-m-d H:i:s', $tpl_filemtime).'
'; //echo $compile_filemtime.'=='.date('Y-m-d H:i:s', $compile_filemtime); //如果文件过期编译 当模板标签有include并且有修改时 也重新编译 // 当修改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= "\r\n$str"; 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 如果默认直接 转成 */ private function parse_echo($content) { } /** * 转换为PHP * @param $content html 模板内容 * @return html 替换好的HTML */ private function parse_php($content){ if(empty($content)) return false; $cOntent= preg_replace("/".$this->template_tag_left."(.+?)".$this->template_tag_right."/is", "", $content); return $content; } /** * if判断语句 * * zhang * * liang * * zhangliang * */ 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模式的时候 每次执行删除模版编译文件 * 使用方法 * @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], '', $content); } return $content; } /** * 解析 foreach * 使用方法 * @param $content 模板内容 * @return html 解析后的内容 */ private function parse_foreach($content) { if(empty($content)) return false; //preg_match_all("/".$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) ? '$'.$key.' =>' : '' ; $s = ''; $cOntent= $this->str_replace($match[0][$match_key], $s, $content); } return $content; } /** * 匹配结束 字符串 */ private function parse_comm($content) { $search = array( "/".$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) ? $this->view_path_param : $this->get_tpl_path() ; $all_tpl_apth = $this->template_c.$tpl_path; if(!is_dir($all_tpl_apth)) { $this->create_dir($tpl_path); } return $all_tpl_apth; } /** * 读文件 * @param string $path 文件完整路径 * @return 模板内容 */ private function read_file($path) { //$this->check_file_limits($path, 'r'); if(($r = @fopen($path, 'r')) === false) { core::show_error('模版文件没有读取或执行权限,请检查!'); } $cOntent= fread($r, filesize($path)); fclose($r); return $content; } /** * 写文件 * @param string $filename 文件名 * @param string $content 模板内容 * @return 文件名 */ private function compile_file($filename, $content, $dir) { if(empty($filename)) core::show_error("{$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.'
编译文件失败,请检查文件权限.'); } //开启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}
没有写入权限,请检查."); } elseif(!is_readable($path) && $status == 'r') { core::show_error("{$path}
没有读取权限,请检查."); } elseif($status == 'rw') {//check wirte and read if(!is_writable($path) || !is_readable($path)) { core::show_error("{$path}
没有写入或读取权限,请检查"); } } } /** * 读取编译后模板的第一行 并分析成数组 * @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.'
读取文件失败,请检查文件权限!'); } 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.'
创建目录失败,请检查是否有可都写权限!'); } chmod($allpath, $mode); } else { $newdir = $allpath; } } return true; } public function __destruct(){ $this->vars = null; $this->view_path_param = null; }}

奇迹扩展源码,vscode 快捷加,ubuntu免费,tomcat网站日志,sqlite数据库vs,jquery 数字键盘插件,比较轻量级的前端框架,有什么驱赶爬虫的方法,php 判断用户名,seo优化论坛运营,win8网站取消导航,网页联机五子棋,织梦大气企业网站模板(扁平化风格)lzw

推荐阅读
  • PHPMailer邮件类邮件发送功能的使用教学及注意事项
    本文介绍了使用国外开源码PHPMailer邮件类实现邮件发送功能的简单教学,同时提供了一些注意事项。文章涵盖了字符集设置、发送HTML格式邮件、群发邮件以及避免类的重定义等方面的内容。此外,还提供了一些与PHP相关的资源和服务,如传奇手游游戏源码下载、vscode字体调整、数据恢复、Ubuntu实验环境搭建、北京爬虫市场、进阶PHP和SEO人员需注意的内容。 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 分享css中提升优先级属性!important的用法总结
    web前端|css教程css!importantweb前端-css教程本文分享css中提升优先级属性!important的用法总结微信门店展示源码,vscode如何管理站点,ubu ... [详细]
  • PHP函数实现分页含文本分页和数字分页【PHP】
    后端开发|php教程PHP,分页后端开发-php教程最近,在项目中要用到分页。分页功能是经常使用的一个功能,所以,对其以函数形式进行了封装。影视网源码带充值系统,vscode配置根 ... [详细]
  • mui框架offcanvas侧滑超出部分隐藏无法滚动如何解决
    web前端|js教程off-canvas,部分,超出web前端-js教程mui框架中off-canvas侧滑的一个缺点就是无法出现滚动条,因为它主要用途是设置类似于qq界面的那种格 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • Oracle 和 mysql的9点区别【MySQL】
    数据库|mysql教程oracle,Oracle,money,mysql,coun数据库-mysql教程1.组函数用法规则mysql中组函数在select语句中可以随意使用,但在o ... [详细]
  • ORACLE空间管理实验5:块管理之ASSM下高水位的影响
    数据库|mysql教程ORACLE,空间,管理,实验,ASSM,下高,水位,影响,数据库-mysql教程易语言黑客软件源码,vscode左侧搜索,ubuntu怎么看上一页,ecs搭 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 本文介绍了在Ubuntu 11.10 x64环境下安装Android开发环境的步骤,并提供了解决常见问题的方法。其中包括安装Eclipse的ADT插件、解决缺少GEF插件的问题以及解决无法找到'userdata.img'文件的问题。此外,还提供了相关插件和系统镜像的下载链接。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • MySQL中的MVVC多版本并发控制机制的应用及实现
    本文介绍了MySQL中MVCC的应用及实现机制。MVCC是一种提高并发性能的技术,通过对事务内读取的内存进行处理,避免写操作堵塞读操作的并发问题。与其他数据库系统的MVCC实现机制不尽相同,MySQL的MVCC是在undolog中实现的。通过undolog可以找回数据的历史版本,提供给用户读取或在回滚时覆盖数据页上的数据。MySQL的大多数事务型存储引擎都实现了MVCC,但各自的实现机制有所不同。 ... [详细]
  • 背景应用安全领域,各类攻击长久以来都危害着互联网上的应用,在web应用安全风险中,各类注入、跨站等攻击仍然占据着较前的位置。WAF(Web应用防火墙)正是为防御和阻断这类攻击而存在 ... [详细]
author-avatar
谁的FrankyCH
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有