I'm trying to pass an object to an underscore.js (1.8.3) template and use it in the template script. Simple example from the console:
> t = _.template("<% console.log(data) %>", {data: 42})
> t()
Uncaught ReferenceError: data is not defined
What is the correct way to pass an object to a template script for use inside?
Note: I know for substituting simple values, just do this:
t = _.template("<%= data %>")
t({data:42})
<- "42"
But I really need to pass and use objects in a more complicated script.
Update
This works:
> t = _.template("<% console.log(data) %>")
> t({data: [1,2,3,4,5]})
I swear I'm not crazy (well not much)... I was looking at this really good tutorial on backbone.js where he passes the object like template("...", object)
. Perhaps this is the way they did it in an older version of underscore.js (version 1.4.2 in the video)?
Since version 1.7.0 Underscore templates no longer accept an initial data object. _.template
always returns a function now.