一个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 缓冲的免费实现方法
Oct 09 PHP
中英文字符串翻转函数
Dec 09 PHP
php二维数组排序详解
Nov 06 PHP
php检测iis环境是否支持htaccess的方法
Feb 18 PHP
php 删除cookie方法详解
Dec 01 PHP
PHP实现加密的几种方式介绍
Feb 22 PHP
laravel容器延迟加载以及auth扩展详解
Mar 02 PHP
PHP执行SQL文件并将SQL文件导入到数据库
Sep 17 PHP
浅析Laravel5中队列的配置及使用
Aug 04 PHP
php获取客户端IP及URL的方法示例
Feb 03 PHP
yii gridview实现时间段筛选功能
Aug 15 PHP
在 Laravel 6 中缓存数据库查询结果的方法
Dec 11 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
老机欣赏|中国60年代精品收音机
2021/03/02 无线电
windows下开发并编译PHP扩展的方法
2011/03/18 PHP
php开启安全模式后禁用的函数集合
2011/06/26 PHP
php读取文件内容至字符串中,同时去除换行、空行、行首行尾空格(Zjmainstay原创)
2012/07/31 PHP
PHP实现一维数组转二维数组的方法
2015/02/25 PHP
php正则修正符用法实例详解
2016/12/29 PHP
PHP中时间加减函数strtotime用法分析
2017/04/26 PHP
PHP删除二维数组中相同元素及数组重复值的方法示例
2017/05/05 PHP
List the Codec Files on a Computer
2007/06/18 Javascript
extjs4 treepanel动态改变行高度示例
2013/12/17 Javascript
JS设置获取cookies的方法
2014/01/26 Javascript
让JavaScript和其它资源并发下载的方法
2014/10/16 Javascript
Javascript中的默认参数详解
2014/10/22 Javascript
深入学习JavaScript对象
2015/10/13 Javascript
JavaScript 模块的循环加载实现方法
2015/12/13 Javascript
js 发布订阅模式的实例讲解
2017/09/10 Javascript
webpack打包并将文件加载到指定的位置方法
2018/02/22 Javascript
JavaScript 日期时间选择器一些小结
2018/04/02 Javascript
通过npm或yarn自动生成vue组件的方法示例
2019/02/12 Javascript
微信用户访问小程序的登录过程详解
2019/09/20 Javascript
解决vue打包报错Unexpected token: punc的问题
2020/10/24 Javascript
[37:47]IG vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
Python中第三方库Requests库的高级用法详解
2017/03/12 Python
python给指定csv表格中的联系人群发邮件(带附件的邮件)
2019/12/31 Python
通过实例了解python__slots__使用方法
2020/09/14 Python
html5简单示例_动力节点Java学院整理
2017/07/07 HTML / CSS
Mytheresa美国官网:德国知名的女性奢侈品电商
2017/05/27 全球购物
英国顶级家庭折扣店:The Works
2017/09/06 全球购物
澳大利亚头发和美容产品购物网站:OZ Hair & Beauty
2020/03/27 全球购物
公务员的自我鉴定
2013/10/26 职场文书
运动会闭幕式解说词
2014/02/21 职场文书
2014年感恩母亲演讲稿
2014/05/27 职场文书
班级活动总结格式
2014/08/30 职场文书
导游词之沈阳清昭陵
2019/12/28 职场文书
Centos7中MySQL数据库使用mysqldump进行每日自动备份的编写
2021/08/02 MySQL
Go并发4种方法简明讲解
2022/04/06 Golang