If you look at the function below, on line 11 where it alert(template);. It prints undefined. If I alert(template); inside the ajax succes
alert(template);
undefined
template isnt defined until your ajax request is completed.
thats why it shows fine in the success function.
not sure what framework that is ( JQuery I presume ) but you need to do the callback in the ajax success function.