Maj*_*Afy 4 html compression smarty minify
如何缩小Smarty中的所有输出HTML模板?
像这样:
$smarty->minify = true;
PS:我找到了{strip}
功能,但是我应该在所有.tpl
文件中使用此功能。我有很多.tpl
文件,这种方式对我来说是不可能的。
小智 5
我使用以下代码:
function minify_html($tpl_output, Smarty_Internal_Template $template) {
$tpl_output = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $tpl_output);
return $tpl_output;
}
// register the outputfilter
$smarty->registerFilter("output", "minify_html");
$smarty->display($template);
Run Code Online (Sandbox Code Playgroud)
注意:您将需要更改// comments
为SCRIPT标签以/* comments */
您可以使用Smarty v3随附的trimwhitespace
输出过滤器。
$smarty->loadFilter('output', 'trimwhitespace');
Run Code Online (Sandbox Code Playgroud)
该过滤器不会直接缩小输出,但是会删除Smarty附带的许多空白。在我的情况下,空格大约是问题的80%到90%。
请注意,output
过滤器每次都在已编译的模板上运行。因此,与发送完整文件相比,此过滤器的运行时间更长,只要您不需要减少流量或使用缓存,它就没有太大用处。但是也许可以编写一个类似的过滤器作为post
过滤器运行。
也有{strip}
街区。这也会删除空格。不同之处在于它在编译时运行,而不是像输出过滤器一样在每次调用时运行。
因此,如果触摸每个模板文件都是一个选择,那{strip}
将是更好的选择。如果不是,则使用输出过滤器。