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 8741095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:14:50+00:00 2026-06-13T11:14:50+00:00

Right now I’m using the following line of code to retrieve an Underscore template

  • 0

Right now I’m using the following line of code to retrieve an Underscore template and create a DOM element from the template, using jQuery.

var element = $(_.template($('#template').html())());

It works, but I think this looks a bit messy/confusing, so I was wondering if there’s a better way to do this?

  • 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-13T11:14:51+00:00Added an answer on June 13, 2026 at 11:14 am

    Updated for Underscore 1.7+: There’s really not much better you can do in newer versions of Underscore since _.template always returns a function:

    template _.template(templateString, [settings])
    Compiles JavaScript templates into functions that can be evaluated for rendering.

    You used to be able to say _.template(tmpl, data) (see below) to get a filled-in template but no longer.

    You can however hide some of the parentheses inside a function with things like:

    var tmpl_to_html = function(id) {
        var $el = $('#' + id);
        var fn  = _.template($el.html());
        return fn();
    };
    var $e = $(tmpl_to_html('template'));
    

    or:

    var tmpl_to_html = function(id, data) {
        var $el = $('#' + id);
        var fn  = _.template($el.html());
        return fn(data);
    };
    var $e = $(tmpl_to_html('template'));
    var $x = $(tmpl_to_html('other', { a: 'b' }));
    

    For old versions of Underscore: You can get the filled in template from _.template by supplying the data parameter:

    template _.template(templateString, [data], [settings])
    […]
    If you’re writing a one-off, you can pass the data object as the second parameter to template in order to render immediately instead of returning a template function.

    Anything truthy will probably due for data but an empty object is probably your best bet:

    var $e = $(_.template($('#template').html(), { }));
    

    That’s still a bit noisy but you can always wrap the _.template call in another function:

    var tmpl_to_html = function(id) {
        var $el = $('#' + id);
        return _.template($el.html(), { });
    };
    var $e = $(tmpl_to_html('template'));
    

    How you bust it into functions depends on your preference what parts you’d be using elsewhere.

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

Sidebar

Related Questions

Right now, I've just some code which fetches the picture from the URL directly.
Right now I'm using the jQuery selector $('foo+bar') . Since I already have $('foo')
Right now I'm using the following command to compile a coffeescript file each time
Right now I am updating user_list List using jquery and submitting the form data
Right now I am listing all of a users microposts using the code below.
Right now im using css to create an onclick image change. The problem is
Right now I am using sqlite within a ios application, and I want to
Right now I am migrating to Amazon S3 from standard server. In current server,
Right now I have the following query: User.find(:all, :conditions => [guest = ? AND
Right now, I have basic code for moving the textfield above the keyboard when

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.