Line breaks when showing customer notes in email

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
jguengerich
Commodore (COMO)
Commodore (COMO)
Posts: 436
Joined: Tue May 07, 2013 1:59 pm

Line breaks when showing customer notes in email

Post by jguengerich » Mon Aug 24, 2015 10:43 am

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.
Jay

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Line breaks when showing customer notes in email

Post by mazhar » Tue Aug 25, 2015 3:09 am

I am not sure but did you tried

Code: Select all

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

jguengerich
Commodore (COMO)
Commodore (COMO)
Posts: 436
Joined: Tue May 07, 2013 1:59 pm

Re: Line breaks when showing customer notes in email

Post by jguengerich » Tue Aug 25, 2015 6:36 am

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;" :)?
Jay

Post Reply