javascriptmeteorspacebarsmeteor-helpermeteor-collections

Is it possible to define multiple template helpers en masse instead of one-by-one in Meteor?


Collection with name Services has multiple documents:

{
    serviceTitle: "Unlimited HDR Photography",
    serviceDescription: "Includes unlimited daytime HDR photos and a single trip out to the property. A zip file with the bare JPG files will be emailed to you. No website or virtual tour / panoramas.",
    serviceHTML: "",
    sku: "hdrPhotos",
    price: 100,
    _id: "x5Hq7ybdS3YHmrgsa"
}

{
    serviceTitle: "Twilight Photography",
    serviceDescription: "Photos will be shot 15 minutes prior to sunset for dramatic lighting effects.",
    serviceHTML: "",
    sku: "twilightPhotos",
    price: 100,
    _id: "RLhjHBGicGQKgS4SQ"
}

{
    serviceTitle: "Video Clips",
    serviceDescription: "We will come in with professional video equipment and shoot numerous video clips of the property. The video files will then be uploaded to your Single Property Website provider.",
    serviceHTML: "",
    sku: "videoClips",
    price: 175,
    _id: "uBZSeNWa4zZNMa8z9"
}

Say that I want helpers that can display the serviceDescription and price of each different service.

Template.myTemplate.helpers({

  hdrPhotosServiceDescription: function(){ 
    return Services.findOne({"sku": "hdrPhotos"}, { serviceDescription:1, _id:0}).serviceDescription
  },
  hdrPhotosPrice: function(){ 
    return Services.findOne({"sku": "hdrPhotos"}, { price:1, _id:0}).price
  },

  twilightPhotosServiceDescription: function(){ 
    return Services.findOne({"sku": "twilightPhotos"}, { serviceDescription:1, _id:0}).serviceDescription
  },
  twilightPhotosPrice: function(){ 
    return Services.findOne({"sku": "twilightPhotos"}, { price:1, _id:0}).price
  },

  videoClipsServiceDescription: function(){ 
    return Services.findOne({"sku": "videoClips"}, { serviceDescription:1, _id:0}).serviceDescription
  },
  videoClipsPrice: function(){ 
    return Services.findOne({"sku": "videoClips"}, { price:1, _id:0}).price
  }

});

This example only shows three different services and the desire to have helpers for only two properties of each service.

{{hdrPhotosServiceDescription}}, {{hdrPhotosPrice}}, {{twilightPhotosServiceDescription}}, {{twilightPhotosPrice}}, {{videoClipsServiceDescription}}, {{videoClipsPrice}}

What if each service had 10 different properties and I wanted helpers for each property and I had 30 different services? Doing it this way would require me to code and define 300 different helpers.

Is there a simpler way to do this? Additionally, with the way my website is structured I cannot use #each as the Meteor handbook uses for its Posts collection.


Solution

  • Template helpers can have parameters. The simplest way would be:

    Template.myTemplate.helpers({
      getField: function(sku, fieldName) {
        var projection = {_id: 0};
        projection[fieldName] = 1;
        return Services.findOne({"sku": sku}, projection)[fieldName];
      }
    }
    

    You could call that helper like {{getField "hdrPhotos" "serviceDescription"}}.