Smarty PHP模板引擎压缩HTML
Smarty 模板压缩 HTML,去除 HTML 中的回车换行空白注释等
方法 1¶
在创建对象时使用 registerFilter 绑定匿名函数
$smarty = new Smarty();
// 压缩HTML
$smarty->registerFilter("output", function ($html) {
$html = preg_replace(':\s+//.*?\n:', '', $html);
$html = preg_replace('/<!--\s*[^[][^!][^<].*?-->/s', '', $html);
$html = preg_replace('/\/\*.*?\*\//s', '', $html);
$html = preg_replace('/>\s*</s', '><', $html);
$html = preg_replace('/(\s)+/s', ' ', $html);
return trim($html);
});
方法 2¶
修改文件 sysplugins/smarty_template_source.php 中的方法:public function getContent()
public function getContent()
{
// return isset($this->content) ? $this->content : $this->handler->getContent($this);
// 压缩HTML
$html = isset($this->content) ? $this->content : $this->handler->getContent($this);
$html = preg_replace(':\s+//.*?\n:', '', $html);
$html = preg_replace('/<!--\s*[^[][^!][^<].*?-->/s', '', $html);
$html = preg_replace('/\/\*.*?\*\//s', '', $html);
$html = preg_replace('/>\s*</s', '><', $html);
$html = preg_replace('/(\s)+/s', ' ', $html);
return trim($html);
}
如果设置了模板缓存,需删除缓存文件后才生效
