Handlebars API

This topic lists the functions of Handlebars that are supported in Designer scripts. It also lists features that are unsupported in OL Connect.

The information in this Online Help focuses on the implementation of Handlebars in OL Connect. For general information about Handlebars and how to use it, see the following web sites: https://handlebarsjs.com/ and https://devdocs.io/handlebars.

Functions

compile('template')

Compiles the specified Handlebars template* into a function that can be called with data. For example:

const template = Handlebars.compile('snippets/Template1.hbs'); 

const result = template(record);

Note: Passing options is not supported in OL Connect.

render('template', data)

Renders a specified Handlebars template.*

If data is passed, that data is used to render the template.
If no data is passed, the current record is used to render the template.

Handlebars.render( "snippets/Template.hbs" )
is identical to:
const compiledTemplate = Handlebars.compile( "snippets/Template.hbs" )
compiledTemplate( record )

The result of this function is HTML.

See also: Handlebars templates.

registerPartial('name', 'template')

Registers a template* as a partial with the specified name.
For example, this line of code registers a template (clauses.hbs) as a partial with the name 'clauses':
Handlebars.registerPartial('clauses', 'snippets/partials/clauses.hbs');

The partial can then be referred to by its name in Handlebars templates as well as functions using {{> partialName}}. For example: {{> clauses}}.

See: Partials.

registerHelper()

Registers a function as helper with the specified name.

Note: Registering multiple helpers with a single call is not supported.

See: Handlebars expressions.

escapeHTML()

HTML-escapes the supplied string. This means that those characters that have a special function in HTML: & < > \" ' ` = are replaced with HTML character references. For example: & will be changed into &amp;.

See: HTML values.

* In all these functions, 'template' can be:

  • the name of an .hbs snippet in the template (for example:{{> snippets/Snippet 1.hbs}}
  • the name of an .hbs snippet on disk (starting with file:///)
  • the name of a remote .hbs snippet (starting with http:// or https://)
  • a string that contains HTML and Handlebars expressions.

With a snippet on disk, the complete syntax is: file://<host>/<path>. If the host is "localhost", it can be omitted, resulting in file:///<path> - note the three forward slashes after file:.
In the remainder of the path you can either use escaped backward slashes:
"file:///C:\\Users\\Administrator\\Desktop\\Handlebars_LoadFile.hbs"
or forward slashes:
"file:///C:/Users/Administrator/Desktop/Handlebars_LoadFile.hbs"

It is not possible to use loadhtml() in Handlebars functions. HTML with Handlebars expressions is not necessarily valid HTML. Processing it with an HTML parser might break both the Handlebars expressions and the HTML.

Unsupported features

The following features are not supported in Handlebars templates in OL Connect: 

  • The SafeString class. It is not needed since the result of a block helper is not HTML-escaped in OL Connect.

  • Inline escapes.

  • Raw block helpers.

  • Passing options when compiling a Handlebars template.

The following Handlebars partials features are not supported in OL Connect.

The following Handlebars helpers features are not supported in OL Connect.

  • The lookup helper and log block helper.

  • The data variable @key. OL Connect only supports iterating over arrays and tables, not over arbitrary objects.

  • Registering multiple helpers with a single call.

  • The blockHelperMissing and helperMissing hooks.

  • Passing options.hash, options.helpers and options.partials arguments to helpers. (options.data, options.fn and options.inverse are supported.)