0

Ran into some weird behavior using underscore.js's template method:

var test = ['first', 'test'];
console.log(_.template('this is a <%= 1 %> ', test));

(Fiddle: http://jsfiddle.net/adamb/mbD6E/)

This snippets outputs: "this is a 1", while the expected result is "this is a test".

No errors are thrown. I've studied the source-code and there's no obvious explanation for this result. It's required that I use interpolation only in my template. I've tried converting the array explicitly to a collection using _.extend({}, test), but no dice.

How can I get this working?

2
  • The template function expects a object, not an array. Commented Nov 6, 2013 at 19:50
  • @Jack When I explicitly convert it to an object I get the same result (as noted in the question.) Please see fiddle for that specific example. Commented Nov 6, 2013 at 19:51

2 Answers 2

3

You can check the compiled template code with:

_.template('this is a <%= 1 %> ').source

which gives this function:

function(obj){
var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
with(obj||{}){
__p+='this is a '+
( 1 )+
' ';
}
return __p;
}

the key part is:

with(obj){
  (1)
}

because 1 isn't a valid property/variable name, it doesn't try to match the properties of the object you are templating, so just prints out '1'.

if you were to use an object like {zero: 'first', one: 'test'} and a template of 'this is a <%= one %> ', then this would look like:

with(obj){
  (one)
}

which is kind of equivelant of obj.one

Sign up to request clarification or add additional context in comments.

Comments

1

A number is not a valid javascript reference.

Here you probably want to write it this way: <%= this[1] %>.

If that's not working, then pass your array to an object key:

_.template('this is a <%= list[1] %> ', { list: test })

3 Comments

<%= this[1] %> doesn't work, while the second snippet works.
Although it's not a valid reference, I thought (perhaps hoped) that it would attempt to retrieve the value from the collection via bracket notation, in which case test[1] is valid.
1 is a valid expression and <%= ... %> wraps expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.