Display line breaks in your Mendix strings

I was asked the following question recently:

Do you know if it is possible to use a new-line character when creating an attribute in Mendix?  I've created an attribute that I would like to display as such:
Attribute Name
Entry 1
Entry 2
Entry 3
Where each Entry is generated via a loop.  I've tried every new-line character combination and trick I know and can find on the internet ( '\n', '\\n', 'newline', hard coding the return, hard coding the return with a character in the first and last slot, hard coding spaces, etc.).  All of the questions about this topic that I've run into on the Mendix forums have claimed that '\n' or '\\n' work but these do not work for me.  I ran into one question on the forums (whichI can't seem to find now) where someone asked the exact question I have and also said that none of the answers he received worked.

The good news is that I can help. I, too, struggled with this topic early on in my Mendix development. I'm sure I read many of the same forum posts this individual did as well as I recognize the proposed solutions. Here's how I deal with line breaks in my Mendix strings.

Determine the display medium

Are you planning to show the data in a text box on a page, or are you going to send it to a user as the body of an email? These two examples require a different solution.

Display on a page

If you simply want to display the information on a page, you can use the carriage return (CR) technique within the sting formula when building as a loop. By the way, does anyone still call it a carriage return? Ok, line break then. Here's an example of the loop described in the question:


In this example, as I iterate through the ExpenseWorkflow lines, I want to add the data from an attribute into a string variable to be shown on screen line after line. There's a few extra things going on in this loop such as how I deal with the first line versus the rest I add (so I don't get an extra line break at the start of my string) but that isn't really important to the question. Instead, focus on the bottom 'Change variable v_ExistingReviewerNotes' action after the 'false' from the first$ exclusive split. Here is what that looks like:


I literally type the 'Enter' key after "...+ '", do it again to create the line break, then close the single quote. That stores a line break in the string and will result in a text box page view of:

Entry 1
Entry 2
Entry 3

Yes, it really is that simple. However, there is a flaw to this method: It does not translate in a Rich Text Format like an Email Template. The result will ignore the line break and smash everything together again.

Display in an email or Rich Text box

This one is a little trickier but not really once you get the hang of it. It requires building a String attribute (preferably unlimited) and usually in a non-persistent entity and the use of the HTML <br \> break tab.

Once you've created the string attribute your microflow should write the data into that string, adding a <br \> tag at the end of each line that you want to create a line break after. Here's an example outside of a loop but it works the same:


Now if you pass this directly to the Email template through the Rich Text template, you'll achieve the same result in the body of your email if they accept Rich Text format, which almost all modern email applications do (just make sure they've added your domain address as a trusted source!). If you want to display that text on a web page, just use a rich text viewer in your page the like the one's available on the Mendix App Store (just search 'Rich Text'). This gives you a whole host of options when displaying text by embedding HTML tags that can be interpreted accordingly, bringing a lot of customization options to your messaging.

I hope this helps anyone else who like me once struggled with line breaks.