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} That Turns New-Lines Into Single Spaces?

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
mankyd
Smarty Regular


Joined: 04 May 2006
Posts: 92
Location: Boston MA

PostPosted: Mon Apr 30, 2007 9:05 pm    Post subject: {strip} That Turns New-Lines Into Single Spaces? Reply with quote

The {strip} tag is great save for one drawback. A lot of times when I create a template I put newlines in my text such that I don't have one long continuous line of text that runs off the side of the screen. i.e.:

Code:
<p>Text
on
multiple
lines</p>


The problem is that, when I surround a paragraph that like above with a pair of {strip} tags, the new line characters are erased, but not replaced with single spaces. Words at the end of one line run straight into the next:

Code:
<p>Textonmulitplelines</p>


Can the behavior or {strip} be changed or can we add an optional "newline_to_space" boolean value that replaces new lines with single spaces? Consecutive new lines can, of course, be reduced to a single space. This is the way HTML rendering would normally display them.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue May 01, 2007 3:18 am    Post subject: Reply with quote

Hi mankyd.

I'm hesistant to fool with {strip} due to code-injection issues.

Feel free to provide a patch for review if you are so inclined Wink
Back to top
View user's profile Send private message
mankyd
Smarty Regular


Joined: 04 May 2006
Posts: 92
Location: Boston MA

PostPosted: Wed May 02, 2007 12:12 am    Post subject: Reply with quote

Hmm, well, I haven't looked through the Smarty code too much, but so far I've come up with a pretty simple solution.

The {strip} tag currently uses the following regex to do it's job:
Code:
$text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', '', $text_blocks[$j]);


By changing this into two regexes, it will strip whitespace/new-lines between apparent html tags and turn other white-space/new-lines into single spaces:

Code:
$text_blocks[$j] = preg_replace('!>[\t ]*[\r\n]+[\t ]*!<', '><', $text_blocks[$j]);
$text_blocks[$j] = preg_replace('![\t ]*[\r\n]+[\t ]*!', ' ', $text_blocks[$j]);


It's not perfect in that, inevitably, CSS can change the layout of html tags, changing whether the whitepace between then is relevant. This does, however, fix white-space in ordinary text being completely erased.

In my own code, I hacked in a {reduce} tag rather than play with the {strip} tag directly. It piggy-backs on the code that strip uses, so it only requires that a few lines be added. If anyone is interested in this, let me know.
Back to top
View user's profile Send private message
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 -> Feature Requests 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