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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:14:37+00:00 2026-06-04T10:14:37+00:00

Say I have a User model in JavaScript that looks something like this: var

  • 0

Say I have a User model in JavaScript that looks something like this:

var User = function(attributes) {
  this.attributes = attributes;
}

User.fields = [
  {name: 'firstName'},
  {name: 'lastName'},
  {name: 'email'}
]

User.prototype.get = function(key) {
  return this.attributes[key];
}

User.all = [new User({firstName: 'Foo'})];

And I want to run it through a Handlebars template that goes through each field on the User class, creates a header for it, and then for each user renders the values:

<table>
  <thead>
    <tr>
      {{#each User.fields}}
      <th>{{name}}</th>
      {{/each}}
    </tr>
  </thead>
  <tbody>
    {{#each User.all}}
    <tr>
      {{#each User.fields}}
      <td>{{content.get(name)}}</td>
      {{/each}}
    </tr>
    {{/each}}
  </tbody>
</table>

My question is, how do I accomplish that internal part:

{{#each User.fields}}
<td>{{content.get(name)}}</td>
{{/each}}

That’s basically doing user.get(field.name). How can I do that in Handlebars, given I don’t know the fields before hand and want this to be dynamic?

Thanks for your help.

  • 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-04T10:14:39+00:00Added an answer on June 4, 2026 at 10:14 am
     <body>
       <div id='displayArea'></div>
       <script id="template" type="text/x-handlebars-template">
        <table border="2">
            <thead>
            <tr>
                {{#each Fields}}
                 <th>{{name}}</th>
                {{/each}}
            </tr>
            </thead>
            <tbody>
              {{#each users}}
              <tr>
                {{#each ../Fields}}
               <td>{{getName name ../this}}</td>
                {{/each}}
              </tr>
             {{/each}}
            </tbody>
         </table>
     </script>
    
    <script type="text/javascript">
        var User = function(attributes) {
            this.attributes = attributes;
        }
    
        User.fields = [
            {name: 'firstName'},
            {name: 'lastName'},
            {name: 'email'}
        ]
    
        User.prototype.get = function(key) {
           return this.attributes[key];
        }
    
        User.all = [new User({firstName: 'Foo',lastName :'ooF',email : 'foo@gmail.com'}) , new User({firstName: 'Foo2'})];       //array of user
    
        //handle bar functions to display
        $(function(){
           var template = Handlebars.compile($('#template').html());
    
            Handlebars.registerHelper('getName',function(name,context){
                              return context.get(name);
              });
            $('#displayArea').html(template({Fields :User.fields,users:User.all}));
        });
       </script>
      </body>  
    

    This will solve ur problem using helpers in handlebar JS

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

Sidebar

Related Questions

Say I have a model that looks like: class Action(models.Model): user = models.ForeignKey('auth.User') action
I have a user model that has many fields (let's say 30 for this
Let's say I have model like this (all irrelevant fields removed for clarity): A(Model):
For instance, let's say I have a User model. Users have things like logins,
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,
Lets say I have a working form that looks like the following =form_for @survey
Let's say I have: class Like(db.Model): user = db.ReferenceProperty(User,collection_name='likes') photo = db.ReferenceProperty(Photo,collection_name='likes_received') created =
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Let's say I have a user that entered 12 links into the database but
Let's say I have an user registration form. In this form, I have the

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.