Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add "\n" to tags in nested HTML

Participant ,
Mar 04, 2025 Mar 04, 2025

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

171
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2025 Mar 05, 2025
LATEST
<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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources