smarty模板执行原理
为了实现程序的业务逻辑和内容表现页面的分离从而提高开发速度,php 引入了模板引擎的概念,php 模板引擎里面最流行的可以说是smarty了,smarty因其功能强大而且速度快而被广大php web开发者所认可。本文将记录一下smarty模板引擎的工作执行原理,算是加深一下理解。
其实所有的模板引擎的工作原理是差不多的,无非就是在php程序里面用正则匹配将模板里面的标签替换为php代码从而将两者混合为一个php的混编文件,然后执行这个混编文件。基本上就是这么回事儿了。下面以smarty为例说下这个过程。
例如本站文章页面:http://www.phpernote.com/article.php?id=795
一般处理过程是这样的:
html模板页面部分代码(article.html):
<body> <div>{subject}</div> <div>{content}</div> </body>
php页面逻辑部分代码:
$subject='smarty视频教程分享'; $content='smarty视频教程分享,下面是具体的下载地址,有需要的朋友可以看看,对smarty模板讲解的非常详细,作者粗略看了一下目录,真是详细到细枝末节该......'; $str=file_get_contents('article.html'); $str=str_replace('{subject}',$subject,$str); $str=str_replace('{content}',$content,$str); echo $str;
使用面向对象技术实现模板功能的封装代码如下:
<?php class Template{ //属性 public $vars; //保存要替换的标记和数据的内容 public $left_delimiter='{*'; //左分隔符 public $right_delimiter='*}'; //右分隔符 //方法 public function assign($key,$value){ $this->vars[$key]=$value; } public function display($file){//file表示模板名 $str=file_get_contents($file);//从模板中读取多有内容,并将内容放入$str中 foreach ($this->vars as $key => $value){ //$key 键名(模板标记) $value 值 $str=str_replace($this->left_delimiter.$key.$this->right_delimiter, $value, $str); } echo $str; //file_put_contents('bak.html', $str); } }
注意:assign(‘name’,’zhangsan’);这句的时候其实还没有进行数据替换,而是把传入的数据保存在vars[]中,当display的时候才进行数据替换。
smarty的处理过程:
1、 smarty将php源文件,首先编译成中间文件
2、 如果启用缓存,再根据编译文件生成缓存文件
3、 之后每次访问都会访问编译文件
如果启用缓存文件而且有缓存文件并且缓存文件没有过期,则直接访问缓存文件(先不考虑缓存的时候的流程)编译文件里时间戳记录模板文件修改时间,如果模板被修改过就可以检测到,然后重新编译。
(编译是把静态内容保存起来,动态内容根据传入的参数不同而不同)
读取编译文件省去了读取模板文件,和字符串替换的时间,所以可以更快。
第一次请求article.php时候编译,产生编译文件,在编译文件里。
第二次请求article.php的时候,判断模板文件是否改变,如果模板文件已改变,那么去读取模板文件,然后再编译,如果没有改变,则去读取编译文件,编译文件最终输出;
缓存默认是关闭的;缓存是把数据彻底的存在缓存文件里,直到缓存文件过期才会重新来缓存;所以说smarty在一些实时性特别强的网站里不是特别合适;
对于以上文字可以抽象的理解为下面的一幅图,读者自己去体会吧!
考虑缓存:
在smarty程序里,判断是否开启了缓存文件,并且缓存文件没有过期,,就去找缓存文件,如果没有开启缓存文件,就去判断模板文件,如果缓存文件已过期,也是去判断模板文件。
您可能感兴趣的文章
- smarty 模板中循环表格补充不全的td
- smarty模板中for循环的扩展插件
- smarty模板中如何生成随机数
- smarty模板中判断数组为空的方法
- 程序中通过define定义的常量如何在smarty模板中使用
- smarty模板中使用php函数以及smarty模板中如何对一个变量使用多个函数
- smarty模板中给信息添加最新标签
- smarty模板保留变量总结

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

Regarding PPT masking, many people must be unfamiliar with it. Most people do not understand it thoroughly when making PPT, but just make it up to make what they like. Therefore, many people do not know what PPT masking means, nor do they understand it. I know what this mask does, and I don’t even know that it can make the picture less monotonous. Friends who want to learn, come and learn, and add some PPT masks to your PPT pictures. Make it less monotonous. So, how to add a PPT mask? Please read below. 1. First we open PPT, select a blank picture, then right-click [Set Background Format] and select a solid color. 2. Click [Insert], word art, enter the word 3. Click [Insert], click [Shape]

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

C++ template specializations affect function overloading and rewriting: Function overloading: Specialized versions can provide different implementations of a specific type, thus affecting the functions the compiler chooses to call. Function overriding: The specialized version in the derived class will override the template function in the base class, affecting the behavior of the derived class object when calling the function.
