一个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 彩色文字实现代码
Jun 29 PHP
php json_encode奇怪问题说明
Sep 27 PHP
php Ubb代码编辑器函数代码
Jul 05 PHP
实现在同一方法中获取当前方法中新赋值的session值解决方法
Jun 26 PHP
php CI框架插入一条或多条sql记录示例
Jul 29 PHP
PHP中使用register_shutdown_function函数截获fatal error示例
Apr 21 PHP
php中define用法实例
Jul 30 PHP
PHP中的Session对象如何使用
Sep 25 PHP
thinkPHP2.1自定义标签库的导入方法详解
Jul 20 PHP
php 常用的系统函数
Feb 07 PHP
php实现简单的权限管理的示例代码
Aug 25 PHP
PHP parse_ini_file函数的应用与扩展操作示例
Jan 07 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 header()函数使用说明
2008/07/10 PHP
PHP结合Mysql数据库实现留言板功能
2016/03/04 PHP
详解php几行代码实现CSV格式文件输出
2017/07/01 PHP
phpcms配置列表页以及获得文章发布时间
2017/07/04 PHP
菜鸟javascript基础整理1
2010/12/06 Javascript
移动节点的jquery代码
2014/01/13 Javascript
一个简单的动态加载js和css的jquery代码
2014/09/01 Javascript
JS 使用for循环遍历子节点查找元素
2014/09/06 Javascript
设置jQueryUI DatePicker默认语言为中文
2016/06/04 Javascript
老生常谈Javascript中的原型和this指针
2016/10/09 Javascript
javascript 内置对象及常见API详细介绍
2016/11/01 Javascript
jQuery简单获取DIV和A标签元素位置的方法
2017/02/07 Javascript
vue图片加载与显示默认图片实例代码
2017/03/16 Javascript
Angular.Js之Scope作用域的学习教程
2017/04/27 Javascript
jQuery实现拖动效果的实例代码
2017/06/25 jQuery
微信小程序实现动态设置placeholder提示文字及按钮选中/取消状态的方法
2017/12/14 Javascript
Js面试算法详解
2018/04/08 Javascript
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
sortable+element 实现表格行拖拽的方法示例
2019/06/07 Javascript
element-ui中Table表格省市区合并单元格的方法实现
2019/08/07 Javascript
vue实现element表格里表头信息提示功能(推荐)
2019/11/20 Javascript
[01:39](回顾)各路豪强针锋相对,几经鏖战四强产生
2014/07/01 DOTA
Python lambda和Python def区别分析
2014/11/30 Python
opencv resize图片为正方形尺寸的实现方法
2019/12/26 Python
pytorch实现特殊的Module--Sqeuential三种写法
2020/01/15 Python
Python操作注册表详细步骤介绍
2020/02/05 Python
python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法
2020/02/26 Python
Python环境下安装PyGame和PyOpenGL的方法
2020/03/25 Python
详解Scrapy Redis入门实战
2020/11/18 Python
澳大利亚厨房和家用电器购物网站:Bing Lee
2021/01/11 全球购物
销售总经理岗位职责
2014/03/15 职场文书
教师对学生的寄语
2014/04/03 职场文书
爱护公共设施标语
2014/06/24 职场文书
家具公司总经理岗位职责
2014/07/08 职场文书
车辆年检委托书范本
2014/10/14 职场文书
兵马俑导游词
2015/02/02 职场文书