一个PHP模板,主要想体现一下思路
2018-10-12 14:08
387 查看
思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下
例如一个论坛首页(index.php):
对应的html模板文件(index.html):
经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.
生成的PHP模板文件(default_index.php):
default_index.php被包含在index.php,这样就可以正常显示了.
这样,HTML模板文件可以用dw来进行修改美化,美工人员应该会方便一些.
template.php
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下
例如一个论坛首页(index.php):
代码: |
<?php require('./template.php'); //由html生成的php文件的前缀,区别使用多种风格. $tpl_prefix = 'default'; //模板文件名 $tpl_index = 'index'; $tpl = new Template($tpl_prefix); $cats = array( array('forum_id'=>'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}","<?foreach(\$cats as \$cat) {?>"); $tpl->assign_block("{/block_cat}","<?}?>"); $tpl->assign_block("{block_forum}","<?foreach(\$forums as \$forum) { \nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>"); $tpl->assign_block("{/block_forum}","<?}\n}?>"); //生产PHP模板文件. $tpl->write_cache($tpl_index); } } //包含PHP模板文件. include($tpl->parse_tpl($tpl_index)); ?> |
对应的html模板文件(index.html):
代码: |
{block_cat} <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#000000" align="center"> <tr align="{=TR_ALING}" bgcolor="#FFFFFF"> <td colspan="6"><span class="title"><b>{=$cat['forum_name']}</b></span></td> </tr> {block_forum} <tr bgcolor="#FFFFFF"> <td valign="top">{=$forum['forum_name']}</td> </tr> {/block_forum} </table> <br> {/block_cat} |
经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.
生成的PHP模板文件(default_index.php):
代码: |
<?foreach($cats as $cat) {?> <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#000000" align="center"> <tr align="<?=TR_ALING?>" bgcolor="#FFFFFF"> <td colspan="6"><span class="title"><b><?=$cat['forum_name']?></b></span></td> </tr> <?foreach($forums as $forum) { if($forum['forum_cat_id'] == $cat['forum_id']) {?> <tr bgcolor="#FFFFFF"> <td valign="top"><?=$forum['forum_name']?></td> </tr> <?} }?> </table> <br> <?}?> |
default_index.php被包含在index.php,这样就可以正常显示了.
这样,HTML模板文件可以用dw来进行修改美化,美工人员应该会方便一些.
template.php
代码: |
<?php /********************************************************************************* * 模板类(Template) * 最后修改时间:2004.4.07 本论坛使用 * * * **********************************************************************************/ class Template { //$this->$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模板,主要想体现一下思路
- hihocoder #1040 矩形判断(计算几何问题 给8个点的坐标,能否成为一个矩形 【模板思路】)
- php5.3以后各版本的特性(主要是整理一下面试用的)
- PHPCMS V9.3.2用户注册模板中的一个低级Bug
- 今天我发现了一个网站有很多的html5手机wap网站模板,在这里分享一下
- 一个个人觉得比较好用的PHP日志类,给大家分享一下
- 自己写的一个导出的程序这里主要用到一下程序
- 认真看了一下博客园的模板,选了一个颜色鲜艳的!
- 最近要复制一个php的产品成java,简单记录一下php的知识
- 一个不成熟的模板引擎思路
- 理解一下base64算法,一个PHP加解密方法
- PHPCMS V9.3.2用户注册模板中的一个低级Bug
- php开发web在线管理器思路分析(主要是对目录和文件进行管理)
- 因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序。 主要的原理就是替换模板里的特殊字符。
- 分享一个sql (主要体现case when 作用)
- Date Range Picker时间插件非常不错,主要体现在选择一个时间区间
- 写了一个PHP版本的MONGODB语法解析器,可以通过类似SQL的语法来进行MONGODB的查询,不知道有人需要不,分享一下吧
- PHP开发的一个小小项目,记录下实现思路(附上实现功能的示例代码)(一)
- 在上一节中,我们讲到吞吐量,做为一个用户你可以对吞吐量毫不关心,但响应时间却是用户感受系统性能的主要体现。 从用户角度来说,软件性能就是软件对用户操作的响应时间。说得更明确一点,对用户来说,当用户单
- 封装的一个mysql_lib.php, 主要是一个分页模型