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

nl2br and textarea

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
Maņungo
Smarty n00b


Joined: 03 May 2003
Posts: 3
Location: Chile

PostPosted: Sat May 03, 2003 11:50 pm    Post subject: nl2br and textarea Reply with quote

The problem:

$var = "<textarea>hello
world</textarea>"

The template said:
{$var|nl2br}

And the result is:
<textarea>hello<br />
world</textarea>

Can you fix it ? Thanks.
Back to top
View user's profile Send private message Visit poster's website
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sat May 03, 2003 11:51 pm    Post subject: Reply with quote

what's wrong about it?
Back to top
View user's profile Send private message
Maņungo
Smarty n00b


Joined: 03 May 2003
Posts: 3
Location: Chile

PostPosted: Sat May 03, 2003 11:59 pm    Post subject: Reply with quote

Inside a textarea a <br /> have no sense... smarty could note this and skip this case.
Back to top
View user's profile Send private message Visit poster's website
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sun May 04, 2003 12:27 am    Post subject: Reply with quote

no... that's not smarty's job
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun May 04, 2003 12:53 am    Post subject: Reply with quote

Wom.bat wrote:
no... that's not smarty's job


Exactly. Besides, how is Smarty supposed to *know* that it is in a text-area? That was the designer's choice. Also note the name of the modifier: NL2BR. I think that says it all!

Maņungo, what would you *LIKE* to happen??
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sun May 04, 2003 12:58 am    Post subject: Reply with quote

he wants smarty to recognize the textarea tags and then not replace newlines between them.
how about letting smarty cook coffee, too? or perhaps we could extend smarty so that neither programmers nor designers need to use their brains anymore... Smile
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun May 04, 2003 1:14 am    Post subject: Reply with quote

That's what he's saying he wants, but I don't think that's what he really wants Wink.

Look, he's doing:
$var = "<textarea>hello world</textarea>";

which is obviously a bad idea. He should be doing:
$var = "hello world";

so that in his template, he can do either:
{$var|nl2br} or <textarea>{$var}</textarea> as appropriate.

In other words, he's not using Smarty properly (sorry dude!). That's why I wanted to know what he thought he was doing as opposed to his suggestion that something is wrong with Smarty. That way, we might be able to help him get the results he needs Wink
Back to top
View user's profile Send private message
Maņungo
Smarty n00b


Joined: 03 May 2003
Posts: 3
Location: Chile

PostPosted: Sun May 04, 2003 1:55 am    Post subject: Reply with quote

Ok, i will try to explain my problem, using my poor english Sad

I have a template. In certain conditions it produces a normal html page, no forms. In others, it is a page with the same information, but in form's fields.
I don't want make two different templates for consistency reasons.

Of course I can make my own version of nl2br or, better, i can check first in what situation I am and use or not nl2br... but i think it is a very nice and "smarty" property that smarty's nl2br does this for me.

That's all.
Back to top
View user's profile Send private message Visit poster's website
dthought
Smarty Regular


Joined: 21 Apr 2003
Posts: 55
Location: Melbourne, Australia

PostPosted: Sun May 04, 2003 2:06 am    Post subject: Reply with quote

You could always send Smarty a variable that is either true or false - eg. {$use_forms}

And if this variable is set, ignore the nl2br, else do it.

{if $use_forms}
{$var}
{else}
{$var|nl2br}
{/if}

I really think that drawing HTML controls from within a variable is very bad practice though; there are probably much better ways around this issue. Always try to keep Smarty to placing just textual content, not HTML content - you'll probably go mad when things start getting complex. Wink

But again, this is not really a bug in Smarty. It's just some logic functionality that you need to work out beforehand.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Sun May 04, 2003 2:53 am    Post subject: Reply with quote

It sounds like Maņungo is trying to "inject" form controls into his templates. Could be a useful hack in some circumstances, though it does mean that it is a "non-standard" use. The suggestion that nl2br SHOULDN'T convert to NL to BR in some circumstances is just plain bad--at least as a default in Smarty.

It also means that he is handling some formatting issues from his application, which is not really recommended. The template needs fomatting logic. I would expand dthought's code just a little:

Quote:
{if $use_forms}
<textarea>{$var}</textarea>
{else}
{$var|nl2br}
{/if}


Which suggests either a custom function or a modifier as in:

{textfield value=$var} or {$var|textfield} so that the inner details of repeating the above logic block can be handled in code, keeping the templates simple and easier to maintain--which is what I think he is trying to achieve with his trick of wrapping the text items in form controls before he passes them to the template.

Smarty is powerful out of the box, but I think application designers should take Smarty's building blocks and extend them to meet their needs. The Smarty developers have done a good job of making this a very straight-forward process Smile
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 -> Bugs 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