文件布局

<!--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自定义模板的更多相关文章

  1. 24章 创建TPL自定义模板(1)

    鼓励分离 促进分工 smarty强大的模板引擎 自己开发可以深入了解模板引擎原理,并且简化(安全性,兼容性和功能不如开源的模板引擎) 流程图

  2. VS自定义项目模板:[2]创建VSIX项目模板扩展

    VS自定义项目模板:[2]创建VSIX项目模板扩展 听语音 | 浏览:1237 | 更新:2015-01-02 09:21 | 标签:软件开发 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师 ...

  3. 使用.Net Core CLI命令dotnet new创建自定义模板

    文章起源来自一篇博客:使用 .NET CORE 创建 项目模板,模板项目,Template - DeepThought - 博客园 之前使用Abp的时候就很认同Abp创建模板项目的方式.想不到.Net ...

  4. 前端学PHP之自定义模板引擎

    前面的话 在大多数的项目组中,开发一个Web程序都会出现这样的流程:计划文档提交之后,前端工程师制作了网站的外观模型,然后把它交给后端工程师,它们使用后端代码实现程序逻辑,同时使用外观模型做成基本架构 ...

  5. PHP的自定义模板引擎

    前面的话 在大多数的项目组中,开发一个Web程序都会出现这样的流程:计划文档提交之后,前端工程师制作了网站的外观模型,然后把它交给后端工程师,它们使用后端代码实现程序逻辑,同时使用外观模型做成基本架构 ...

  6. Django自定义模板

    定义simple_tag步骤 一.创建templatetags文件 首先在app下创建templatetags文件:名字不许叫这个,不能改变. 二.在文件中创建一个py文件 文件名自定义 三.在创建的 ...

  7. .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(三)

    阅读目录: 7.HtmlHelper.HtmlHelper<T>中的ViewModel的类型推断 8.控制ViewModel中的某个属性的呈现(使用PartialView部分视图细粒度控制 ...

  8. 使用requireJS,backboneJS,和underscoreJS完成自定义模板封装

    使用requireJS,backboneJS,和underscoreJS完成自定义模板封装 原来的代码 当我们进行一个列表的数据填充的时候,是这样做的: //获取美食列表 function getFo ...

  9. SharePoint 2013 自定义模板页后在列表里修改不了视图

    前言 最近系统从2010升级至2013,有自定义模板页.突然发现在列表中切换不了视图,让我很费解. 我尝试过以下解决方案: 去掉自定义css 去掉自定义js 禁用所有自定义功能 结果都没有效还是一样的 ...

随机推荐

  1. DDD~大话目录

    来自:http://www.cnblogs.com/lori/p/3472789.html DDD~DDD从零起步架构说明 DDD~概念中的DDD DDD~microsoft NLayerApp项目中 ...

  2. 计算机原理--cpu篇

    简介 本文的目的是为了能够对特定的计算模型估算所需的CPU规格,个数. 这里主要介绍CPU的基本工作原理,指令集.(仅以X86体系结构的CPU为例 )

  3. JavaScript正则表达式基础知识汇总

    一.创建正则对象: 1.构造函数RegExp创建正则对象 var pattern = new RegExp('s$'); //pattern匹配以s结尾的字符串 2.使用正则直接量 var patte ...

  4. redis conf 详解

    2.8配置 # Redis configuration file example # Note on units: when memory size is needed, it is possible ...

  5. atitit.userService 用户系统设计 v6 q413

    atitit.userService 用户系统设计 v6 q413 1. 新特性1 2. Admin  login1 3. 用户注册登录2 3.1. <!-- 会员注册使用 -->  商家 ...

  6. iOS 图片的属性

    UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIView ...

  7. 一次csrf配合xss的攻击实例

    xss出现在我的邮箱处 alert of payload "><script>alert(/test/)</script><" by cooki ...

  8. 信号signal编号及意义及一般处理

    SIGQUIT:停止 SIGILL:illegal instruction SIGABRT:Abort SIGFPE:Float point exception SIGPIPE:Broken pipe ...

  9. phpcms利用广告位实现轮播图调用

    如果我们使用自带的广告轮播,那么就是失去原有的轮播样式,这里就教大家一种使用广告轮播,还能保留原有的轮播样式 1.需要找到广告位的模块位置 3 下载广告代码 https://files.cnblogs ...

  10. Android基础新手教程——3.1 基于监听的事件处理机制

    Android基础新手教程--3.1.1 基于监听的事件处理机制 标签(空格分隔): Android基础新手教程 本节引言: 第二章我们学习的是Android的UI控件,我们能够利用这些控件构成一个精 ...