一个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 相关文章推荐
frename PHP 灵活文件命名函数 frename
Sep 09 PHP
PHP 杂谈《重构-改善既有代码的设计》之三 重新组织数据
Apr 09 PHP
PHP中常用的转义函数
Feb 28 PHP
PHP使用GIFEncoder类处理gif图片实例
Jul 01 PHP
PHP自带函数给数字或字符串自动补齐位数
Jul 29 PHP
PHP编写RESTful接口的方法
Feb 21 PHP
CodeIgniter针对数据库的连接、配置及使用方法
Mar 03 PHP
PHP函数shuffle()取数组若干个随机元素的方法分析
Apr 02 PHP
一波PHP中cURL库的常见用法代码示例
May 06 PHP
php使用Jpgraph创建折线图效果示例
Feb 15 PHP
php简单计算权重的方法示例【适合抽奖类应用】
Jun 10 PHP
PHP Beanstalkd消息队列的安装与使用方法实例详解
Feb 21 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
一个显示天气预报的程序
2006/10/09 PHP
php根据分类合并数组的方法实例详解
2013/11/06 PHP
Laravel 5.4向IoC容器中添加自定义类的方法示例
2017/08/15 PHP
一个分享按钮的插件使用介绍(可扩展,内附开发制作流程)
2011/09/19 Javascript
获取非最后一列td值并将title设为该值的方法
2013/10/30 Javascript
探讨jQuery的ajax使用场景(c#)
2013/12/03 Javascript
js验证输入是否为手机号码或电话号码示例
2013/12/30 Javascript
NodeJS学习笔记之Connect中间件应用实例
2015/01/27 NodeJs
js删除数组元素、清空数组的简单方法(必看)
2016/07/27 Javascript
关于原生js中bind函数的简单实现
2016/08/10 Javascript
使用bootstrap插件实现模态框效果
2017/05/10 Javascript
node.js-v6新版安装具体步骤(分享)
2017/09/06 Javascript
基于JavaScript实现前端数据多条件筛选功能
2020/08/19 Javascript
使用Vue.set()方法实现响应式修改数组数据步骤
2019/11/09 Javascript
vue如何实现动态加载脚本
2020/02/05 Javascript
Vue实现返回顶部按钮实例代码
2020/10/21 Javascript
详解Vue3.0 + TypeScript + Vite初体验
2021/02/22 Vue.js
Python list操作用法总结
2015/11/10 Python
django输出html内容的实例
2018/05/27 Python
在python里协程使用同步锁Lock的实例
2019/02/19 Python
python nmap实现端口扫描器教程
2020/05/28 Python
Python 模拟动态产生字母验证码图片功能
2019/12/24 Python
python中的 zip函数详解及用法举例
2020/02/16 Python
Django通过json格式收集主机信息
2020/05/29 Python
python 引用传递和值传递详解(实参,形参)
2020/06/05 Python
Keras 利用sklearn的ROC-AUC建立评价函数详解
2020/06/15 Python
python tkinter实现下载进度条及抖音视频去水印原理
2021/02/07 Python
python上下文管理器异常问题解决方法
2021/02/07 Python
天猫超市:阿里巴巴打造的网上超市
2016/11/02 全球购物
兼职业务员岗位职责
2014/01/01 职场文书
2014三八妇女节活动总结范文四篇
2014/03/09 职场文书
关于旅游的活动方案
2014/08/15 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
师德师风个人总结
2015/02/06 职场文书
辩论会主持词
2015/07/03 职场文书
JavaScript实现显示和隐藏图片
2021/04/29 Javascript