So lets say I generate content from an external handlebars template (through AJAX)
$.ajax({
url: path,
dataType: 'html',
success: function(data) {
source = data;
template = Handlebars.compile(source);
//execute the callback if passed
if (callback) callback(template);
}
});
Now inside my template.handlebars I output the data I sent using the tags {{this}} etc. But I also include some javascript that are only meant for that template on the bottom of the template (which works fine). However I need to access some of the data sent to the template inside the javascript, is it possible?
A really dumb example which I know doesn't work but should explain what I'm after incase it's unclear. So this could be an example of my template.handlebars file:
<h1>{{question}}</h1>
<script>
//want to access the data sent to template in the JS included
(function($) { //wrap to local scope
$(document).ready(function() {
//this doesn't work ofcourse.. but should explain what I'm after
var question = {{question}};
console.log(question)
});
})(jQuery);
Any ideas on how to do that? For me it feels clean to include the JS that is only meant for the particular template this way.