smarty模板引擎原理解析
//php 控制器文件
<?php
//引入模板引擎文件
include("20130304.php");
$smarty =newTinySmarty();
$qq_numbers=array('a1'=>'12333','a2'=>'2222222','a3'=>'333333','a4'=>'3333333');
$smarty->assign($qq_numbers);
$smarty->assign('title','这是我的QQ号码');
$smarty->assign('contents','这是我的QQ:1211884772');
$smarty->display('20120305_01.html');
?>
模板引擎类20130304.php
<?php
/***
smarty模板引擎原理
1:读取模板文件
2:替换模板标签为php可执行代码
3:保存替换成功的php文件
***/
/*
问题?
1:每次访问都编译浪费cpu?
编译文件存在,不用在编译直接引入
2:模板文件修改后,必须重新编译该文件
当模板文件修改时间大于编译文件修改时间,说明模板文件被修改了,
因此要重新编译模板文件
*/
classTinySmarty{
//模板文件存放目录
public $template_dir="./templates/";
//编译后文件存放目录
public $compile_dir="./c_templates/";
//存放变量值
public $tpl_vars=array();
//assign
//将变量以数组形式存放到该$tpl_var属性
publicfunction assign($tpl_var,$var=null){
//传入数组形式,为批量赋值
if(is_array($tpl_var)){
foreach($tpl_var as $_key=>$_val){
if($_key!=''){
$this->tpl_vars[$_key]= $_val;
}
}
}else{
//传入非空字符
if($tpl_var!=''){
$this->tpl_vars[$tpl_var]= $var;
}
}
}
/*
name display
param string $tpl_file 文件名
*/
publicfunction display($tpl_file){
//模板文件路径
$template_file_path = $this->template_dir.$tpl_file;
//编译文件路径
$compile_file_path = $this->compile_dir.$tpl_file;
//判断编译文件是否存在
if(!file_exists($compile_file_path)||filemtime($template_file_path)>filemtime($compile_file_path)){
//判断文件是否存在
if(!file_exists($template_file_path)){
returnfalse;
}
//读取文件内容
$fpl_file_con = file_get_contents($template_file_path);
//替换模板标签
//如:{$title} 替换为<?php echo $title; ? >
//正则表达式//此处正则涉及到正则的反响引用
$pattern ='/{\s*\$([_a-zA-Z][_0-9a-zA-Z]*)\s*\}/i';
$replace ='<?php echo $this->tpl_vars["${1}"];?>';
$new_file_con= preg_replace($pattern,$replace,$fpl_file_con);
//写入文件内容
file_put_contents($compile_file_path,$new_file_con);
}
//引入编译后的文件
include ($compile_file_path);
}
}
?>
模板文件20120305_01.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>{$title}</title>
</head>
<body>
<h1>hello-</h1>
{$contents}
</body>
</html>
smarty模板引擎原理解析的更多相关文章
- 迷你版 smarty --模板引擎和解析
http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...
- 高性能JavaScript模板引擎原理解析
随着 web 发展,前端应用变得越来越复杂,基于后端的 javascript(Node.js) 也开始崭露头角,此时 javascript 被寄予了更大的期望,与此同时 javascript MVC ...
- Smarty模板引擎原理概述
smarty(模板引擎,模板技术) 使用smarty主要是为了实现逻辑和外在内容的分离: 特点: 1.速度快(因为第二次执行的时候使用第一次执行时生成的编译文件) 2.缓存技术(正是因为缓存技术,使得 ...
- 深入浅出之Smarty模板引擎工作机制(二)
源代码下载地址:深入浅出之Smarty模板引擎工作机制 接下来根据以下的Smarty模板引擎原理流程图开发一个自己的模板引擎用于学习,以便加深理解. Smarty模板引擎的原理,其实是这么一个过程: ...
- 深入浅出之Smarty模板引擎工作机制(一)
深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...
- 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...
- Smarty模板引擎技术二
Smarty模板引擎技术 内建函数 include_php内建函数 作用:载入一个php文件,将载入的文件的内容赋值给一个变量 注意:该内建函数只能在2.0中使用,如果使用的话,必须得实例化Sma ...
- JavaScript模板引擎原理
JavaScript模板引擎原理,几行代码的事儿 2013-12-03 16:35 by BarretLee, 650 阅读, 6 评论, 收藏, 编辑 一.前言 什么是模板引擎,说的简单点,就是一个 ...
- Smarty模板引擎技术
Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...
随机推荐
- hdoj 1862 EXCEL排序
EXCEL排序 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- PAT 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- 【JAVA - SSM】之MyBatis与原生JDBC、Hibernate访问数据库的比较
首先来看一下原生JDBC访问数据库的代码: public static void main(String[] args) { // 数据库连接 Connection connection = null ...
- Jsp学习(2)
Jsp的三大指令 (1).include 作用:相当于把当前页面去包含页面 语法: <%@include file="/common/test.jsp" %> 实例如下 ...
- HibernateTemplate的find(String querystring)返回值具体解释
项目源代码中出现例如以下代码: HibernateTemplate ht =-- List<Object[]> tempList = ht.find(String querystring) ...
- 从robots.txt開始网页爬虫之旅
做个网页爬虫或搜索引擎(下面统称蜘蛛程序)的各位一定不会陌生,在爬虫或搜索引擎訪问站点的时候查看的第一个文件就是robots.txt了.robots.txt文件告诉蜘蛛程序在server上什么文件是能 ...
- C#生成XML的三种途径
C#生成XML的三种途径 为了全面,这里都将XML保存到文件中,有三种生成XML的方式: 1.我认为是最原始,最基本的一种:利用XmlDocument向一个XML文件里写节点,然后再利用XmlDocu ...
- Windows下Memcached在.Net程序中的实际运用(从Memcached客户端Enyim的库的编译到实际项目运用)
1.一点基础概念 2.获取EnyimMemcached客户端的源代码并编译出动态库 3.Memcached的服务器安装(windows server) 4.在web项目中实战 一.基础概念 memca ...
- Android开发之设定Dialog的位置
今天自定义了一个对话框,但是弹出时默认是显示在屏幕中间.主要代码:menuDialog = new AlertDialog.Builder(this).create(); ...
- FreeBSD系统更新与软件安装方法
一.系统更新 freebsd-update fetch freebsd-update install 二.软件源更新(类似yum update.apt-get update) 1.取回源 portsn ...