登录  /  注册

一个PHP模板,主要想体现一下思路_php技巧

php中文网
发布: 2016-05-17 09:42:17
原创
690人浏览过
思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下

例如一个论坛首页(index.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}","");
      $tpl->assign_block("{/block_cat}","}?>");
        $tpl->assign_block("{block_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}

 
   
 
{block_forum}
 
   
 
{/block_forum}
{=$cat['forum_name']}
{=$forum['forum_name']}



{/block_cat}

经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.

生成的PHP模板文件(default_index.php):
代码:



 
   
 
if($forum['forum_cat_id'] == $cat['forum_id']) {?>
 
   
 
}
}?>
=$cat['forum_name']?>
=$forum['forum_name']?>



}?>

default_index.php被包含在index.php,这样就可以正常显示了.

这样,HTML模板文件可以用dw来进行修改美化,美工人员应该会方便一些.

template.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);
   }
}
?>
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号