Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

strip as an output filter rather than inline

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
alan
Smarty Regular


Joined: 17 Apr 2003
Posts: 43

PostPosted: Fri Apr 18, 2003 10:28 pm    Post subject: strip as an output filter rather than inline Reply with quote

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
View user's profile Send private message
joscha
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Metzingen, Germany

PostPosted: Sat Apr 19, 2003 7:35 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
alan
Smarty Regular


Joined: 17 Apr 2003
Posts: 43

PostPosted: Mon Apr 21, 2003 12:41 am    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Apr 21, 2003 1:09 am    Post subject: Reply with quote

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
View user's profile Send private message
alan
Smarty Regular


Joined: 17 Apr 2003
Posts: 43

PostPosted: Mon Apr 21, 2003 10:51 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Apr 21, 2003 1:49 pm    Post subject: Reply with quote

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:

Code:

&amp;nbsp;
Back to top
View user's profile Send private message Visit poster's website
joscha
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Metzingen, Germany

PostPosted: Tue Apr 22, 2003 10:20 am    Post subject: Reply with quote

It seems like that does not work, the [code] [/code] tags do not affect &
Back to top
View user's profile Send private message Visit poster's website
djn
Smarty Rookie


Joined: 15 Nov 2004
Posts: 13
Location: Trst

PostPosted: Wed Aug 25, 2010 11:01 pm    Post subject: A better way? Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP