php的smarty模板引擎
php是 嵌入式HTML脚本语言,为了将HTML和php代码分离,即所谓的逻辑层和表现层,这就是模板引擎的目的。为了达到该目的 模板引擎需具备的功能有:
1.存储变量;
2.读取模板文件;
3.结合前两个生成输出。
Code如下:
test01.php
$name='xiaoshenge';
?>
test02.php
name=
test03.php
include'test01.php';
include'test02.php;
?>
当然这里只是简单的模拟如何实现php模板引擎的功能,在php开源社区里著名的smarty就是封装了上面的功能。保存数据->加载模板->编译生成输出文件。有关 smarty的应用具体参考手册,这里只是解释其功能原理。
记录一下smarty缓存遇到的问题。
如果开启smarty缓存,第一次执行时会将其编译好的输出文件保存到cache目录中,在程序中通过smarty的is_cache()函数检测其 cache文件是否过期,若过期会更新缓存,如果没有过期会自动调用cache文件,省去编译的过程。检测cache过期是看模板文件是否在指定的生命周期内是否更改,这里的更改是是通过检测文件的最近修改时间实现的,不是通过检测模板文件内容。
阻止一个模板文件的 整篇被缓存 :
index.php:
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;
function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
$smarty->display('index.tpl');
index.tpl:
Page created: {"0"|date_format:"%D %H:%M:%S"}
{dynamic}
Now is: {"0"|date_format:"%D %H:%M:%S"}
... do other stuff ...
{/dynamic}
当重新加载这个页面,你将会注意到这两个日期不同。一个是“动态“,一个是“静态”。你能够在{dynamic}...{/dynamic}之间作任何事情,并且保证它将不会像剩下的页面一样被缓存。

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.