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

Add "\n" to tags in nested HTML

Community Beginner ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

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

Views

70
Translate

Report

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

Copy link to clipboard

Copied

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>

Votes

Translate

Report

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