Search code examples
javascriptbackbone.jsunderscore.js-templating

Underscore template: "<string" not displayed, but "< string" is


I found a strange problem with the underscore templates.

<string does not display the string with html encoded < but returns an empty string instead. But < string works correctly. Note the space required.

jsfiddle: http://jsfiddle.net/BwHxv/281/

In the fiddle, try adding a space, so you'll see that it works correctly.

Any particular reason? If a user accidentally types this character, it should not turn into an empty string.


Solution

  • The HTML string <string is the start of a "string" element. It's nonsense, so your browser doesn't show anything.

    The HTML string < string is just text. It's valid, so your browser shows the text is represents.

    The bug is that you pass plain text to a method expecting HTML. The simplest fix is to change

    .html
    

    to

    .text