简体   繁体   中英

In Meteor, how can I write a helper to use in the handlebar's each loop?

Each row of my Accounts (a collection) is about {_id: xxx, last_name:'kuo', first_name'willy'}

I have following in my client.js

Template.accountPage.accounts = function() {
    return Accounts.find({});
}

My question is in my client.html:

{{#each accounts}}

  {{last_name}}
  {{first_name}}
  {{full_name}}   # <-----  how can I implement the full_name helper
                  #         which should return account.first_name + account.last_name

{{/each}}

EDIT

Above example should be simple, following is the new one:

I have following in my client.js

Template.accountPage.accounts = function() {
    return Accounts.find({});
}

My question is in my client.html:

{{#each accounts}}

  {{last_name}}
  {{first_name}}
  {{created_at}}   # <-----  how can I implement the created_at
                   #         which is computed by accounts._id.getTimestamp()

{{/each}}

Defined the following helper can achieve the goal:

Template.accountPage.helpers({
    created_at: function() {
        return this._id.getTimestamp();
    }
})

waitingkuops method works, but only on that specific case. If you want to assign arbitrary helpers to looped items, you do something like this:

{{#each accounts}}
  {{> accountItem}}
{{/each}}

<template name="accountItem">
  // custom helper
  {{customValue}}
  // standard values
  {{last_name}}
  {{first_name}}
  {{last_name}}
</template>

Template.accountItem.helpers({
  customValue: this.first_name + this.last_name + 'hey!'
});

Try

{{#each accounts}}
  {{last_name}}
  {{first_name}}
  {{first_name}} {{last_name}} 
{{/each}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM