博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单模板类
阅读量:4451 次
发布时间:2019-06-07

本文共 1573 字,大约阅读时间需要 5 分钟。

MyMiniSmarty.class.php

View Code
1 
tpl_vars[$tpl_var] = $var;19 }20 }21 //替换占位符编译成可运行文件22 function display($tpl_file) {23 $tpl_file_path = $this->template_dir . $tpl_file;24 $complie_file_path = $this->compile_dir . 'com_' . $tpl_file . '.php';25 if (!file_exists($tpl_file_path)) {26 return false;27 }28 //判断是否有缓存文件,以前编译过,检查模板文件和编译文件修改时间29 if (!file_exists($complie_file_path) || filemtime($tpl_file_path) > filemtime($complie_file_path)) {30 $fpl_file_con = file_get_contents($tpl_file_path);31 32 //核心是正则替换33 34 $pattern = array(35 '/\{\s*\$([a-zA-Z_]\w*)\s*\}/i'36 );37 $replace = array(38 '
tpl_vars["${1}"] ?>' 39 );40 41 $new_str = preg_replace($pattern, $replace, $fpl_file_con);42 43 file_put_contents($complie_file_path, $new_str);44 }45 46 47 48 49 //引用编译后的文件50 include($complie_file_path);51 }52 53 }54 ?>

intro.php

View Code
assign('title', '模板标题'); $mysmarty->assign('content', '模板内容'); $mysmarty->display('intro.tpl');?>

intro.tpl

View Code
{$title}{$content}

模板机制在于,简单标签代替php代码,使逻辑代码和页面设计分离,同时加入文件缓存等机制

 

转载于:https://www.cnblogs.com/caps/archive/2013/02/26/2934217.html

你可能感兴趣的文章
Struts2的crud
查看>>
java上传文件
查看>>
大学生对技术网站需求的调查问卷结果分析
查看>>
测试一
查看>>
vertx的HttpServer模块
查看>>
as3事件流机制彻底理解
查看>>
Selenium webdriver操作日历控件
查看>>
Pascal程序练习-与7无关的数
查看>>
Linux:cut命令...未完待续
查看>>
PX4地面站QGroundControl在ubuntu下的安装
查看>>
react实现svg实线、虚线、方形进度条
查看>>
Web
查看>>
那些容易忽略的事(1) -变量与运算符+
查看>>
九度oj 题目1252:回文子串
查看>>
(十一)tina | openwrt关闭调试串口(DEBUG UART)
查看>>
angularjs 使用angular-sortable-view实现拖拽效果(包括拖动完成后的方法使用)
查看>>
2015生命之旅---南京、南通、上海之行
查看>>
高精度练习之乘法(codevs_3117)
查看>>
小Z爱划水
查看>>
Qt Font
查看>>