2

I seem to be having issues with leading/trailing spaces in textareas!

If the last user has typed values into a textarea with leading/trailing spaces across multiple lines, they all disappear with exception to one space in the beginning & end.

Example: If the textbox had the following lines: (quotes present only to help illustrate spaces)

"   3.0"
"  2.2  "
"0.3   "

it would be saved in the backend as

"<textarea id=... >   3.0/n  2.2  /n0.3   </textarea>"

My template (for this part) is fairly straightforward (entire template, not as easy...):
${label} ${textField}

When I load up the values again, I notice getTextField() is properly getting the desired string, quoted earlier... But when I look at the html page it's showing

" 3.0"
"2.2"
"0.3 "

And of course when "View Sourcing" it doesn't have the string seen in getTextField()

What I've tried:

  • Ensure the backend has setWhitespaceStripping(false); set
  • Adding the <#ftl strip_whitespace=false>
  • Adding the <#nl> on the same line as ${textField}

No matter what I've tried, I'm not having luck keeping the spaces after the interpolation. Any help would be very appreciated!

scottnaka
  • 65
  • 1
  • 10

1 Answers1

2

Maybe you are inside a <#compress>...</#compress> (or <@compress>...</@compress>) block. Those filter the whole output on runtime and reduce whitespace regardless where it comes from. I recommend not using this directive. It makes the output somewhat smaller, but it has runtime overhead, and can corrupt output in cases like this.

FreeMarker interpolations don't remove whitespace from the inserted value, or change the value in any way. Except, if you are lexically inside an <#escape ...>....</#escape>, block, that will be automatically applied. But it's unlikely that you have an escaping expression that corrupts whitespace. But to be sure., you can check if there's any <#escape ...> in the same template file (no need to check elsewhere, as it's not a runtime directive).

strip_whitespace and #nt are only removing white-space during parsing (that's before execution), so they are unrelated.

You can also check if the whitespace is still there in the inserted value before inserting like this:

${textField?replace(" ", "[S]")?replace("\n", "[N]")?replace("\t", "[T]")}

If you find that they were already removed that probably means that they were already removed before the value was put into the data-model. So then if wasn't FreeMarker.

ddekany
  • 29,656
  • 4
  • 57
  • 64
  • Thanks for a response! Appreciate the info about strip_whitespace and #nt not being useful in this case. I was quite confused about that, heh.I tried using your advice with the replace and indeed the [S]'s and [N]'s show up, so it looks like it's making it that far. And there definitely are no <#escape> blocks in this template. So, I suppose this means another template may be using mine which is forcing compression on it? Do you any other ways compression may be active? – scottnaka Jan 08 '13 at 22:36
  • Ahha! Never mind, found the <#compress> that was causing all the troubles. Thank you so much for the help! – scottnaka Jan 08 '13 at 23:40