How to link Template Helpers?

Meteor Newbie here!

I have a page where all the open orders are displayed. The order details are stored in a collection. A Template helper will return the order details.

Template.delivery.helpers({
  openOrders: function(){
   return Orders.find({status: "open"});
  }
});

The template look some what like this.

{{#if openOrders}}
  {{#each openOrders}}
	 Date: {{createdAt}}
     Total items: {{items.length}}
	 Location: {{location}} //It prints the location _id
  {{/each}}
{{/if}}

The Order Collection only have the _id of the location. The Location details are stored in a Collection named Locations.

I want to display the location name (which is stored in the Location collection) instead of the _id.

I created a Template helper which returns the Location details, but how can I link these to helpers so that the Location name is displayed instead of Location _id?

Make use of: https://github.com/dburles/meteor-collection-helpers

Mentioned in the Guide: http://guide.meteor.com/collections.html#collection-helpers

1 Like

I use “with” a lot. Maybe Google how it works exactly.

Location: {with place}{{locationName} {/place}

Template.delivery.helpers({
place: function () {
return Locations.findOne({_id: this.location});
}
});

Basically use a function, I called it place, to find the Locations name using an id. Then use ‘with’ to call that function.

When I think about it, the ‘with’ isnt necessary. but its how I did a similar problem.

You don’t need to use if outside of each. If each is empty, it won’t render anyways.

I have an else statement also, which is not included in the question.

You can use this syntax (which is a little weird, but official):

{{#each openOrders}}
  Date: {{createdAt}}
  Total items: {{items.length}}
  Location: {{location}} //It prints the location _id
{{#else}}
  Nothing to see here!
{{/each}}

(Check out the Meteor Guide Todos example)

1 Like