Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace carriage returns with <br /> using freemarker and spring?

I've got an internationalised app that uses spring and freemarker. I'm getting content from localised property files using.

${rc.getMessage("help.headings.frequently_asked_questions")}

For some of the content there are carriage returns in the property values. Because I'm displaying in a web page I'd like to replace these with
.

What is the best way to do this?

Edit: looking closer it seems that I don't actually have carriage returns in the property files. The properties are coming back as single line strings.

Is there a better way to declare the properties so they know they are multi-line?

help.faq.answer.new_users=If you have not yet set a PIN, please enter your username and passcode (from your token) in the boxes provided and leave the PIN field blank.\
You will be taken through the steps to create a PIN the first time you log in.

Cheers, Pete

like image 220
Programming Guy Avatar asked Jun 17 '11 05:06

Programming Guy


People also ask

How do you replace a carriage return in Java?

s = s. replaceAll("\\n", ""); s = s. replaceAll("\\r", "");

How do you escape characters in Freemarker?

esc creates a markup output value out of a string value by escaping all special characters in it.

How do I print a value in Freemarker?

For example, if you create a variable called "foo" in the template, you can print its value with ${foo} . If, coincidently, there's a variable called "foo" in the data-model too, the variable created in the template will hide (not overwrite!)


2 Answers

${springMacroRequestContext.getMessage("help.headings.frequently_asked_questions", [], "", false)?html?replace("\n", "<br>")}

like image 80
Jiří Vypědřík Avatar answered Sep 20 '22 06:09

Jiří Vypědřík


To handle CR + LF (carriage return + line feed) line endings, as well as just LF do this:

<#escape x as x?html?replace("\\r?\\n","<br />",'r')>...</#escape>
like image 30
Joman68 Avatar answered Sep 20 '22 06:09

Joman68