文件布局

<!--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. Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用

    一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...

  2. JS JSOP跨域请求实例详解

    JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.这篇文章主要介绍了JS JSOP跨域请求实例详解的相关资料,需要的朋友可以参考下 ...

  3. poj_2773

    题目的愿意非常easy.给你一个n,求在升序排列的情况下,第k个与之相互素的数. 解法:首先我们要知道gcd(b×t+a,b)=gcd(a.b),那么接下来就非常easy了.全部与之互素的数都是以ph ...

  4. xgboost 自定义目标函数和评估函数

    https://zhpmatrix.github.io/2017/06/29/custom-xgboost/ https://www.cnblogs.com/silence-gtx/p/5812012 ...

  5. mysql数据库中不能插入0000-00-00 00:00:00日期数据(报错Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00')

    报错信息 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' ...

  6. SET QUOTED_IDENTIFIER OFF语句的作用 转载

    SET QUOTED_IDENTIFIER ON SELECT * FROM "USER" WHERE a='netasp' SET QUOTED_IDENTIFIER ON SE ...

  7. c#动态类型

    class Program { static void Main(string[] args) { dynamic test = new ExpandoObject(); test.Name = &q ...

  8. C#多线程方法同步

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. poj Squares n个点,共能组成多少个正方形 二分 + 哈希

    题目链接:http://poj.org/problem?id=2002 测试数据: 41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20 有 ...

  10. (译)Getting Started——1.2.2 Desinging a User Interface(设计用户界面)

    ​      用户需要以最简单的方式与应用界面进行交互.应该从用户的角度出发设计页面,使得界面更高效.简捷和直接. storyboard以图形化的方式帮助你设计和实现界面.在设计和实现界面的过程中,你 ...