Stripping whitespace out of smarty templates from PHP

余生颓废 提交于 2020-01-13 19:15:21

问题


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?


回答1:


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).




回答2:


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




回答3:


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.




回答4:


Here is another output filter to strip white space.

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



来源:https://stackoverflow.com/questions/3558464/stripping-whitespace-out-of-smarty-templates-from-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!