php是 嵌入式HTML脚本语言,为了将HTML和php代码分离,即所谓的逻辑层和表现层,这就是模板引擎的目的。为了达到该目的 模板引擎需具备的功能有:
1.存储变量;
2.读取模板文件;
3.结合前两个生成输出。
Code如下:
test01.php
<?php
$name='xiaoshenge';
?>
test02.php
<html>
<head><title>test</title></head>
<body>
name=<?php echo $name; ?>
</body>
</html>
test03.php
<?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}之间作任何事情,并且保证它将不会像剩下的页面一样被缓存。
详细解决方案
php的smarty模板发动机
热度:59 发布时间:2016-04-28 22:18:18.0
相关解决方案
- smarty 数据库查询有关问题
- Smarty 的控制器怎么调用其他文件的方法
- smarty section,该如何处理
- smarty 模版中,js修改了表单的值,如其直接在模版中用该表单值进行if语句判断
- smarty 遍历多维数组有关问题
- smarty 截取字符串,调用php中的步骤,foreach循环
- smarty 如若给模板传值是动态的,生成缓存 也是动态
- smarty 怎么实现i++
- smarty 循环,该怎么解决
- smarty 截取字符串解决方法
- php+smarty 为什么在IE上正常,在火狐+IE9上全是源代码?
- php smarty 缓存施用
- smarty options汉语言显示不正常
- Fatal error: Smarty error:syntax error: unrecognized tag,该怎么解决
- smarty section取数据有关问题。求大神
- smarty section如何结束循环
- smarty 关于truncate使用的有关问题
- 使用 $smarty->register_object 报错解决方案
- smarty 怎么查看版本信息?
- Smarty
- smarty 的truncate详细用法。该怎么处理
- php web2.0中的有关问题(php+smarty+zend)
- smarty 分页因缓存出有关问题
- smarty,key从一开始
- php smarty js 设立cookies
- smarty?截取字符串,该如何解决
- smarty?sql 报错
- 初来乍到-smarty
- smarty?section循环报错
- smarty——display传值有关问题