Partials

Partials are normal Handlebars templates that may be called directly by other templates. This topic explains how to work with Handlebars partials in OL Connect.

Note: 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.

Creating a partial

Since partials are normal Handlebars templates, you can create them just like any other Handlebars template; see Creating a Handlebars template.

Tip: To keep an overview you could group the partials in a sub folder of the Snippets folder on the Resources pane.

Using partials

To include a partial in either a Handlebars template or a section, you can:

  • Write an expression that refers to the partial by name. You can use either {{+ partialname}} or {{> partialname}}.

Note: If partials are used in a section, editing in Preview mode is not supported. Any changes made in Preview mode are reverted when you switch back to Design mode.

To include a partial in a Handlebars template (not a section), you can also:

  • Drag the partial from the Snippets folder on the Resources pane to the desired location in the Handlebars template. This copies the contents of the partial into the other.

When a partial is missing

If there is no partial with the specified name, Handlebars can generate some other content, called failover content. Failover content can be specified using block syntax. The expression that calls the partial is the start of a block. An expression with a closing tag and the name of the partial signals the end of the block. The content between these expressions will be used if the called partial is missing.

Example: {{#+ myPartial }} Failover content {{/myPartial}}

The expression in this example will render "Failover content" if no "myPartial" partial is found.

Dynamic partials

To load partials dynamically, based on data, you could use a Block Helper (see Using functions in expressions: Helpers and Adding Handlebars templates through script: examples).

It is also possible to dynamically select a partial by using a sub expression between parentheses. The sub expression should evaluate to the name of a partial.
For example, if whichPartial is a function that returns the name of a partial, {{> (whichPartial) }} in a Handlebars template calls whichPartial and then renders the partial whose name is returned by this function.
Note that to use a function in this way, it must be registered as a Helper. See Creating custom Helpers.

Inline partials

Inline partials are supported as well. See the two examples at https://docs.w3cub.com/handlebars/partials#inline-partials.

Registered partials

When a partial is registered, it can then be referred to by the name with which it was registered. In OL Connect, registering partials is not necessary, but it is possible.

To register a partial, use the following function in a script:
Handlebars.registerPartial('partialName', 'template')
(For details see: Handlebars API).

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

The "aPartial" partial can now be included in a Handlebars template or section with the following expression: {{+ mySmartPartialName}}

Tip: Partials registered in a Control Script will be available in all standard scripts. Control Scripts are executed first. (See Control Scripts.)

Note: Registering multiple partials with a single call is not supported in OL Connect.

Unsupported features

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