一个PHP模板,主要想体现一下思路


Posted in PHP onDecember 25, 2006

思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下

例如一个论坛首页(index.php):

代码:
'1','forum_cat_id'=>'0','forum_name'=>'PHP学习'),    array('forum_id'=>'2','forum_cat_id'=>'0','forum_name'=>'MYSQL学习') ); $forums = array(    array('forum_id'=>'3','forum_cat_id'=>'1','forum_name'=>'PHP高级教程'),    array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP初级教程'),    array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL相关资料') ); if ($cats) {    if ($tpl->chk_cache($tpl_index))//检查判断是否需要重新生产PHP模板文件.     {        $tpl->load_tpl($tpl_index);//加载html模板文件.       //替换PHP语句       $tpl->assign_block("{block_cat}","");       $tpl->assign_block("{/block_cat}","}?>");         $tpl->assign_block("{block_forum}","");        $tpl->assign_block("{/block_forum}","}\n}?>");       //生产PHP模板文件.       $tpl->write_cache($tpl_index);    } } //包含PHP模板文件. include($tpl->parse_tpl($tpl_index)); ?>

对应的html模板文件(index.html):
代码:
{block_cat}         {block_forum}         {/block_forum}
{=$cat['forum_name']}
{=$forum['forum_name']}

{/block_cat}

经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.

生成的PHP模板文件(default_index.php):

代码:
                } }?>
=$cat['forum_name']?>
=$forum['forum_name']?>

}?>

default_index.php被包含在index.php,这样就可以正常显示了.

这样,HTML模板文件可以用dw来进行修改美化,美工人员应该会方便一些.


template.php
代码:
$template,储存模板数据.    var $template = '';    //模板路径.    var $tpl_path = '';    //模板前缀(风格名称).    var $tpl_prefix = '';     //cache路径(编译后的路径).    var $cache_path = '';    //css文件路径.    var $css_path = '';    //header文件路径.    var $header_path = '';    //footer文件路径     var $footer_path = '';    /**    * 初始化模板路径.    */    function Template($root = 'default')    {       //模板前缀(风格名称).       $this->tpl_prefix = $root;       //模板文件路径.       $this->tpl_path = './templates/' . $root . '/';       //生成的PHP文件存放路径.       $this->cache_path = './template_data/' .$this->tpl_prefix . '_';       return true;    }    /**    * chk_cache,检查"编译"后的模板是否需要更新,判断依据:最后修改时间,"编译"文件是否存在.    */    function chk_cache($tpl_index)     {       $tpl_file = $this->tpl_path . $tpl_index . '.html';       $cache_file = $this->cache_path . $tpl_index . '.php';       //判断是否需要更新.       if(!file_exists($cache_file))         {          return true;       }         elseif(filemtime($tpl_file) > filemtime($cache_file))         {          return true;       }    }    /**    * 输出模板文件.    */    function parse_tpl($tpl_index,$message='')     {        return $this->cache_path . $tpl_index . '.php';     }    /**    * 加载模板文件.    */    function load_tpl($tpl_index)     {       $tpl_file = $this->tpl_path . $tpl_index . '.html';       $fp = fopen($tpl_file, 'r');       $this->template = fread($fp, filesize($tpl_file));       fclose($fp);    }    /**    * 替换变量,并且"编译"模板.    */    function write_cache($tpl_index)     {       $cache_file = $this->cache_path . $tpl_index . '.php';       //变量显示.       $this->template = preg_replace("/(\{=)(.+?)(\})/is", "=\\2?>", $this->template);       //界面语言替换.       $this->template = preg_replace("/\{lang +(.+?)\}/ies", "\$lang['main']['\\1']", $this->template);         $fp = fopen($cache_file, 'w');         flock($fp, 3);         fwrite($fp, $this->template);         fclose($fp);     }    /**    * 替换block.    */    function assign_block($search,$replace)     {       $this->template = str_replace($search,$replace,$this->template);    } } ?>
PHP 相关文章推荐
php 调用远程url的六种方法小结
Nov 02 PHP
PHP 面向对象 PHP5 中的常量
May 05 PHP
超级好用的一个php上传图片类(随机名,缩略图,加水印)
Jun 30 PHP
PHP备份/还原MySQL数据库的代码
Jan 06 PHP
PhpDocumentor 2安装以及生成API文档的方法
May 21 PHP
linux下编译安装memcached服务
Aug 03 PHP
PHP类的反射用法实例
Nov 03 PHP
php实现购物车功能(上)
Jul 23 PHP
PHP版本的选择5.2.17 5.3.27 5.3.28 5.4 5.5兼容性问题分析
Apr 04 PHP
PHP数据库处理封装类实例
Dec 24 PHP
php插件Xajax使用方法详解
Aug 31 PHP
Thinkphp 框架扩展之Widget扩展实现方法分析
Apr 23 PHP
ob_start(),ob_start('ob_gzhandler')使用
Dec 25 #PHP
php预定义常量
Dec 25 #PHP
php中看实例学正则表达式
Dec 25 #PHP
谈谈新手如何学习PHP
Dec 23 #PHP
服务器端解压缩zip的脚本
Dec 22 #PHP
Windows2003 下 MySQL 数据库每天自动备份
Dec 21 #PHP
剖析 PHP 中的输出缓冲
Dec 21 #PHP
You might like
解析php中const与define的应用区别
2013/06/18 PHP
Thinkphp+smarty+uploadify实现无刷新上传
2015/07/30 PHP
weiphp微信公众平台授权设置
2016/01/04 PHP
php支持断点续传、分块下载的类
2016/05/02 PHP
Javascript实例教程(19) 使用HoTMetal(1)
2006/12/23 Javascript
jquery validator 插件增加日期比较方法
2010/02/21 Javascript
JQuery魔力之$("tagName")与selector
2012/03/05 Javascript
jQuery计算textarea中文字数(剩余个数)的小程序
2013/11/28 Javascript
IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理
2014/12/16 Javascript
JS+CSS实现感应鼠标渐变显示DIV层的方法
2015/02/20 Javascript
javascript常用函数(2)
2015/11/05 Javascript
JavaScript数据绑定实现一个简单的 MVVM 库
2016/04/08 Javascript
jQuery配合coin-slider插件制作幻灯片效果的流程解析
2016/05/13 Javascript
angular.js分页代码的实例
2016/07/27 Javascript
使用angular帮你实现拖拽的示例
2017/07/05 Javascript
关于vue面试题汇总
2018/03/20 Javascript
createObjectURL方法实现本地图片预览
2019/09/30 Javascript
JavaScript提升机制Hoisting详解
2019/10/23 Javascript
p5.js绘制创意自画像
2019/11/04 Javascript
python 顺时针打印矩阵的超简洁代码
2018/11/14 Python
numpy.meshgrid()理解(小结)
2019/08/01 Python
Python如何获取文件指定行的内容
2020/05/27 Python
使用Python实现音频双通道分离
2020/12/25 Python
Python实现图片指定位置加图片水印(附Pyinstaller打包exe)
2021/03/04 Python
为你的html5网页添加音效示例
2014/04/03 HTML / CSS
美国领先的男士和女士内衣购物网站:Freshpair
2019/02/25 全球购物
一套英文Java笔试题面试题
2016/04/21 面试题
商务英语专业应届毕业生求职信
2013/10/28 职场文书
事业单位竞聘上岗实施方案
2014/03/28 职场文书
音乐节策划方案
2014/06/09 职场文书
公司年底活动方案
2014/08/17 职场文书
关于长城的导游词
2015/01/30 职场文书
小学教学工作总结2015
2015/05/13 职场文书
欢送会主持词
2015/07/01 职场文书
学长教您写论文:经验总结
2019/07/09 职场文书
用Python实现屏幕截图详解
2022/01/22 Python