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 subfolder of the Snippets folder on the Resources pane.
Using partials
To include a partial in either a section or a Handlebars template (i.e. another partial), write an expression that refers to the partial by name:
{{+partialname}}
".hbs" can be omitted from the name of the partial.
Note: If the name of the partial contains a space, the space needs to be escaped. Either surround the name with quotes or replace the space with %20. For example, a partial called “par tial.hbs” can be referenced as follows: {{+par%20tial}} or {{+('par tial')}}.
Partials don't have to be located in the template. They can also be loaded from a disk or from an endpoint (which could facilitate a search in a database or CMS). For instance, you can load a partial from a URL like this:
{{+http://localhost:1880/policies?p=travel}}
You could pass data to the partial using one or more hash arguments.
Example: {{+myPartial test=123}}
See Hash arguments.
Expressions inside a partial are resolved using the record, unless a custom context is passed to the partial. For example, if a field in your data has an address
object, that object could be passed as follows:
Example: {{+myPartial address}}
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.
-
Use the expression: {{> partialname}}. In sections this does not work, because the greather-than-sign will then be interpreted as HTML.
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
There are several ways to load partials dynamically, based on data.
-
You could use a Block Helper (see Using functions in expressions: Helpers and Using Handlebars templates).
Tip: To keep the template clean and clutter-free it's best to put the Block Helper itself in a partial.
-
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 (with or without the extension: ".hbs").
Example:
{{+ (whichPartial) }}
If
whichPartial
is a data field that returns the name of a partial, the template renders the partial whose name is found in the data field.whichPartial
could also be the name of a function. Note that to use a function in this way, it must be registered as a Helper. See Creating custom Helpers. -
Finally, instead of loading a different partial you could load the same partial and pass different data to it using one or more hash arguments. See Hash arguments.
Inline partials
Inline partials are supported as well. See the two examples at https://docs.w3cub.com/handlebars/partials#inline-partials.