View previous topic :: View next topic |
Author |
Message |
alan Smarty Regular
Joined: 17 Apr 2003 Posts: 43
|
Posted: Fri Apr 18, 2003 10:28 pm Post subject: strip as an output filter rather than inline |
|
|
Not sure if we're permitted to post plugins here, but I do find it time consuming to check every template for {strip}...{/strip} so a wrote a small output filter which does the same job:
[php:1:adb2747050]<?php
/*
* Smarty plugin
* --------------------------------------------------------
* File: outputfilter.strip.php
* Type: outputfilter
* Name: strip
* Version: 1.0
* Date: 18th Apr 2003
* Purpose: Performs the same functionality as the {strip}
* {/strip} tags, but across the board.
* Install: Place in your (local) plugins directory and
* add the call:
* $smarty->load_filter('output', 'strip');
* Author: Alan McFarlane
* --------------------------------------------------------
*/
function smarty_outputfilter_strip( $source, &$smarty )
{
// compress all whitespace (tabs, newlines, spaces etc) down to one space
$source = preg_replace("`\s+`ms", " ", $source);
// compress <tag> ... <tag> to <tag>...</tag>
$source = preg_replace("`>\s*(.*)\s*<`Ums", ">\\1<", $source);
// trim the result
$source = trim($source);
return $source;
}
?>[/php:1:adb2747050] |
|
Back to top |
|
joscha Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Metzingen, Germany
|
Posted: Sat Apr 19, 2003 7:35 am Post subject: |
|
|
A outputfilter handling this seems to be nice, but there could be problems with it, because then you'd have to convert all spaces you want to & nbsp;
for example here:
Code: | <a href="foo"> bar </a> to <a href="foo">& nbsp;bar& nbsp;</a> |
comment: i had to make a space between & and nbsp; because the forum converted it...
and that seems not so practical to me...
greets,
Joscha |
|
Back to top |
|
alan Smarty Regular
Joined: 17 Apr 2003 Posts: 43
|
Posted: Mon Apr 21, 2003 12:41 am Post subject: |
|
|
I agree that there are issues with this, however personally, I never use spaces to adjust the visuals of a site, I prefer to rely on stylesheets.
There are probably many different ways to do this, however I found that my strip filter is easier for me to use especially if I need to see the entire source of the html document in it's original form rather than stripped.
For this, I just don't bother to load the strip filter, whereas before I would have to go through each and every template removing the {strip} and {/strip} tags. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Apr 21, 2003 1:09 am Post subject: |
|
|
Nice filter alan. It serves the purpose you describe perfectly. Also, joscha's objection can be overcome by tweaking the compress tag regex. |
|
Back to top |
|
alan Smarty Regular
Joined: 17 Apr 2003 Posts: 43
|
Posted: Mon Apr 21, 2003 10:51 am Post subject: |
|
|
Thankyou, glad someone likes it.
I have to admit though, my original strip filter was a bit slow, so I tend to use the following:
[php:1:c42b538781]function smarty_outputfilter_strip( $output, &$smarty )
{
return trim(preg_replace(array("`>\s+`", "`\s+<`"), array(">", "<"), $output));
}[/php:1:c42b538781]
Same problems apply joscha, but as I said earlier, it works for me! |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Apr 21, 2003 1:49 pm Post subject: |
|
|
joscha wrote: |
comment: i had to make a space between & and nbsp; because the forum converted it...
|
To escape it for the forum display, you can do this:
|
|
Back to top |
|
joscha Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Metzingen, Germany
|
Posted: Tue Apr 22, 2003 10:20 am Post subject: |
|
|
It seems like that does not work, the [code] [/code] tags do not affect & |
|
Back to top |
|
djn Smarty Rookie
Joined: 15 Nov 2004 Posts: 13 Location: Trst
|
Posted: Wed Aug 25, 2010 11:01 pm Post subject: A better way? |
|
|
I found a post on StackOverflow linking to this thread (http://stackoverflow.com/questions/3558464/stripping-whitespace-out-of-smarty-templates-from-php) and I believe a better solution would be to just modify outputfilter.trimwhitespace.php inserting the whitespace replacement function at line 51, taking advantage of the nice way the plugin saves and then restores the blocks where one might want whitespace not to be modified (script, pre and textarea elements, to which I'd add the code element). |
|
Back to top |
|
|