Creating paragraph breaks in form text area

5,375 views
Skip to first unread message

Kevin

unread,
Feb 20, 2012, 5:11:30 AM2/20/12
to Lift
How to do this in Lift?

http://www.webmasterworld.com/forum88/4829.htm

"So, when you type into a text area in a form (like I'm doing now to
create this post) if I hit return twice
to create a new paragraph

it works ... a new paragraph is created in which I can continue my
latest question.

But...in my form it doesn't work. Got a form that's being inserted in
a Mongodb, but when I view the form's contents (in html via php) all
the paragraph breaks I've entered disappear and I get one long line of
text."

Kevin

unread,
Feb 20, 2012, 5:11:55 AM2/20/12
to Lift
Thanks,

Sören Kress

unread,
Feb 20, 2012, 6:14:33 AM2/20/12
to lif...@googlegroups.com
Hi Kevin,

the problem is that the newlines are stored as such in the database (\n), but HTML doesn't care about it. You have to convert the newlines into <br /> elements before printing it. Something like this should help:

def stringWithNewlineToNodeSeqWithBr(s: String): NodeSeq = s.split("\n").toList.flatMap(x => Text(x) ++ <br/>).dropRight(1)

Best regards
Soeren


Kevin

unread,
Feb 20, 2012, 8:50:03 AM2/20/12
to Lift
that is getting so much closer. thanks.

However, say, how to "insert" this (\n) in the SHtml.ajaxText or
SHtml.ajaxTextArea? If the functions are not unsuitable for this
purpose, what is the alternative?

Sören Kress

unread,
Feb 20, 2012, 9:04:02 AM2/20/12
to lif...@googlegroups.com
Inside a textarea the \n should be ok. I.e. if you take a user's input from a textarea and write it in the database, you get a string that contains newlines in form of escape sequences. If you want to display it as HTML you have to convert \n to <br />. If a user should edit his text, then you simply use the string from the database (without converting \n to <br />).

Potentially you have to use \n, \r and \r\n wherever I said \n. End of line characters are platform dependent: http://www.wilsonmar.com/1eschars.htm

Best regards
Soeren

David Pollak

unread,
Feb 20, 2012, 10:12:28 AM2/20/12
to lif...@googlegroups.com
Or you can wrap the resulting string in a <pre> block:

val s: String = get the string from the database
<pre>{s}</pre>


--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


Reply all
Reply to author
Forward
0 new messages