There is a round-about way to retain HTML
comments after template evaluation, but it comes with its owns issues.
You first enclose the comments in your template in CDATA
tags. See example below:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<![CDATA[<!-- This is a comment -->]]>
<p>GET - Gmail Push Notification Endpoint</p>
</body>
</html>
When evaluated you'll end up with the following:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<!-- This is a comment -->
<p>GET - Gmail Push Notification Endpoint</p>
</body>
</html>
Notice that certain characters (the <
and >
) are encoded as html entities, so you need to convert these back somehow. Unfortunately, Javascript does not have a native way to do that, so you either have to write your own script to replace them or use an existing library. I do the later and leverage the he
library. You can create a script file in your Apps Script project and drop that code in as a dependency or put it in it's own project and deploy it as a dedicated Apps Script library.
Now you can bring it all together as follows:
// create template
let template = HtmlService
.createTemplateFromFile('template_with_comments_wrapped_in_CDATA_tags');
// add properties to template for evaluation
template.props = {...};
// get evaluated content as string
let evaluated = template
.evaluate()
.getContent();
// decode html entities
let decoded = he.decode(evaluated);
// return decoded as HtmlOutput
return HtmlService.createHtmlOutput().setContent(decoded);
And there you go.
Enclosing content in a template with CDATA
tags has a number of applications, it even allows you to use non-HTML content as templates, so you can use this technique to create templates for all sorts of content (JSON, RFC822, etc.). Its a good trick to have in your arsenal of tools.