Step: Mustache

The mustache step fills a mustache template with data. It is must useful for generating text or HTML content for notifications and reports.

The mustache step takes a piece of data, renders it using a mustache template, and provides the resulting text as a result.

The mustache.zip (11.7 KB) examples file contains three scenarios. All of them use the following small parcel tracking dataset that is rendered in a mustache template.

{
  :id 198321,
  :from "DE, Hamburg",
  :to "GB, London",
  :events [
    {
      :time 2020-11-25T15:55:23Z,
      :event "pickup",
      :location "DE, Hamburg"
    },
    {
      :time 2020-11-25T16:21:32Z,
      :event "arrived sorting facility",
      :location "DE, Hamburg"
    },
    {
      :time 2020-11-25T18:02:29Z,
      :event "left sorting facility",
      :location "DE, Hamburg"
    },
    {
      :time 2020-11-26T05:31:52Z,
      :event "arrived sorting facility",
      :location "GB, London"
    },
    {
      :time 2020-11-26T07:01:02Z,
      :event "left sorting facility",
      :location "GB, London"
    },
    {
      :time 2020-11-26T09:22:16Z,
      :event "delivered",
      :location "GB, London"
    }
  ]
}

Using external template files

The file_templates folder contains the most common use case: We use external template files to render a complex output.

The step is configured with the template directory to use, and starts with a main template which in turn includes partials to construct the entire output.

When run, it generates a self-contained HTML output that is suitable as an email message.

Using inline templates

If the templating task is relatively small, you might want to use a dict containing template names and their template strings instead of files. The inline_templates folder has an example how to do this:

The resulting text is suitable as a quick notification:

Here's the tracking report on your parcel id 198321 from DE, Hamburg to GB, London:

2020/11/25 15:55 - Parcel pickup at DE, Hamburg
2020/11/25 16:21 - Parcel arrived sorting facility at DE, Hamburg
2020/11/25 18:02 - Parcel left sorting facility at DE, Hamburg
2020/11/26 05:31 - Parcel arrived sorting facility at GB, London
2020/11/26 07:01 - Parcel left sorting facility at GB, London
2020/11/26 09:22 - Parcel delivered at GB, London

Using lambdas

Mustache supports the notion of lambdas that you can call to help generate a desired layout.

The sample in the lambdas folder demonstrates how you can use a function as a mustache lambda.

This example uses the decorate lists feature of the step, which wraps all list items to include the item index, as well as boolean first and last indicators. The original item is available as value.

The event_section lambda prefixes its content and wraps it in a {{#value}}...{{/value}} scope. The contents of an event_section are therefore prefixed and scoped to the item value.

The resulting text is:

Here's the tracking report on your parcel id 198321 from DE, Hamburg to GB, London:

Event #0: 2020/11/25 15:55 - Parcel pickup at DE, Hamburg
Event #1: 2020/11/25 16:21 - Parcel arrived sorting facility at DE, Hamburg
Event #2: 2020/11/25 18:02 - Parcel left sorting facility at DE, Hamburg
Event #3: 2020/11/26 05:31 - Parcel arrived sorting facility at GB, London
Event #4: 2020/11/26 07:01 - Parcel left sorting facility at GB, London
Event #5: 2020/11/26 09:22 - Parcel delivered at GB, London

Keep in mind that mustache is logic-less by design, and using lambdas as data formatting utilities usually does not work, because mustache lambdas receive a template string that has not been substituted yet.

Mustache expects that you perform any data formatting on the data before passing it to the template.