创建TPL自定义模板
文件布局

<!--1d7c7a527b6335cc7a623305ca940e1findex.tpl.html-->
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title>小明在线</title>
</head>
<body>
小明你在干嘛?<!--我是静态页面-->
<hr/> <div>我是二号页面</div>
<hr/> 0...1<br/>
1...2<br/>
2...3<br/>
3...4<br/>
4...5<br/>
5...6<br/>
6...7<br/> 分页数是10</body>
</html>
<!--系统变量文件 profile.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<root>
<taglib>
<name>webname</name>
<value>小明在线</value>
</taglib> <taglib>
<name>pagesize</name>
<value>10</value>
</taglib>
</root>
<!--Parser.class.php解析类-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/28
* Time: 10:27
*/
//模板解析类
class Parser {
//字段保存模板类内容
private $_tpl; function __construct($_tplFile){
if(!$this->_tpl=file_get_contents($_tplFile)){
exit('ERROR:模板文件读取错误!');
}
} //解析普通变量
private function parVar(){
$_pattern='/\{\$([\w]+)\}/';
if(preg_match($_pattern,$this->_tpl)){
$this->_tpl=preg_replace($_pattern,"<?php echo \$this->_vars['$1'];?>",$this->_tpl);
}
} //解析if else语句
private function parIf(){
$_patternStartIf='/\{if\s+\$([\w]+)\}/';
$_patternEndIf='/\{\/if\}/';
$_patternStartElse='/\{else\}/';
$_patternEndElse='/\{\/else\}/';
if(preg_match($_patternStartIf,$this->_tpl)){
$this->_tpl=preg_replace($_patternStartIf,"<?php if(\$this->_vars['$1']){?>",$this->_tpl);
if(preg_match($_patternEndIf,$this->_tpl)){
$this->_tpl=preg_replace($_patternEndIf,"<?php }?>",$this->_tpl);
if(preg_match($_patternStartElse,$this->_tpl)){
$this->_tpl=preg_replace($_patternStartElse,"<?php }else{?>",$this->_tpl);
}
}else{
exit('ERROR:if语句没有关闭!');
}
}
} //PHP注释解析
private function parCommon(){
$_patternCommon='/\{#(.*)#\}/';
if(preg_match($_patternCommon,$this->_tpl)){
$this->_tpl=preg_replace($_patternCommon,"<?php /* $1 */?>",$this->_tpl);
}else{
exit('ERROR:PHP注释解析错误!');
}
} //foreach语句解析
private function parForeach(){
$_patternStartForeach='/\{foreach\s+\$([\w]+)\(([\w]+),([\w]+)\)\}/';
$_patternEndForeach='/\{\/foreach\}/';
$_patternKeyOrValue='/\{@([\w]+)\}/';
if(preg_match($_patternStartForeach,$this->_tpl)){
if(preg_match($_patternEndForeach,$this->_tpl)){
$this->_tpl=preg_replace($_patternStartForeach,"<?php foreach(\$this->_vars['$1'] as \$$2=>\$$3){?>",$this->_tpl);
$this->_tpl=preg_replace($_patternEndForeach,"<?php }?>",$this->_tpl);
if(preg_match($_patternKeyOrValue,$this->_tpl)){
$this->_tpl=preg_replace($_patternKeyOrValue,"<?php echo \$$1;?>",$this->_tpl);
}
}else{
exit('ERROR:foreach语句没有关闭!');
}
}
} //include语句解析
private function parInclude(){
$_patternInclude='/\{include\s+file=\"([\w\.\_]+)\"\}/';
if(preg_match($_patternInclude,$this->_tpl,$_file)){
if(!file_exists($_file[1])||empty($_file)){
exit('ERROR:包含文件出错!');
}
$this->_tpl=preg_replace($_patternInclude,"<?php include ROOT_PATH.'/$1';?>",$this->_tpl);
}
} //系统变量解析
private function parConfig(){
$_patternConfig='/<!--\{([\w]+)\}-->/';
if(preg_match($_patternConfig,$this->_tpl)){
$this->_tpl=preg_replace($_patternConfig,"<?php echo \$this->_config['$1'];?>",$this->_tpl);
}
} //对外公共接口
public function compile($_parFile){
//解析模板内容
$this->parVar();
$this->parIf();
$this->parCommon();
$this->parForeach();
$this->parInclude();
$this->parConfig();
//生成编译文件
if(!file_put_contents($_parFile,$this->_tpl)){
exit('ERROR:编译文件出错!');
}
}
} ?>
<!--Templates.class.php模板类-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/28
* Time: 10:26
*/
//模板类
class Templates {
//动态接收 变量
private $_vars=array();
//保存系统变量
private $_config=array(); //创建一个构造方法判断各个目录是否存在
public function __construct()
{
if(!is_dir(TPL_DIR)||!is_dir(TPL_C_DIR)||!is_dir(CACHE_DIR)){
exit('ERROR:模板文件目录或编译文件目录或缓存文件目录不存在,请手动添加!');
}
$_sxe=simplexml_load_file('config/profile.xml');
$_taglib=$_sxe->xpath('/root/taglib');
foreach($_taglib as $_tag){
$this->_config["$_tag->name"]=$_tag->value;
}
} //assign()方法,用于注入变量
public function assign($_var,$_value){
//$_var用于同步模板里的变量,$_var相当于index.php里的name,那么在index.tpl中就是{$name}
//$_value相当于index.php里的$_value,在index.php里就是$_name
if(isset($_var)&&!empty($_var)){
$this->_vars[$_var]=$_value;
}else{
exit('ERROR:请设置模板变量!');
}
} //display方法
public function display($_file){
//设置模板路径
$_tplFile=TPL_DIR.$_file;
//判断模板文件是否存在
if(!file_exists($_tplFile)){
exit('ERROR:模板文件不存在!');
}
//编译文件
$_parFile=TPL_C_DIR.md5($_file).$_file.'.php';
//缓存文件
$_cacheFile=CACHE_DIR.md5($_file).$_file.'.html';
//如果第二次运行相同的文件,只需要载入缓存文件
if(IS_CACHE){
//编译文件和缓存文件必须同时存在
if(file_exists($_parFile)&&file_exists($_cacheFile)){
//编译文件和缓存文件没有修改过
if(filemtime($_parFile)>=filemtime($_tplFile)&&filemtime($_cacheFile)>=filemtime($_parFile)){
//载入缓存文件
include $_cacheFile;
return;
}
}
}
if(!is_file($_parFile)||filemtime($_parFile)<filemtime($_tplFile)){
//引入模板解析类
require ROOT_PATH.'/includes/Parser.class.php';
$_parser=new Parser($_tplFile);
$_parser->compile($_parFile);
}
//载入编译文件
include $_parFile;
if(IS_CACHE) {
//获取缓存区里的数据,并创建缓存文件
file_put_contents($_cacheFile, ob_get_contents());
//清除缓冲区里(编译文件加载的内容)
ob_clean();
//载入缓存文件
include $_cacheFile;
}
}
} ?>
<!--index.tpl模板文件-->
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title><!--{webname}--></title>
</head>
<body>
{$name}{$content}
<!--我是静态页面-->
{#我是php中注释#}
<hr/> {if $a}
<div>我是一号界面</div>
{else}<div>我是二号页面</div>
{/if}
<hr/> {foreach $array(key,value)}
{@key}...{@value}<br/>
{/foreach} 分页数是<!--{pagesize}-->
</body>
</html>
<!--由模板生成的1d7c7a527b6335cc7a623305ca940e1findex.tpl.php解析文件-->
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html";charset="utf-8">
<title><?php echo $this->_config['webname'];?></title>
</head>
<body>
<?php echo $this->_vars['name'];?><?php echo $this->_vars['content'];?>
<!--我是静态页面-->
<?php /* 我是php中注释 */?>
<hr/> <?php if($this->_vars['a']){?>
<div>我是一号界面</div>
<?php }else{?><div>我是二号页面</div>
<?php }?>
<hr/> <?php foreach($this->_vars['array'] as $key=>$value){?>
<?php echo $key;?>...<?php echo $value;?><br/>
<?php }?> 分页数是<?php echo $this->_config['pagesize'];?>
</body>
</html>
<!--主页面index.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/28
* Time: 10:02
*/
require dirname(__FILE__).'/template.inc.php';
//实例化模板类
$_tpl=new Templates();
//声明一个变量
$_name='小明';
$_content='你在干嘛?';
$_array=array(1,2,3,4,5,6,7);
//注入变量 name相当于index.tpl里的{$name}
$_tpl->assign('name',$_name);
$_tpl->assign('content',$_content);
$_tpl->assign('a',5<4);
$_tpl->assign('array',$_array);
//载入tpl文件
$_tpl->display('index.tpl');
?>
<!--template.inc.php辅助index.php的分担代码页面-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/29
* Time: 20:05
*/
//开启缓冲区
ob_start();
//设置utf-8编码
header('content-type:text/html;charset="utf-8"');
//网站根目录
define('ROOT_PATH',dirname(__FILE__));
//模板文件目录
define('TPL_DIR',ROOT_PATH.'/templates/');
//编译文件目录
define('TPL_C_DIR',ROOT_PATH.'/templates_c/');
//缓存文件目录
define('CACHE_DIR',ROOT_PATH.'/cache/');
//是否开启缓冲区
define('IS_CACHE',true);
//判断是否开启缓冲区
IS_CACHE?ob_start():null;
//引入模板类
require ROOT_PATH.'/includes/Templates.class.php';
?>
创建TPL自定义模板的更多相关文章
- 24章 创建TPL自定义模板(1)
鼓励分离 促进分工 smarty强大的模板引擎 自己开发可以深入了解模板引擎原理,并且简化(安全性,兼容性和功能不如开源的模板引擎) 流程图
- VS自定义项目模板:[2]创建VSIX项目模板扩展
VS自定义项目模板:[2]创建VSIX项目模板扩展 听语音 | 浏览:1237 | 更新:2015-01-02 09:21 | 标签:软件开发 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师 ...
- 使用.Net Core CLI命令dotnet new创建自定义模板
文章起源来自一篇博客:使用 .NET CORE 创建 项目模板,模板项目,Template - DeepThought - 博客园 之前使用Abp的时候就很认同Abp创建模板项目的方式.想不到.Net ...
- 前端学PHP之自定义模板引擎
前面的话 在大多数的项目组中,开发一个Web程序都会出现这样的流程:计划文档提交之后,前端工程师制作了网站的外观模型,然后把它交给后端工程师,它们使用后端代码实现程序逻辑,同时使用外观模型做成基本架构 ...
- PHP的自定义模板引擎
前面的话 在大多数的项目组中,开发一个Web程序都会出现这样的流程:计划文档提交之后,前端工程师制作了网站的外观模型,然后把它交给后端工程师,它们使用后端代码实现程序逻辑,同时使用外观模型做成基本架构 ...
- Django自定义模板
定义simple_tag步骤 一.创建templatetags文件 首先在app下创建templatetags文件:名字不许叫这个,不能改变. 二.在文件中创建一个py文件 文件名自定义 三.在创建的 ...
- .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(三)
阅读目录: 7.HtmlHelper.HtmlHelper<T>中的ViewModel的类型推断 8.控制ViewModel中的某个属性的呈现(使用PartialView部分视图细粒度控制 ...
- 使用requireJS,backboneJS,和underscoreJS完成自定义模板封装
使用requireJS,backboneJS,和underscoreJS完成自定义模板封装 原来的代码 当我们进行一个列表的数据填充的时候,是这样做的: //获取美食列表 function getFo ...
- SharePoint 2013 自定义模板页后在列表里修改不了视图
前言 最近系统从2010升级至2013,有自定义模板页.突然发现在列表中切换不了视图,让我很费解. 我尝试过以下解决方案: 去掉自定义css 去掉自定义js 禁用所有自定义功能 结果都没有效还是一样的 ...
随机推荐
- SSE,MSE,RMSE,R-square 指标讲解
SSE(和方差.误差平方和):The sum of squares due to error MSE(均方差.方差):Mean squared errorRMSE(均方根.标准差):Root mean ...
- unity, Global和Local编辑模式
下图表示是在Local模式下: 下图表示是在Global模式下: 不要搞反.
- GitHub的Fork 是什么意思
现在有这样一种情形: 有一个叫做Joe的程序猿写了一个游戏程序,而你可能要去改进它.并且Joe将他的代码放在了GitHub仓库上. 下面是你要做的事情 fork并且更新GitHub仓库的图表演示 Fo ...
- Source Insight 项目简单使用说明
SI(Source Insight) 是我一直写代码的好伙伴, 相信这强大的软件也是广大程序猿编写软件的利器. 正所谓" 工欲善其事, 必先利其器", 我们要学会利用这款软件. 先 ...
- 【Android】利用Fiddler进行抓包详解教程。抓取接口以及数据,可以抓真实安卓手机或者模拟器。
大家都知道抓包的方法很多.我这里给大家介绍介绍一种,利用fiddler进行抓包,当然比如Wireshark也可以抓包,我们这里不做介绍.我这里演示的是fiddler+天天模拟器,当然真实安卓手机也是一 ...
- jquery几个按钮同时调用一个方法
- SqlServer 数据分页
select * from ( select ROW_NUMBER() over (partition by name order by name) rowid,* from table ) t
- 关于dbutils中QueryRunner看批量删除语句batch
//批量删除 public void delBooks(String[] ids) throws SQLException { QueryRunner qr = new QueryRunner(C3P ...
- Buffer ByteBuffer 缓冲区
http://blog.sina.com.cn/s/blog_4150f50c0100gfa3.html
- 【Mac + Python + Selenium】之PyCharm配置Selenium自动化
一.安装PyCharm 1.下载地址: Pycharm编辑器官网下载地址 2.安装完成之后打开,一路下去点击,直到填写注册码,参考: <[转载][Pycharm编辑器破解步骤]之idea和Pyc ...