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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:16:12+00:00 2026-06-04T16:16:12+00:00

I am using underscore template engine (as part of Backbone.js) and have an issue

  • 0

I am using underscore template engine (as part of Backbone.js) and have an issue where an attribute of the JSON object has a period in it i.e.

{
"id": 1234,
"company.id": 4321
}

When I try to access this in the underscore template I get a JavaScript error:

Company ID: <@= company.id @>

I’m wondering if it’s possible and how it’s done to access attributes with period in them. I’m not in a position to change the generation of the JSON.

cowper

  • 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-04T16:16:13+00:00Added an answer on June 4, 2026 at 4:16 pm

    An easy way around that is to wrap another object around it so that you can use [] to access 'company.id'. For example, your template could look like this:

    <script id="tmpl" type="text/html">
        id: <%= o.id %><br>
        company: <%= o['company.id'] %>
    </script>​
    

    and your JavaScript like this:

    var html = _.template($('#tmpl').html(), {
        o: {
            "id": 1234,
            "company.id": 4321
        }
    });
    

    Demo: http://jsfiddle.net/ambiguous/wtLkP/1/
    ​
    The Underscore template compiler uses with to supply the context for simple things like <%= x %> in the templates so I don’t think you’re going to be able to do any better than the o. trick above. Underscore builds a function from your template, you can see the function’s source by looking at the source attribute of the function:

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

    That will give you something like this:

    function(obj){
    var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
    with(obj||{}){
    __p+='\n    id: '+
    ( o.id )+
    '<br>company: '+
    ( o['company.id'] )+
    ' and stuff\n';
    }
    return __p;
    }
    

    and you can see why just <%= [x] %> won’t work: with only adjusts the current scope, it can’t make [x] into valid JavaScript.

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

Sidebar

Related Questions

I'm using backbone's underscore templating engine with the mustache formatting patterns. I have already
I am using underscore.js's templating capabilities from backbone.js, I have the following template that
I'm trying to render an html table using underscore template engine. First I have
I'm using the underscore.js templating function and have done a template like this: <script
I am using the underscore.js template library with my backbone example. My template looks
I'm using Backbone with Underscore templates. I have a JavaScript if() condition in my
When using backbone.js and the companion templating engine in underscore, I've noticed that most
I'm using backbone.js and underscore.js templates in a JSF application with Facelets. The issue
I'm using Backbone and therefore Underscore to render my templates. My templates get rendered
I am using backbone.js and underscore.js to build an javascript application. Since hours of

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.