开发者

Stripping whitespace out of smarty templates from PHP

Is there a way to tell Smar开发者_StackOverflowty 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).


Here is the most recent implementation of the trimWhitespace output filter from smarty 3.1 which seems to do what you want.

raw file: http://smarty-php.googlecode.com/svn-history/r4136/branches/Smarty_3_1_cleanup/distribution/libs/plugins/outputfilter.trimwhitespace.php

source browser: http://code.google.com/p/smarty-php/source/browse/branches/Smarty_3_1_cleanup/distribution/libs/plugins/outputfilter.trimwhitespace.php?r=4136


You could create and register an output filter to do this; in the output filter, you can use for example this function to strip out unneeded whitespace.


Here is another output filter to strip white space.

http://www.smarty.net/forums/viewtopic.php?t=25&sid=26a10d55ac90d50dca7914e33fdc6fa1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜