1

I'm trying to use underscore.js template function on the following code based on http://underscorejs.org/#template and other tutorials

json = {"data": [{"img0": "image.jpg"}]}
var compiled = _.template("image: <%= img0 %>");
compiled(json.data[0]);
document.getElementById("albums").innerHTML = compiled();

but i got this error:

Uncaught ReferenceError: img0 is not defined

Can you explain to me what the problem is?

1

1 Answer 1

1

You need pass arguments to template function when you call it,

var json = {"data": [{"img0": "image.jpg"}]};
var compiled = _.template("image: <%= img0 %>");

document.getElementById("albums").innerHTML = compiled(json.data[0]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="albums"></div>

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

1 Comment

Thanks, i understand now what the problem was :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.