Handlebars Templating Language

Overview

The Handlebars Templating Language is available for use with Andi PrintPowers and EmailPowers. Both follow the same pattern of feeding data to the power along with the template to produce an HTML output. In the case of PrintPowers a fileId is used as a reference to the generated HTML content for custom print outs and the with EmailPowers the resulting HTML is sent as an email.

 

In this Article

 

Escaping Handlebars Expressions

Block Escaping

Template

\{{escaped}}
{{{{raw}}}}
{{escaped}}

Inline Escaping

Template

{{{{/raw}}}}

 

Language Features

Simple Expressions

Template

<p>{{firstname}} {{lastname}}</p>

Model

{
firstname: "Yehuda",
lastname: "Katz",
}

Output

<p>Yehuda Katz</p>

 

Nested Input Objects

Template

{{person.firstname}} {{person.lastname}}

Model

{
person: {
  firstname: "Yehuda",
  lastname: "Katz",
}
}
Output
Yehuda Katz
 

Evaluation Context

Template

{{#with person}}
{{firstname}} {{lastname}}
{{/with}}

Model

{
  person: {
    firstname: "Yehuda",
    lastname: "Katz",
  },
}

Output

Yehuda Katz
 

Changing the Context

Template

{{#each people}}
    {{../prefix}} {{firstname}}
{{/each}}

Model

{
  people: [
    { firstname: "Nils" },
    { firstname: "Yehuda" },
  ],
  prefix: "Hello",
}

Output

Hello Nils
Hello Yehuda
 
 

Template Comments

Template
 

 
Model
//empty model
{}
 
Output
 
 
 

HTML Escaping

Template

raw: {{{specialChars}}}
html-escaped: {{specialChars}}

Model 

{ specialChars: "& < \" ' ` =" }

Output

raw: & < " ' ` =
html-escaped: & < " ' ` =

 

Whitespace Control

Template

{{#each nav ~}}
  
    {{~#if test}}
      {{~title}}
    {{~^~}}
      Empty
    {{~/if~}}
  
{{~/each}}

Model

{
  nav: [{ url: "foo", test: true, title: "bar" }, { url: "bar" }];
}

Output

 

Versus no Whitespace Control with same model.

Template

{{#each nav}}
  
    {{#if test}}
      {{title}}
    {{^}}
      Empty
    {{/if}}
  
{{~/each}}

Output

 

Built-In Helpers

#if

Template

Model

{
  author: true,
  firstName: "Yehuda",
  lastName: "Katz",
}

Output

 

#unless

Template

Model

//empty model
{}

Output

 

#each

Template

Model

{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley",
  ],
}

Output

 

#with

Template

{{#with person}}
  {{firstname}} {{lastname}}
{{/with}}

Model

{
  person: {
    firstname: "Yehuda",
    lastname: "Katz",
  },
}

Output

Yehuda Katz

 

#lookup

Template

{{#each people}}
   {{.}} lives in {{lookup ../cities @index}}
{{/each}}

Model

{
  people: ["Nils", "Yehuda"],
  cities: [
    "Darmstadt",
    "San Francisco",
  ],
}

Output

Nils lives in Darmstadt
Yehuda lives in San Francisco

 

Extensibility

Partials

The current incarnation of the power does not currently allow skill writers to extend the Handlebars templating language, but it does allow the use of Inline Partials. Partials help with reuse and can make templates less verbose but except for Inline Partials need to be registered with the template engine. As skill writers start leverage this power this would be the most likely area that we allow them to extend.
 

Inline Partial

Template
{{#*inline "myPartial"}}
  My Content
{{/inline}}
{{#each people}}
  {{myPartial}}
{{/each}}

Model

{
  people: [
    { firstname: "Nils" },
    { firstname: "Yehuda" },
  ],
}

Output

My Content
My Content

 

Back to Top

Was this article helpful?
0 out of 0 found this helpful