Gravatar for mcandrew@rdacorp.com

Question by Ken McAndrew, Feb 2, 2019 1:48 PM

Raw HTML in a computed field result

I have a scenario with a computer field that generates HTML link markup:

{{ _.each(raw.computedfield, function(item) { }}
<li>{{- item}}</li>
{{  }) }}

Where the "computedfield" field starts out as something like:

<a href="#">Link</a>;<a href="#">Link2</a>

When I loop through, I get the correct values as the raw HTML in text, but I need to translate that into activating as real HTML, like Html.Raw in MVC. Is there a similar construct in Coveo/underscore.js to handle this? I tried wrapping Html.Raw around the -item bit but it wouldn't compile that.

1 Reply
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Feb 4, 2019 1:51 PM

You can find UnderscoreJS templates documentation here: https://underscorejs.org/#template

It says: Template functions can both interpolate values, using:

<%= … %>

as well as execute arbitrary JavaScript code, with:

<% … %>

If you wish to interpolate a value, and have it be HTML-escaped, use:

<%- … %>

That means that = is not HTML escaping the values and you should use it instead of - in this specific use case.

Gravatar for mcandrew@rdacorp.com

Comment by Ken McAndrew, Feb 4, 2019 1:56 PM

Great, thanks. I also found that "split" worked in there and was able to do it that way, with a computed field that I could split (URL and title) and format on the other end.

Ask a question