I'm using (or at least starting with) HandlebarsJS for the html templates but I might have hit a dead end. What I want is to pass a function to the template, e.g.
<div id="divTemplate">
<span onclick="{{func}}">{{text}}</span>
</div>
and then I would expect to have something like
var source = $('#divTemplate').html();
var template = Handlebars.compile(source);
var data = {
"text": "Click here",
"func": function(){
alert("Clicked");
}
};
$('body').append(template(data));
But the function is executed on init, it is not passed into the template and the result is:
<span onclick="">Click here</span>.
I was trying some stuff with the helper functions as well but I couldn't make it work too. Any ideas would be appreciated. :)
See Question&Answers more detail:os