Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:31:35+00:00 2026-06-09T11:31:35+00:00

The underscore template documentation suggest the following should be possible, yet it is not

  • 0

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 %>
<% }); %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:31:37+00:00Added an answer on June 9, 2026 at 11:31 am

    That’s a perfectly valid template; in fact, the compiled JavaScript version looks like this (reformatted for readability):

    function(obj) {
        var __p = '';
        var print = function() { __p += Array.prototype.join.call(arguments, '') };
        with(obj || {}) {
            __p += '\n';
            FB.api('/me', function(response) { 
                __p += '\n' + response.name + '\n';
            }); 
            __p += '\n';
        }
        return __p;
    }
    

    And there’s nothing wrong with that. BTW, you can look at the source attribute of a compiled Underscore template if you want to see the JavaScript for the template:

    var t = _.template(raw_template);
    console.log(t.source);
    

    However, it won’t do what you’re expecting it to do. Your problem is that the FB.api call 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 __p variable anymore. The sequence looks something like this:

    1. Compile the template and call the compiled template function.
    2. FB.api gets called.
    3. The template function returns some HTML.
    4. The HTML from 3 is added to the DOM.
    5. Time passes.
    6. Facebook responds and your FB.api callback gets called.
    7. response.name is appended to the __p buffer.

    You’re going to have to turn your logic inside out a bit. Your FB.api call should be outside your template:

    var t = _.template(...);
    FB.api('/mu', function(response) {
        var html = t({ response: response });
        // Somehow add html to the DOM
    });
    

    so that you don’t try to use the template until all the data is ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use underscore function in jade template, like this p= _.keys(user) Not
I am using underscore.js's templating capabilities from backbone.js, I have the following template that
I use Underscore template . It is possible to attach a external file as
I'm using the underscore.js templating function and have done a template like this: <script
Should client-side templates like the following (using underscore's templating engine): <p class=foo><%= bar %></p>
I'm using an underscore template to create an HTML list, this list contains a
How to set up variables inside an underscore.js template for an app built with
I am using the _.template() function of underscorejs together with backbonejs. When using underscore.js
In underscore.js the for each method starts with the following: var each = _.each
What does the underscore mean in the following regex? [a-zA-Z0-9_] The _ seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.