|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jengo Smarty n00b
Joined: 17 Mar 2005 Posts: 2
|
Posted: Thu Mar 17, 2005 1:01 pm Post subject: Output filter: Remove HTML-comments from template |
|
|
Hello,
I think it would be very nice to remove with the trimwhitespace output filter also the html-comments, but of course only if they are not needed (e.g. javascript, etc.).
The simplest way for me to accomplish this was to add one line of sourcecode to the outputfilter.trimwhitespace.php:
Code: |
$source = preg_replace("/<!--.*-->/U","",$source);
|
directly before, all the blocks get replaced back.
(line 52 before: // replace script blocks)
For me this allowed to leave comments in the source code while not increasing download size, etc.
If anybody sees problems with that, I would be happy to recieve your comments.
P.S.: Can not forget mention, that smarty really rocks, has reduced rendering time of our templates over 30% compared to a former homebrew template engine. Also our php code gets much smaller and better readable..
Kind regards,
Philipp |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 17, 2005 2:19 pm Post subject: |
|
|
This would also remove javascript that is surrounded by HTML comments, that might not be what you want.
This should probably be a separate filter. trimwhitespace isn't meant to remove content, just remove extraneaous white space. |
|
Back to top |
|
jengo Smarty n00b
Joined: 17 Mar 2005 Posts: 2
|
Posted: Thu Mar 17, 2005 2:28 pm Post subject: |
|
|
mohrt wrote: | This would also remove javascript that is surrounded by HTML comments, that might not be what you want.
|
Well, maybe I have not been clear, where I added it, after all javascripts and pre blocks have been taken out before, as it is already.
Here the full code (with just that one line added):
Code: |
function smarty_outputfilter_trimwhitespace($source, &$smarty)
{
// Pull out the script blocks
preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
$_script_blocks = $match[0];
$source = preg_replace("!<script[^>]+>.*?</script>!is",
'@@@SMARTY:TRIM:SCRIPT@@@', $source);
// Pull out the pre blocks
preg_match_all("!<pre>.*?</pre>!is", $source, $match);
$_pre_blocks = $match[0];
$source = preg_replace("!<pre>.*?</pre>!is",
'@@@SMARTY:TRIM:PRE@@@', $source);
// Pull out the textarea blocks
preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
$_textarea_blocks = $match[0];
$source = preg_replace("!<textarea[^>]+>.*?</textarea>!is",
'@@@SMARTY:TRIM:TEXTAREA@@@', $source);
// remove all leading spaces, tabs and carriage returns NOT
// preceeded by a php close tag.
$source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
//This is the only new line, all javascript, etc. has been out before
$source = preg_replace("/<!--.*-->/U","",$source);
// replace script blocks
smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:SCRIPT@@@",$_script_blocks, $source);
// replace pre blocks
smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:PRE@@@",$_pre_blocks, $source);
// replace textarea blocks
smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
return $source;
}
|
Or is there something I am missing, so that there are cases, for which it will not work?
mohrt wrote: |
This should probably be a separate filter. trimwhitespace isn't meant to . |
I agree fully, I will take this code in a customer filter, and leave the orignal one untouched. Does anybody already have such a filter that takes out all "unnecessary" elements (whitspaces, comments) out of a document?
Best regards,
Philipp |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|