一个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 相关文章推荐
一个阿拉伯数字转中文数字的函数
Oct 09 PHP
php长字符串定义方法
Jul 12 PHP
php使用正则表达式获取图片url的方法
Jan 16 PHP
php实现有趣的人品测试程序实例
Jun 08 PHP
简介WordPress中用于获取首页和站点链接的PHP函数
Dec 17 PHP
Twig模板引擎用法入门教程
Jan 20 PHP
PHP Header用于页面跳转时的几个注意事项
Oct 21 PHP
PHP针对伪静态的注入总结【附asp与Python相关代码】
Aug 01 PHP
yii2.0整合阿里云oss删除单个文件的方法
Sep 19 PHP
PHP数组式访问接口ArrayAccess用法分析
Dec 28 PHP
PHP+百度AI OCR文字识别实现了图片的文字识别功能
May 08 PHP
laravel excel 上传文件保存到本地服务器功能
Nov 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
使用php4加速网络传输
2006/10/09 PHP
PHP 错误之引号中使用变量
2009/05/04 PHP
php数组函数序列之asort() - 对数组的元素值进行升序排序,保持索引关系
2011/11/02 PHP
php array_keys 返回数组的键名
2016/10/25 PHP
javascript事件模型代码
2007/07/01 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
一个html5播放视频的video控件只支持android的默认格式mp4和3gp
2014/05/08 Javascript
jquery插件pagination实现无刷新ajax分页
2015/09/30 Javascript
jQuery简单操作cookie的插件实例
2016/01/13 Javascript
如何利用Promises编写更优雅的JavaScript代码
2016/05/17 Javascript
返回函数的JavaScript函数
2016/06/14 Javascript
jquery datatable服务端分页
2016/08/31 Javascript
Javascript中常用的检测方法小结
2016/10/08 Javascript
JS根据生日月份和日期计算星座的简单实现方法
2016/11/24 Javascript
使用node.js中的Buffer类处理二进制数据的方法
2016/11/26 Javascript
基于Bootstrap和JQuery实现动态打开和关闭tab页的实例代码
2019/06/10 jQuery
微信小程序(订阅消息)功能
2019/10/25 Javascript
Vue中避免滥用this去读取data中数据
2021/03/02 Vue.js
python通过ftplib登录到ftp服务器的方法
2015/05/08 Python
Python中的urllib模块使用详解
2015/07/07 Python
django 2.0更新的10条注意事项总结
2018/01/05 Python
pytorch构建网络模型的4种方法
2018/04/13 Python
python 对类的成员函数开启线程的方法
2019/01/22 Python
Python发展史及网络爬虫
2019/06/19 Python
3种适用于Python的疯狂秘密武器及原因解析
2020/04/29 Python
python中scrapy处理项目数据的实例分析
2020/11/22 Python
python 如何引入协程和原理分析
2020/11/30 Python
支票、地址标签、包装纸和慰问卡:Current Catalog
2018/01/30 全球购物
班组长工作职责
2013/12/25 职场文书
中国文明网签名寄语
2014/01/18 职场文书
华清池导游词
2015/02/02 职场文书
重阳节慰问信
2015/02/15 职场文书
人工作失职检讨书
2015/05/05 职场文书
CSS filter 有什么神奇用途
2021/05/25 HTML / CSS
常用的Python代码调试工具总结
2021/06/23 Python
Java图书管理系统,课程设计必用(源码+文档)
2021/06/30 Java/Android