Discussion:
[SMARTY] nl2br in a template?
gawan
2003-02-21 23:10:33 UTC
Permalink
Hi

I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?

gawan
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul Lockaby
2003-02-21 23:09:13 UTC
Permalink
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
Yes! Using a plugin for this is ideal. Here is an example:

function smarty_modifier_nl2br($message) {
return nl2br($body);
}

Put that in a file by itself, named modifier.nl2br.php and then in your
template file, simply call it like this:

{ $var|nl2br }

And poof(!) $var will have all newlines replaced with <BR>s. You can
also check out this page right here:

http://smarty.incutio.com/?page=SmartyPlugins

For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.

Hope that helps!
~Paul
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul Lockaby
2003-02-21 23:10:37 UTC
Permalink
Post by Paul Lockaby
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Put that in a file by itself, named modifier.nl2br.php and then in your
{ $var|nl2br }
And poof(!) $var will have all newlines replaced with <BR>s. You can
http://smarty.incutio.com/?page=SmartyPlugins
For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.
Hope that helps!
~Paul
Yeah, proofreading kicks ass, but only before you click "send". Lemme
rewrite those instructions so that they make sense. Put the above
function into a file called modifier.nl2br.php and save that file in
your plugins directory. Then call it as described from your template. I
hope that makes a *little* more sense.
--
Paul Lockaby
***@paullockaby.com
http://www.paullockaby.com
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ubaldo Huerta
2003-02-21 23:49:02 UTC
Permalink
Hi,

I just saw that somebody shared this pluggin,
http://marc.theaimsgroup.com/?l=smarty-general&m=104378865428811&w=2
which I haven't tried yet. A while back, I struggled writing something
similar on my own that I'm
not too proud of because it still doesn't cover all cases (regex isn't my
favorite thing on earth)

Anyhow, I think that perhaps both modifier pluggins should merge into
something
like this

function smarty_modifier_htmlize($string, $what = 'all')

where $what could be 'nl2br', 'href, 'mailto' or 'all'.

because I think that it's very likely that if someone needs to wrap
www.foo.com in an
<a href ...>, chances are that they might one to turn every new line into a
<br>

Perhaps HTMLizing text should be part of the standard (out of the box),
plug-in repository.

Adios,

ubaldo
www.loquo.com

----- Original Message -----
From: "Paul Lockaby" <***@vt.edu>
To: "Paul Lockaby" <***@vt.edu>
Cc: "gawan" <***@gawan.net>; <smarty-***@lists.php.net>
Sent: Saturday, February 22, 2003 12:10 AM
Subject: Re: [SMARTY] nl2br in a template?
Post by Paul Lockaby
Post by Paul Lockaby
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Put that in a file by itself, named modifier.nl2br.php and then in your
{ $var|nl2br }
And poof(!) $var will have all newlines replaced with <BR>s. You can
http://smarty.incutio.com/?page=SmartyPlugins
For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.
Hope that helps!
~Paul
Yeah, proofreading kicks ass, but only before you click "send". Lemme
rewrite those instructions so that they make sense. Put the above
function into a file called modifier.nl2br.php and save that file in
your plugins directory. Then call it as described from your template. I
hope that makes a *little* more sense.
--
Paul Lockaby
http://www.paullockaby.com
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
gawan
2003-02-21 23:21:50 UTC
Permalink
Post by Paul Lockaby
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Put that in a file by itself, named modifier.nl2br.php and then in your
{ $var|nl2br }
And poof(!) $var will have all newlines replaced with <BR>s. You can
http://smarty.incutio.com/?page=SmartyPlugins
For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.
Hope that helps!
~Paul
Thanks Paul, for a really quick answer!

And I thought plugins are something difficult.
This examble helps to understand them in general, thanks.

gawan
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Pete
2003-02-26 16:59:28 UTC
Permalink
Could this function go in the standard distrinution as I'm sure
everyone else uses it

Pete

function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Post by Paul Lockaby
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Put that in a file by itself, named modifier.nl2br.php and then in your
{ $var|nl2br }
And poof(!) $var will have all newlines replaced with <BR>s. You can
http://smarty.incutio.com/?page=SmartyPlugins
For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.
Hope that helps!
~Paul
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Juzan Jean-Michel
2003-02-26 17:06:02 UTC
Permalink
Hi,

You have a thread in the mailing list about nl2br

Regards.
Post by Pete
Could this function go in the standard distrinution as I'm sure
everyone else uses it
Pete
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Post by Paul Lockaby
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Put that in a file by itself, named modifier.nl2br.php and then in your
{ $var|nl2br }
And poof(!) $var will have all newlines replaced with <BR>s. You can
http://smarty.incutio.com/?page=SmartyPlugins
For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.
Hope that helps!
~Paul
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Jean-Michel Juzan ***@ecreasol.ch

Direction Informations & Technologies
eCreaSol - NetWork Creative Solutions SARL - http://www.ecreasol.ch/

PGP public keys server : http://www.keyserver.net
Monte Ohrt
2003-02-26 21:44:40 UTC
Permalink
This can be added, sounds useful enough. A few questions:

1) Is there a better name, or keep nl2br for consistancy of PHP
developers familiarity? nl2br is nice and short though...

2) Should it always return XHTML compliant code <br />, regardless of
PHP version? the PHP command nl2br() pre-PHP 4.0.5 doesn't output XHTML
break tags. I think it would be good to always be XHTML compliant.

3) Should it cover newline formats from Mac/PC appropriately? In
otherwords replace \n or \r\n with <br /> tags?

4) Should it replace newlines already preceeded by a <br> tag?
Controllable option? If so, what should be default?


Monte
Post by Pete
Could this function go in the standard distrinution as I'm sure
everyone else uses it
Pete
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Post by Paul Lockaby
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
function smarty_modifier_nl2br($message) {
return nl2br($body);
}
Put that in a file by itself, named modifier.nl2br.php and then in your
{ $var|nl2br }
And poof(!) $var will have all newlines replaced with <BR>s. You can
http://smarty.incutio.com/?page=SmartyPlugins
For more ideas on plugins you can use to modify your text. I especially
like the bbcode2html plugin and use a modified version of it on my own site.
Hope that helps!
~Paul
--
Monte Ohrt <***@ispi.net>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul Lockaby
2003-02-26 21:48:06 UTC
Permalink
Post by Monte Ohrt
2) Should it always return XHTML compliant code <br />, regardless of
PHP version? the PHP command nl2br() pre-PHP 4.0.5 doesn't output XHTML
break tags. I think it would be good to always be XHTML compliant.
Since Smarty doesn't even work with versions before 4.0.6, I hardly see
this as relevant. But I could be wrong.

~Paul
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
1LT John W. Holmes
2003-02-26 22:00:15 UTC
Permalink
Post by Monte Ohrt
1) Is there a better name, or keep nl2br for consistancy of PHP
developers familiarity? nl2br is nice and short though...
Keep it.
Post by Monte Ohrt
2) Should it always return XHTML compliant code <br />, regardless of
PHP version? the PHP command nl2br() pre-PHP 4.0.5 doesn't output XHTML
break tags. I think it would be good to always be XHTML compliant.
So you're going to re-implement the function and not use the default
nl2br()? If so, then yeah, always make it output XHTML compliant tags.
Post by Monte Ohrt
3) Should it cover newline formats from Mac/PC appropriately? In
otherwords replace \n or \r\n with <br /> tags?
The regular nl2br() does this already, so yeah, it should.
Post by Monte Ohrt
4) Should it replace newlines already preceeded by a <br> tag?
Controllable option? If so, what should be default?
Yes, replace any newlines. If your text already has HTML code in it, then
you probably shouldn't be calling nl2br() on it anyhow.

Overall, just a wrapper to the regular nl2br() function would be the best, I
think. Why recreate it?

Wouldn't it just be smarter overall to convince people to use the
$smarty->register() method? (which I just learned about) Otherwise it seems
like you'd just end up writing a smarty.modifier file for every PHP function
that people want.

---John Holmes...
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Monte Ohrt
2003-02-26 22:13:53 UTC
Permalink
OK if we don't worry about preceeding <br> tags, then a wrapper to PHP
nl2br() should suffice... Otherwise I have it written:

function smarty_modifier_nl2br($string, $preceed_test = true)
{
if(!$preceed_test) {
return preg_replace('%(?:\r\n|\r|\n)%m', '<br />', $string);
} else {
return preg_replace('%(?:\<br(?:\s*\/)?\>)?(?:\r\n|\r|\n)%', '<br
/>', $string);
}
}

Monte
Post by 1LT John W. Holmes
Post by Monte Ohrt
1) Is there a better name, or keep nl2br for consistancy of PHP
developers familiarity? nl2br is nice and short though...
Keep it.
Post by Monte Ohrt
2) Should it always return XHTML compliant code <br />, regardless of
PHP version? the PHP command nl2br() pre-PHP 4.0.5 doesn't output XHTML
break tags. I think it would be good to always be XHTML compliant.
So you're going to re-implement the function and not use the default
nl2br()? If so, then yeah, always make it output XHTML compliant tags.
Post by Monte Ohrt
3) Should it cover newline formats from Mac/PC appropriately? In
otherwords replace \n or \r\n with <br /> tags?
The regular nl2br() does this already, so yeah, it should.
Post by Monte Ohrt
4) Should it replace newlines already preceeded by a <br> tag?
Controllable option? If so, what should be default?
Yes, replace any newlines. If your text already has HTML code in it, then
you probably shouldn't be calling nl2br() on it anyhow.
Overall, just a wrapper to the regular nl2br() function would be the best, I
think. Why recreate it?
Wouldn't it just be smarter overall to convince people to use the
$smarty->register() method? (which I just learned about) Otherwise it seems
like you'd just end up writing a smarty.modifier file for every PHP function
that people want.
---John Holmes...
--
Monte Ohrt <***@ispi.net>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Juzan Jean-Michel
2003-02-21 23:27:05 UTC
Permalink
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
This is in the manual
{$mytext|regex_replace:"/[\n]/":"<br>"}
Post by gawan
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Jean-Michel Juzan ***@ecreasol.ch

Direction Informations & Technologies
eCreaSol - NetWork Creative Solutions SARL - http://www.ecreasol.ch/

PGP public keys server : http://www.keyserver.net
Monte Ohrt
2003-02-22 00:39:15 UTC
Permalink
register the function:

$smarty->register('nl2br','nl2br');

then in your template:

{$foo|nl2br}

Or, use a modifier like suggested, and the register is unnecessary.

Monte
Post by gawan
Hi
I have text fields from a database to display through smarty template.
Is there a convenient way to use something similar to php nl2br()
to show new lines as <br> tags when in a template?
gawan
--
Monte Ohrt <***@ispi.net>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...