Skip to content

Instantly share code, notes, and snippets.

@cdlewis
Last active May 8, 2024 18:07
Show Gist options
  • Save cdlewis/ab75ad2a1e821a5a985a to your computer and use it in GitHub Desktop.
Save cdlewis/ab75ad2a1e821a5a985a to your computer and use it in GitHub Desktop.
Handlebars for Iterable

Chris' Iterable Handlebars Cheat Sheet

Merge Parameters

Iterable merge paremeters have different brackets depending on whether it's a user object or a feed. These differences extend to functions operating on those parameters. For example, {{if}} needs to be [[if]] when evaluating a feed parameter.

User object

{{firstName}}

Feed object

[[firstName]]

Conditionals

Basic if/else expressions.

{{#if firstName}}
	Hello {{firstName}}
{{else}}
	Hello Friend
{{/if}}

'Unless' expressions

{{#unless firstName}}
You don't have a first name!
{{/unless}}

Equality

{{#ifEq age 21}}
	Age is 21
{{/ifEq}}

Greater Than

{{#ifGt age 21}}
	Age is over 21
{{/ifGt}}

Greater Than Or Equal To

{{#ifGte age 21}}
	Age is greater than or equal to 21
{{/ifGte}}

Less Than

{{#ifLt age 21}}
	Age is less than 21
{{/ifLt}}

Less Than Or Equal To

{{#ifLte age 21}}
	Age is less than or equal to 21
{{/ifLte}}

Modulus (The Remainder of a Division Operation)

{{#ifModEq age 21 0}}
	Age % 21 == 0 (age is a multiple of 21)
{{/ifModEq}}

Loops

A loop will go through each item in a list. Your current position in the loop can be accessed with @index. Other Handlebars functions (such as conditionals) can be nested inside the loop.

{{#each story}}
	{{title}}
{{/each}}

List Operations

Minimum Item in List

{{#minInList shoppingCartItems "price"}}
	Those least expensive item in your cart is: {{price}}
{{/minInList}}

Maximum Item in List

{{#maxInList shoppingCartItems "price"}}
	Those most expensive item in your cart is: {{price}}
{{/maxInList}}

String Functions

Encode a field/value so it can be validally included in a URL

{{#urlEncode}}
	{{myParam}}
{{/urlEncode}}

Capitalise the First Letter of a Field

{{capitalizeFirst myUrl}}

Format a JSON Date String

{{ dateFormat date "yyyy-MM-dd HH:mm:ss" tz="America/Los_Angeles" }}

Replace Newlines (\r\n) with HTML Line Breaks (<br />)

{{#breaklines}}some text or a {{handlebars_variable}}{{/breaklines}

Automatically Provided Merge Parameters

  • View in Browser URL: {{viewInBrowserUrl}}
  • Unsubscribe URL: {{unsubscribeUrl}}
  • Hosted Unsubscribe URL: {{hostedUnsubscribeUrl}}
  • Unsubscribe Message Type URL: {{unsubscribeMessageTypeUrl}}
  • Campaign Id: {{campaignId}}
  • Recipient Email: {{email}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment