一个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 相关文章推荐
dedecms防止FCK乱格式化你的代码的修改方法
Mar 17 PHP
PHP 操作文件的一些FAQ总结
Feb 12 PHP
PHP全概率运算函数(优化版) Webgame开发必备
Jul 04 PHP
php将远程图片保存到本地服务器的实现代码
Aug 03 PHP
php实现表单多按钮提交action的处理方法
Oct 24 PHP
thinkPHP基于ajax实现的菜单与分页示例
Jul 12 PHP
Laravel 5.4.36中session没有保存成功问题的解决
Feb 19 PHP
PHP闭包定义与使用简单示例
Apr 13 PHP
php微信开发之图片回复功能
Jun 14 PHP
PHPExcel实现表格导出功能示例【带有多个工作sheet】
Jun 13 PHP
PHP将英文数字转换为阿拉伯数字实例讲解
Jan 28 PHP
PHP7内核CGI与FastCGI详解
Apr 14 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
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
2011/08/12 PHP
Codeigniter中mkdir创建目录遇到权限问题和解决方法
2014/07/25 PHP
php线性表的入栈与出栈实例分析
2015/06/12 PHP
thinkPHP数据查询常用方法总结【select,find,getField,query】
2017/03/15 PHP
基于jQuery的Tab选项框效果代码(插件)
2011/03/01 Javascript
javascript跑马灯悬停放大效果实现代码
2012/12/12 Javascript
jquery简单实现鼠标经过导航条改变背景图
2013/12/17 Javascript
JavaScript中统计Textarea字数并提示还能输入的字符
2014/06/10 Javascript
js实现回放拖拽轨迹从过程上进行分析
2014/06/26 Javascript
JS跨域问题详解
2014/11/25 Javascript
PhotoShop给图片自动添加边框及EXIF信息的JS脚本
2015/02/15 Javascript
Web Uploader文件上传插件使用详解
2016/05/10 Javascript
修改jquery中dialog的title属性方法(推荐)
2016/08/26 Javascript
jQuery实现select模糊查询(反射机制)
2017/01/14 Javascript
ajax接收后台数据在html页面显示
2017/02/19 Javascript
微信小程序 空白页重定向解决办法
2017/06/27 Javascript
JavaScript Drum Kit 指南(纯 JS 模拟敲鼓效果)
2017/07/23 Javascript
微信小程序loading组件显示载入动画用法示例【附源码下载】
2017/12/09 Javascript
layui 实现表格某一列显示图标
2019/09/19 Javascript
Javascript实现鼠标移入方向感知
2020/06/24 Javascript
[53:15]Mineski vs iG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python实现朴素贝叶斯分类器的方法详解
2018/07/04 Python
python+Splinter实现12306抢票功能
2018/09/25 Python
解决python Markdown模块乱码的问题
2019/02/14 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
Django处理Ajax发送的Get请求代码详解
2019/07/29 Python
Python中如何引入第三方模块
2020/05/27 Python
德国综合购物网站:OTTO
2018/11/13 全球购物
中国旅游网站:途牛旅游网
2019/09/29 全球购物
医学检验专业个人求职信范文
2013/12/04 职场文书
《母鸡》教学反思
2014/02/25 职场文书
红领巾广播站广播稿
2014/10/19 职场文书
《葡萄沟》教学反思
2016/02/23 职场文书
Redis字典实现、Hash键冲突及渐进式rehash详解
2021/09/04 Redis
AudioContext 实现音频可视化(web技术分享)
2022/02/24 Javascript
《异世界四重奏》剧场版6月10日上映 PV视觉图原创角色发表
2022/03/20 日漫