Spacebars is a Meteor template language inspired by Handlebars. It shares some of the spirit and syntax of Handlebars, but it has been tailored to produce reactive Meteor templates when compiled.
each
Iterates over an array or query cursor
Example 1
An array of objects
Js
Template
Or each .. in
Result
Example 2
An array of strings
Js
Template
Or each .. in
Result
Example 3
A query cursor
Spacebars is smart enough to know how to iterate over a query cursor (and keep everything updated as things change).
Assuming that our Items collection currently has documents that have name and pet fields (just like example 1).
Js
Template
Or each .. in
Result
with
Let’s highlight why we use this helper.
Js
Template Without with
Template With with
with can also take an optional else statement which is used if dog is falsey
Js
Template
While easy to overlook; as you can see above, with is very much like an if statement. If dog is falsey, the content contained within will not render.
if, else
Js
Template
Result
Spot is awake!
unless
unless is the opposite of if
Js
Template
Result
Template inclusion
Example 1
We can easily pull in another template to display.
The most common usage is within an each block.
Js
Templates
Result
Notice that our listItem template is automatically passed the current data context from our each block.
Example 2
Passing a data context
Js
Templates
Result
Example 3
Passing named arguments
Js
Template
Result
Helpers that take arguments
Js
Template
Custom block helpers
Template
Template
Example with an else block
Comments
Template
Template.dynamic
Js
Template
This operator allows you to render templates along with their data context dynamically.