I am trying to access an object in an array using a global variable. However, it seems that Handlebars does not allow you to do that. Is there a way around this problem?
This is my code:
<!DOCTYPE html>
<html>
<head>
<title>Handlebars.js example</title>
</head>
<body>
<div id="placeholder">This will get replaced by handlebars.js</div>
<script type="text/javascript" src="handlebars-v1.3.0.js"></script>
<script id="myTemplate" type="text/x-handlebars-template">
<table>
<tbody>
<tr><th>testing</th><td> <input type="text"><td></tr>
{{!names[index] is not working}}
------> {{#each names.[index]}}
<tr><th> {{this}}</th><td><input type="text"><td></tr>
{{/each}}
</tbody>
</table>
</script>
<script type="text/javascript">
var source = document.getElementById("myTemplate").innerHTML;
var template = Handlebars.compile(source);
var index=0;
var data = {
names: [
{ name: "foo",id:'id',type:"type"},
{ name: "bar",id:'id' },
{ name: "baz",id:'id' }
]};
document.getElementById("placeholder").innerHTML = template(data);
</script>
</body>
</html>
I know in this example that I don't need to use a global variable, but with the code that I am using I need to. Please no JQuery, as I can't use it.
EDIT:This is what I am trying to do in the each helper. I am going through each of the attributes of one object. In order to do so, I must select an object at a particular index like names.[0] for example.