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

  • Home
  • SEARCH
  • 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 7723671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:30:03+00:00 2026-06-01T04:30:03+00:00

EDIT: Forgot to remind the reader that I remembered to set templateSettings as follows:

  • 0

EDIT: Forgot to remind the reader that I remembered to set templateSettings as follows:

_.templateSettings = {
    interpolate : /\{\{([\s\S]+?)\}\}/g
};

I’m having a hard time getting a varialbe to interpolate in underscore, while running my Jasmine specs. Given the template, rendering method, and jasmine test below, I am able to get the template to interpolate variables properly via:

_.template(
  boneHeaderInstance.template.html(), 
  { id:boneHeaderInstance.id,  
    columns:boneHeaderInstance.columns
  }
)

While this fails to interpolate the columns variable:

boneHeader = Backbone.View.extend({
  el: $('#boneTableHeader'),
  template: $('#boneTableHeaderTemplate'),
  initialize: function(){
    this.id = 'boneTableHeader';
    this.el = $( '#' + this.id );
    this.columns = 'blah';
    this.template = $( '#' + this.id + 'Template' );
    this.render();
    return this;
  },
  render: function(){
    var that = this;
    var data = {id: that.id, columns: that.columns}
    this.el.html( _.template( this.template.html(), data ) );
  }
});

Template:

<script type = 'text/template' id = 'boneTableHeaderTemplate'>
  <tr id = "{{obj.id}}Row">
    {{obj.columns}}
  </tr>
</script> 

In Render Method:

render: function(){
  var that = this;
  var data = {id: that.id, columns: that.columns}
  this.el.html( _.template( that.template.html(), data ) );
}

Jasmine Test:

describe('boneHeader', function(){
  beforeEach(function(){
    boneHeaderInstance = boneTableInstance.header;
  }); 
  describe('rendering', function(){
    it('should have expected html', function(){
      expect( 
        boneHeaderInstance.el.html().replace(/\s\t\n/ , '', 'g') 
      ).toEqual( 
        _.template(boneHeaderInstance.template.html(), 
        { id:boneHeaderInstance.id,  
          columns:boneHeaderInstance.columns
        }).replace(/\s\t\n/ , '', 'g') 
      );
    }); 
  }); 
});

Jasmine Result:

Expected ' <tr id="boneTableHeaderRow"></tr> ' to equal ' <tr id = "boneTableHeaderRow"> blah </tr> '
  • 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-01T04:30:05+00:00Added an answer on June 1, 2026 at 4:30 am

    You have various problems. First of all, Underscore uses <% %> for templates unless you change it with something like:

    _.templateSettings = {
      interpolate : /\{\{(.+?)\}\}/g
    };
    

    So your template should look like this:

    <script type = 'text/template' id = 'boneTableHeaderTemplate'>
        <tr id = "<%= obj.id %>Row">
            <td><%= obj.columns %></td>
        </tr>
    </script>
    

    I’ve also fixed the HTML error you had in your template, you can’t have a text node as an immediate child of a <tr> and there’s no telling what sort of chicanery a browser will get up to if you try such a thing.

    Secondly, _.template() is usually used to return a compiled version of a template and that compiled version is a function that you execute to get the final HTML:

    var t    = _.template(some_template_html);
    var html = t(data);
    

    So you probably want something like this in your constructor:

    this.template = _.template($('#' + this.id + 'Template').html());
    

    and this in your render:

    this.el.html(this.template(data));
    

    You can do it all at once with _.template(template_html, context) though.

    Thirdly, you’re referencing obj.id and obj.columns in your template but you’re only giving it id and columns so either drop the obj. prefixes from your template or alter data thusly:

    var data = {
        obj: {
            id: that.id,
            columns: that.columns
        }
    };
    

    Demo: http://jsfiddle.net/ambiguous/NYLqH/

    You’ll have to fix your test to account for the corrected HTML of course.

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

Sidebar

Related Questions

EDIT: forgot a line of code that is likely causing the issue (night's sleep
EDIT Sorry I forgot the most important part here. Each key can have more
EDIT: I was an idiot. I simply had an image that was vertically long,
EDIT: See my answer below--> I am wanting to have a view that when
EDIT: iam using ajax to load text in my content that is why onload
Edit : Note that, as Daniel and latkin noted in an answer and a
EDIT : It turned out that this can only be done through an external
I've never seen this before. I'm seg faulting when comparing two integers. EDIT: Forgot
EDIT: forgot to include my environment info... Win7x64, RubyInstaller Ruby v1.9.1-p378 EDIT 2: just
Installed latest version of pandas 0.9.0 in case this was an error. EDIT: forgot

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.