1

I know there's already a question on converting line breaks to <br> tags in Thymeleaf, but the "#strings.replace" solution from there, relies on getting the line.separator system property to make it platform neutral. Since Thymeleaf 3.0.12 this is now broken (see this GitHub issue) when used with th:utext. Is there a recommended replacement for this, other than just using "\n"? For reference, the current line (which we use all over our codebase) looks something like this:

<div th:utext="*{#strings.replace(#strings.escapeXml(fieldName),T(java.lang.System).getProperty('line.separator'),'&lt;br&gt;')}">
Raj
  • 125
  • 1
  • 11
  • 2
    In the same thread, you have mentioned, there is the best solution (from my point of view), which unfortunately doesn't mark as the answer, is to use escaped text `th:text` with `style="white-space: pre-wrap"`. In the end, you'll get safe and formatted content. This doesn't answer your Thymeleaf question but just think about it. – Slava Ivanov Jan 27 '21 at 17:13
  • Thanks, in the end I just went with `' '` instead of `line.separator` – Raj Feb 05 '21 at 11:28

1 Answers1

1

I made a Thymeleaf dialect that makes it easy to keep the line breaks, if the css white-space property isn't an option. It also bring support for BBCode if you want it. You can either import it as a dependency (it's very light) or just use it as inspiration to make your own. Check it out here : https://github.com/oxayotl/meikik-project