一个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实现telnet功能示例
Apr 08 PHP
ThinkPHP标签制作教程
Jul 10 PHP
PHP实现的下载远程图片自定义函数分享
Jan 28 PHP
php判断两个浮点数是否相等的方法
Mar 14 PHP
php实现插入数组但不影响原有顺序的方法
Mar 27 PHP
浅析PHP中Session可能会引起并发问题
Jul 23 PHP
php生成静态html页面的方法(2种方法)
Sep 14 PHP
php使用strip_tags()去除html标签仍有空白的解决方法
Jul 28 PHP
PHP微信API接口类
Aug 22 PHP
基于thinkPHP框架实现留言板的方法
Oct 17 PHP
php实现的后台表格分页功能示例
Oct 23 PHP
ThinkPHP框架实现FTP图片上传功能示例
Apr 08 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加MYSQL服务器
2006/10/09 PHP
php Smarty模板生成html文档的方法
2010/04/12 PHP
通过php快速统计某个数据库中每张表的数据量
2012/09/04 PHP
PHP 中 Orientation 属性判断上传图片是否需要旋转
2015/10/16 PHP
Laravel如何实现适合Api的异常处理响应格式
2020/06/14 PHP
Jquery Ajax解析XML数据(同步及异步调用)简单实例
2014/02/12 Javascript
用jquery写的菜单从左往右滑动出现
2014/04/11 Javascript
javascript生成随机数方法汇总
2015/11/12 Javascript
IE8兼容Jquery.validate.js的问题
2016/12/01 Javascript
js选项卡的制作方法
2017/01/23 Javascript
vue-resourse将json数据输出实例
2017/03/08 Javascript
利用node.js如何搭建一个简易的即时响应服务器
2017/05/28 Javascript
vue中实现上传文件给后台实例详解
2019/08/22 Javascript
Ant Design Vue 添加区分中英文的长度校验功能
2020/01/21 Javascript
基于vue和websocket的多人在线聊天室
2020/02/01 Javascript
vue2路由方式--嵌套路由实现方法分析
2020/03/06 Javascript
python实现批量获取指定文件夹下的所有文件的厂商信息
2014/09/28 Python
Python中使用Beautiful Soup库的超详细教程
2015/04/30 Python
深入解析Python中的变量和赋值运算符
2015/10/12 Python
动感网页相册 python编写简单文件夹内图片浏览工具
2016/08/17 Python
Python3.6安装及引入Requests库的实现方法
2018/01/24 Python
对python中list的拷贝与numpy的array的拷贝详解
2019/01/29 Python
详解python项目实战:模拟登陆CSDN
2019/04/04 Python
pytorch 使用单个GPU与多个GPU进行训练与测试的方法
2019/08/19 Python
基于python2.7实现图形密码生成器的实例代码
2019/11/05 Python
Python常用模块函数代码汇总解析
2020/08/31 Python
使用CSS3实现圆角,阴影,透明
2014/12/23 HTML / CSS
英国Zoro工具:手动工具,电动工具和个人防护用品
2016/11/02 全球购物
Prototype如何为一个Ajax添加一个参数
2015/12/06 面试题
管理学专业个人求职信范文
2013/09/21 职场文书
师范生自荐信范文
2013/10/06 职场文书
禁毒宣传工作方案
2014/05/23 职场文书
淘宝好评语句大全
2014/12/31 职场文书
2015年社区计生工作总结
2015/04/21 职场文书
勤俭节约倡议书范文
2015/04/29 职场文书
2016春季运动会通讯稿
2015/07/18 职场文书