3

Model:

Field('text1', type='text', length=1000, notnull=True)

Function:

def textinput():
f=SQLFORM(db.example, fields=['text1'])
if f.accepts(request.vars, session):
return dict(form=f)

I want to be able to display the 'text1' field with proper line spacing/formatting. If a user was to press the [enter] key in the form to start a new line or whole new paragraph, I want this to reflect on the view.

For example, if a user enters the following into a SQLFORM:

This is a paragraph. Blah blah blah blah blah blah blah.
Blah blah blah blah blah blah blah blah blah.
Blah blah blah blah.

This is another paragraph. Blah blah blah blah blah.
Blah blah blah blah blah.

I want it to come out exactly like that in the view, instead of having it all crammed up with no spacing. How would I go about doing this? I was thinking about using the .replace method and replacing all [enter] keystrokes with a line break, but I'm not sure how to do it. I've searched all over Google but I can't find my exact issue.

Any help would be greatly appreciated. Thanks.

samer.j92
  • 85
  • 1
  • 7

1 Answers1

5

The easiest method is to wrap the text in a <pre> tag and use CSS to control the styling. You can also replace the line breaks ('\n') with <br /> tags. If you are displaying the text via a readonly SQLFORM, a Crud read form, a SQLTABLE, or SQLFORM.grid, then you can set the field's "represent" attribute to control the display:

Using <pre>:

Field('text1', type='text', length=1000, notnull=True,
    represent=lambda text, row: PRE(text))

Using line break replacement:

Field('text1', type='text', length=1000, notnull=True,
    represent=lambda text, row: XML(text.replace('\n', '<br />'),
        sanitize=True, permitted_tags=['br/']))

If you're inserting the text manually into a view, you can simply use either of the above methods directly in the view. For example:

{{=PRE(row.text1)}}

Note, browsers typically display text in a <pre> tag with a fixed-width font. If you don't want that, you'll need to use CSS to change the font.

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • Thanks for the quick reply. The line breaks are getting put in now, but I've still got a problem. The text now stretches the entire column, line breaks no longer automatically occur when a line reaches the width of the column. Even when I set a maximum width for the column, the text still displays outside of the column and displays over all the other content in the website. It now fully relies on the line breaks that I put into the form. Is there any way I could prevent the text from displaying outside the width of the column? – samer.j92 May 14 '12 at 17:31
  • I assuming you're using the `
    ` solution. In that case, try [this](http://stackoverflow.com/a/248013/440323). Note, the line break replacement solution should not have this problem.
    – Anthony May 14 '12 at 18:14
  • Thanks for that, it works now! Appreciate it. Just a quick question, is there any way I can specify a fixed size for the SQLFORM text input widget? I don't want to give users the ability to rescale the widget since it stretches out all the other content in the table and makes everything look messed up. Could I use a widget with a vertical scrollbar instead? – samer.j92 May 14 '12 at 18:54
  • Try something like `textarea {width: 400px; resize: none;}` in your CSS file. – Anthony May 14 '12 at 21:12