Page 1 of 1

Line breaks when showing customer notes in email

Posted: Mon Aug 24, 2015 10:43 am
by jguengerich
When a customer enters an order note, it may have line breaks ("\r\n" or "\n", depending on browser and version). When an email template is processed that includes a comment, I would like to convert these line breaks to HTML <br /> tags. I tried this:

Code: Select all

$orderNote.replace("\r\n", "<br />")
The replacement isn't done, however. After searching the web, I tried with multiple (up to 4) "\" before the r and n, but that doesn't work either. Replacing one letter with another does work:

Code: Select all

$orderNote.replace("e", "z")
Does anyone know how to get .replace to replace the line breaks?

I could also "pre-process" the note(s), replacing the line breaks with <br />, then add a list of the pre-processed note(s) to the email template as a parameter and show it (them) instead of the original order note(s), but that seems like a messy workaround.

I'm still on R5 if that makes a difference.

Re: Line breaks when showing customer notes in email

Posted: Tue Aug 25, 2015 3:09 am
by mazhar
I am not sure but did you tried

Code: Select all

$orderNote.Replace("\\r\\n", "<br />")

Re: Line breaks when showing customer notes in email

Posted: Tue Aug 25, 2015 6:36 am
by jguengerich
First a correction to my original code: It is $orderNote.Comment.replace(...), not $orderNote.replace(...).

As I metioned in my original post, I tried "\r\n", "\\r\\n", "\\\r\\\n", and "\\\\r\\\\n", and none of them work.

I did a little more searching this morning, and found a suggestion to put "\r" and "\n" into the context, so I added two lines to the Process method in CommerceBuilder/Messaging/NVelocityEngine.cs:

Code: Select all

        public string Process(Hashtable parameters, string template)
        {
            string retVal = string.Empty;
            using (StringWriter writer = new StringWriter())
            {
                using (StringReader templateReader = new StringReader(template))
                {
                    VelocityContext context = new VelocityContext(parameters);
                    context.Put("helper", new NVelocityHelper());

                    // following two lines allow .replace("${r}${n}", "<br />") on strings in email templates
                    context.Put("n", "\n");
                    context.Put("r", "\r");

                    this.Evaluate(context, writer, "AbleCommerce", templateReader);
                }
                
                retVal = writer.ToString();
            }
            return retVal;
        }
Then you use it in replace like this:

Code: Select all

$orderNote.Comment.replace("${r}${n}", "<br />")
It works, but I get slightly different HTML in the email preview in /Admin/Orders/Email/SendMail.aspx compared to the email that is actually sent (from that preview page or by the system when an order is placed). The preview uses <br></br> (as shown by browser's developer tools), while the actual email just contains <br> (as shown by email client's view source). I don't know if AbleCommerce/.Net/NVelocity is actually generating different HTML, or if the browser/email server/email client are "fixing" the HTML as they prefer.

Hmm.., now I realize spaces at the beginning of lines are collapsed by HTML renderers. Should I figure out how to replace them with "&emsp;" :)?