The underscore template documentation suggest the following should be possible, yet it is not working for me. Executing the template simply returns nothing for this.
<% FB.api('/me', function(response){ %>
<%= response.name %>
<% }); %>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s a perfectly valid template; in fact, the compiled JavaScript version looks like this (reformatted for readability):
And there’s nothing wrong with that. BTW, you can look at the
sourceattribute of a compiled Underscore template if you want to see the JavaScript for the template:However, it won’t do what you’re expecting it to do. Your problem is that the
FB.apicall is an AJAX call and A stands for asynchronous. So by the time your callback gets called (i.e.<%= response.name %>is executed), the template will have been converted to HTML and added to the DOM and nothing will be looking at the__pvariable anymore. The sequence looks something like this:FB.apigets called.FB.apicallback gets called.response.nameis appended to the__pbuffer.You’re going to have to turn your logic inside out a bit. Your
FB.apicall should be outside your template:so that you don’t try to use the template until all the data is ready.