博客做好之后,为了更快的打开页面,我就想把模版的页面全部的压缩一下。
thinkphp里面渲染模板都是用display方法。其实,它是继承自Think核心代码里面。
\ThinkPHP\Library\Think\View.class.php 查找到display方法大概67行。将下面的两端页面压缩的代码放入进去即可。
压缩代码:
第一种:将页面压缩html标签 js jq保留 个人比较倾向这个。$content = preg_replace("~>\s+\s+\r\n~",">",$content));
第二种:将页面全部压缩$content = ltrim(rtrim(preg_replace(array("/> *([^ ]*) *","//","'/\*[^*]*\*/'","/\r\n/","/\n/","/\t/",'/>[ ]+'),array(">\\1
修改的代码:public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
G('viewStartTime');
// 视图开始标签
Hook::listen('view_begin',$templateFile);
// 解析并获取模板内容
$content = $this->fetch($templateFile,$content,$prefix);
// 第一种:将页面压缩html标签 js jq保留 个人比较倾向这个。
$content = preg_replace("~>\s+\s+\r\n~",">",$content));
// 第二种:将页面全部压缩
$content = ltrim(rtrim(preg_replace(array("/> *([^ ]*) *","//","'/\*[^*]*\*/'","/\r\n/","/\n/","/\t/",'/>[ ]+'),array(">\\1
// 输出模板内容
$this->render($content,$charset,$contentType);
// 视图结束标签
Hook::listen('view_end');
}
只要按照上面的修改方式,很轻松的就实现了thinkphp页面模版压缩的效果。