Skip to main content
Inspiring
March 4, 2025
Question

Add "\n" to tags in nested HTML

  • March 4, 2025
  • 1 reply
  • 158 views

I need to prepare html for sending via the Postmark API.

Given the following:

<cfset vHTML = "<html><body><div>This is html</div></body></html>">

What would be the cleanest way to arrive at:

<cfset vPostmarkHTML = "<html>\n<body>\n<div>This is postmark html</div>\n</body>\n</html>">

i.e. surround every nested tag group with "\n" except the outer <html> group

    1 reply

    BKBK
    Community Expert
    Community Expert
    March 5, 2025
    <cfset vHTML = "<html><body><div>This is a div.</div><div>This is a second div.</div></body></html>">
    
    <!--- Test both versions of line-feed, and see which fits your needs. --->
    <!---<cfset LF=chr(10)>--->
    <cfset LF="\n">
    
    
    <cfset vPostmarkHTML = vHTML.replaceNoCase("<body>","#LF#<body>").replaceNoCase("<div>","#LF#<div>","all").replaceNoCase("</body>","</body>#LF#").replaceNoCase("</div>","</div>#LF#","all")>
    
    <cfoutput>
    	<strong>vPostmarkHTML</strong><br>
    	#vPostmarkHTML#
    </cfoutput>