[Solved] Handlebar snippets rendered as text instead of html

I have a piece of code that loads in a html template with handlebar expressions in it.
The html is loaded into docHTML based on values in a Javascript object (layout of this object is not really important for this example).
Then it will go through the text in docHTML and does a find/replace for any placeholder that I defined and replaces it with a handlebar snippet.
As I can define multiple placeholders it checks for this in a for loop.
After this is done I have the resulting text and then render the final HTML with the Handlebars.render.
And finally I make sure that everything is displayed on the document.

I notice that when I render the main HTML with handlebar expressions it works fine but the html in the handlebar snippet gets rendered as text.
I have already used a triple { and } to make sure this is rendered as HTML but this does not seem to work.

Do note that the information is read in fine, it is just represented as text (only the snippet and not the main html) which it should not do.
How can I make sure that everything is properly rendered as HTML instead?

// First load the handlebar content
docHTML = loadtext("file:///" + gDocument.replacements[i].value);

// Also check if there are any handlebar snippets in the content that need to be replaced
for (var j = 0; j < gDocument.handlebars.length; j++) {
	docHTML = replaceAll(docHTML, gDocument.handlebars[j].placeholder, "{{{+ file:///" + replaceAll(gDocument.handlebars[j].value, "\\", "/") + " }}}");
}

// Render the handlebar information (if any)
docResult = Handlebars.render( docHTML, { gTexts, gDocument, record });

// Only do something if we can load the file
if (docResult != "") {
	// And now replace the placeholders with the loaded information
	result.replaceWith(docResult);
}

function replaceAll(str, find, replace) {
  // Escape special characters for use in a regular expression
  const escapedFind = find.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  return str.replace(new RegExp(escapedFind, 'g'), replace);
}

I found out this only happens when I use a helper that I registered and I just return the string.
When I use a “return new Handlebars.SafeString(returnvalue)” instead it returns the information as HTML, which I needed.

1 Like

I’m having the same(?) issue I think but modifying the returun value didn’t solve it.

My handlebar is defined as

Handlebars.registerHelper('verbo', function(tu, voi){
	sesso = this.Receiver__Sex
	
	if (sesso == 'M' || sesso == 'F'){
		return tu
	}
	
	if (sesso == 'S') {
		return voi
	}
	
	return 'KO DECLINAZIONE VERBO TU VOI'
})

and when I call {{ verbo A B}} inside a dynamically placed HTML the resuling paragraph contains the raw handlebar call instead of its result.

I’ve tried to replace the return value with return new Handlebars.SafeString(tu) with no success.

Am I understanding wrongly your solution?

See Handlebar not executed for dynamically loaded HTML