剥离来自PHP的smarty模板的空白 [英] Stripping whitespace out of smarty templates from PHP
问题描述
有没有办法通过PHP告诉Smarty您希望它在发送到浏览器之前去除模板中的所有空白,就好像您的所有模板都嵌入了{strip}标签一样?某些类型的Smarty对象参数或其他内容?
在您的Smarty插件文件夹中有一个可轻松适应任务: outputfilter.trimwhitespace.php
。
只需添加行
$ source = preg_replace(`\s +`ms,,$ source);
(来自Martin的论坛帖子)在51行,然后调用输出过滤器。 p>
优点是,所述过滤器在保存并恢复代码块时可以很好地工作,您可以在这些代码块中单独留出空白 - 在脚本内部, pre 和 textarea 元素(我也将 code 元素添加到列表中)。
Is there a way to tell Smarty from PHP that you want it to strip all the whitespace in your templates before sending to browser, as if all your templates were embedded in {strip} tags? Some sort of Smarty object parameter or something?
In your Smarty plugin folder there is a filter that can be easily adapted to the task: it's outputfilter.trimwhitespace.php
.
Just add the line
$source = preg_replace("`\s+`ms", " ", $source);
(from the forum post linked by Martin) at line 51 and then call the output filter.
The advantage is that said filter does a nice job of saving and then restoring the code blocks where you might want to leave whitespace alone - inside script, pre and textarea elements (I'dd add the code element to the list, too).
这篇关于剥离来自PHP的smarty模板的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!