一个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
网站加速 PHP 缓冲的免费实现方法
Oct 09 PHP
修改PHP的memory_limit限制的方法分享
Feb 21 PHP
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
Jan 03 PHP
在win7中搭建Linux+PHP 开发环境
Oct 08 PHP
php+mysqli数据库连接的两种方式
Jan 28 PHP
PHP实现微信发红包程序
Aug 24 PHP
thinkPHP查询方式小结
Jan 09 PHP
PHP对象克隆clone用法示例
Sep 28 PHP
php curl常用的5个经典例子
Jan 20 PHP
thinkphp5 模型实例化获得数据对象的教程
Oct 18 PHP
Yii框架 session 数据库存储操作方法示例
Nov 18 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
虹吸式咖啡探讨–研磨
2021/03/03 冲泡冲煮
第二章 PHP入门基础之php代码写法
2011/12/30 PHP
php模拟js函数unescape的函数代码
2012/10/20 PHP
php读取excel文件的简单实例
2013/08/26 PHP
php实现扫描二维码根据浏览器类型访问不同下载地址
2014/10/15 PHP
php编程每天必学之验证码
2016/03/03 PHP
Laravel框架下载,安装及路由操作图文详解
2019/12/04 PHP
如何让您的中波更粗更长 - 中波框形天线制作
2021/03/10 无线电
js调用flash的效果代码
2008/04/26 Javascript
ExtJS 2.0实用简明教程之应用ExtJS
2009/04/29 Javascript
jquery插件pagination实现无刷新ajax分页
2015/09/30 Javascript
基于Echarts 3.19 制作常用的图形(非静态)
2016/05/19 Javascript
JS中对数组元素进行增删改移的方法总结
2016/12/15 Javascript
微信小程序用户位置权限的获取方法(拒绝后提醒)
2018/11/15 Javascript
利用weixin-java-miniapp生成小程序码并直接返回图片文件流的方法
2019/03/29 Javascript
详解微信小程序中var、let、const用法与区别
2020/01/11 Javascript
使用Python脚本将绝对url替换为相对url的教程
2015/04/24 Python
Django中更新多个对象数据与删除对象的方法
2015/07/17 Python
python实现textrank关键词提取
2018/06/22 Python
python DataFrame 取差集实例
2019/01/30 Python
python识别文字(基于tesseract)代码实例
2019/08/24 Python
python基于K-means聚类算法的图像分割
2019/10/30 Python
python 实现turtle画图并导出图片格式的文件
2019/12/07 Python
Python表达式的优先级详解
2020/02/18 Python
Django 构建模板form表单的两种方法
2020/06/14 Python
django 获取字段最大值,最新的记录操作
2020/08/09 Python
python识别验证码的思路及解决方案
2020/09/13 Python
SCHIESSER荷兰官方网站:德国内衣专家
2020/10/09 全球购物
什么是数组名
2012/05/10 面试题
建筑实习自我鉴定
2013/10/18 职场文书
关于环保的建议书
2014/05/12 职场文书
机电一体化专业求职信
2014/07/22 职场文书
企业与个人合作经营协议书
2014/11/01 职场文书
2015年加油站工作总结
2015/05/13 职场文书
初中团委工作总结
2015/08/13 职场文书
使用python+pygame开发消消乐游戏附完整源码
2021/06/10 Python