Last Updated: February 25, 2016
·
3.202K
· hamxiaoz

Pass data to template in Meteor.js

EDIT: This method works in 0.8.1. If anything gets changed in 1.0, I'll update this.

With the new blaze engine, I found the arguments of a template is confusing. Here is what I found that works for me:

# main page
template(name="downloads")
  h4 Software
  .row
    .col-md-12
      table.table
        each exes
          +downloadResourceRow resource=this class="success"

# helper
Template.downloads.helpers
  exes: ->
    Resources.find()

# partial
template(name="downloadResourceRow")
  tr(class="{{class}}")
    td.col-md-4
      b #{resource.name}
    td.col-md-8
      a.btn.btn-default(href="{{resource.file_link}}") Download


# data
@Resources = new Meteor.Collection2 "resources",
  schema:
    name:
      type: String
    file_link:
      type: String

Basically I always pass data using keyword arguments and they will become the new data context.
In the example above, the data context the template downloadResourceRow gets is this:

{
  resouce: 'the resource object'
  class: "success"
}

Reference: https://github.com/meteor/meteor/blob/devel/packages/spacebars/README.md#inclusion-and-block-arguments