View previous topic :: View next topic |
Author |
Message |
Maņungo Smarty n00b
Joined: 03 May 2003 Posts: 3 Location: Chile
|
Posted: Sat May 03, 2003 11:50 pm Post subject: nl2br and textarea |
|
|
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 |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Sat May 03, 2003 11:51 pm Post subject: |
|
|
what's wrong about it? |
|
Back to top |
|
Maņungo Smarty n00b
Joined: 03 May 2003 Posts: 3 Location: Chile
|
Posted: Sat May 03, 2003 11:59 pm Post subject: |
|
|
Inside a textarea a <br /> have no sense... smarty could note this and skip this case. |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Sun May 04, 2003 12:27 am Post subject: |
|
|
no... that's not smarty's job |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun May 04, 2003 12:53 am Post subject: |
|
|
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 |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Sun May 04, 2003 12:58 am Post subject: |
|
|
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... |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun May 04, 2003 1:14 am Post subject: |
|
|
That's what he's saying he wants, but I don't think that's what he really wants .
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 |
|
Back to top |
|
Maņungo Smarty n00b
Joined: 03 May 2003 Posts: 3 Location: Chile
|
Posted: Sun May 04, 2003 1:55 am Post subject: |
|
|
Ok, i will try to explain my problem, using my poor english
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 |
|
dthought Smarty Regular
Joined: 21 Apr 2003 Posts: 55 Location: Melbourne, Australia
|
Posted: Sun May 04, 2003 2:06 am Post subject: |
|
|
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.
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun May 04, 2003 2:53 am Post subject: |
|
|
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 |
|
Back to top |
|
|