sk_225
sk_225

Reputation: 425

Trademark replace in underscore?

My code below in my HTML underscore template is literally printing out

 "<sup>&reg;</sup>" 

on the page instead of the trademark replacement (®) I want the below code to do. What's wrong with my code? EDIT: I want designation.title and designation.description to print to the page, just replace the (R) in there with ®.

<%- designation.title.replace(/(®)/ig, "<sup>&reg;</sup>") %>
<%- designation.description.replace(/(®)/ig, "<sup>&reg;</sup>")%>

Upvotes: 1

Views: 110

Answers (3)

rockerest
rockerest

Reputation: 10508

Use <%= instead.

<%- escapes HTML, <%= does not.

Upvotes: 1

Ronnie Smith
Ronnie Smith

Reputation: 18565

Create a DOM element and put your string in it. In your case, hand the el object to the method, not the string?

var output = document.getElementsByTagName('div')[0];
var output2 = document.getElementsByTagName('div')[1];
var el = document.createElement( 'div' );
el.innerHTML = "<sup>&reg;</sup>";

var content = document.createTextNode("<sup>&reg;</sup>");

output.appendChild(el);

// this does not work
output2.appendChild(content);
<div></div>
<div></div>

Upvotes: 0

Paul
Paul

Reputation: 141829

You're using the type of template function that does automatic escaping for HTML: <%- instead of <%. Using that will cause underscore to escape everything so that it can be output as is (output without being interpreted as HTML, so < becomes &lt;, & becomes &amp;, etc.

If you want to output raw HTML just drop the - off <%-, but that can be unsafe, depending on the rest of designation.title and if it has any characters that need escaping.

Upvotes: 2

Related Questions