一个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 相关文章推荐
mysql_fetch_assoc和mysql_fetch_row的功能加起来就是mysql_fetch_array
Jan 15 PHP
php 服务器调试 Zend Debugger 的安装教程
Sep 25 PHP
PHP获取网址的顶级域名函数代码
Sep 24 PHP
关于PHP堆栈与列队的学习
Jun 21 PHP
关于php循环跳出的问题
Jul 01 PHP
通过dbi使用perl连接mysql数据库的方法
Apr 16 PHP
php从给定url获取文件扩展名的方法
Mar 14 PHP
8个PHP数组面试题
Jun 23 PHP
PHP封装函数实现生成随机的字符串验证码
Jan 24 PHP
PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法
Jun 20 PHP
PHP实现驼峰样式字符串(首字母大写)转换成下划线样式字符串的方法示例
Aug 10 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
May 13 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中文转拼音的实现代码
2014/02/11 PHP
PHP中使用TCPDF生成PDF文档实例
2014/07/01 PHP
php实现扫描二维码根据浏览器类型访问不同下载地址
2014/10/15 PHP
php rsa加密解密使用详解
2015/01/14 PHP
php获取当月最后一天函数分享
2015/02/02 PHP
php验证码的制作思路和实现方法
2015/11/12 PHP
laravel7学习之无限级分类的最新实现方法
2020/09/30 PHP
JavaScript中计算网页中某个元素的位置
2015/06/10 Javascript
基于MVC5和Bootstrap的jQuery TreeView树形控件(二)之数据支持json字符串、list集合
2016/08/11 Javascript
JavaScript实战(原生range和自定义特效)简单实例
2016/08/21 Javascript
Jq通过td获取同行其它列td的方法
2016/10/05 Javascript
javascript-解决mongoose数据查询的异步操作
2016/12/22 Javascript
前端面试知识点锦集(JavaScript篇)
2016/12/28 Javascript
使用proxy实现一个更优雅的vue【推荐】
2018/06/19 Javascript
详解vue axios用post提交的数据格式
2018/08/07 Javascript
node.js环境搭建图文详解
2018/09/19 Javascript
使用vue-cli3新建一个项目并写好基本配置(推荐)
2019/04/24 Javascript
elementUi vue el-radio 监听选中变化的实例代码
2019/06/28 Javascript
vue+elementUi图片上传组件使用详解
2019/08/20 Javascript
基于Node的Axure文件在线预览的实现代码
2019/08/28 Javascript
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
Python 26进制计算实现方法
2015/05/28 Python
Python编程修改MP3文件名称的方法
2017/04/19 Python
Python+selenium 获取浏览器窗口坐标、句柄的方法
2018/10/14 Python
django 通过url实现简单的权限控制的例子
2019/08/16 Python
Django model class Meta原理解析
2020/11/14 Python
房地产销售计划书
2014/01/10 职场文书
优秀实习生主要事迹
2014/05/29 职场文书
护士实习求职信
2014/06/22 职场文书
研究生简历自我评价范文
2014/09/13 职场文书
小班下学期幼儿评语
2014/12/30 职场文书
证券公司客户经理岗位职责
2015/04/09 职场文书
2015年护士长个人工作总结
2015/04/24 职场文书
2019企业给员工的慰问信
2019/06/24 职场文书
如何用JavaScript学习算法复杂度
2021/04/30 Javascript
Python实现简繁体转换
2021/06/07 Python