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.
Information about Handlebars templates in OL Connect can be found in the topics Handlebars templates.
Creating a partial
Any Handlebars template, including remote Handlebars templates, can be used as a partial in a Handlebars template in OL Connect.
To create a partial in an OL Connect template, simply create a Handlebars template; see Creating a Handlebars template.
How to register a partial
It is not mandatory to register a partial, but it can be useful, for example if you want to work with dynamic partials. Registered partials can be referred to in Handlebars expressions and functions by the specified name; see Using a registered partial in a template.
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 (clauses.hbs) as a partial with the name 'clauses': Handlebars.registerPartial('clauses', 'snippets/partials/clauses.hbs');
Using partials in Handlebars templates
To include a partial in a Handlebars template, insert an expression that refers to it, for example:
{{> partialname.hbs}}.
Relative paths (starting with snippets/) and arbitrary URLs (starting with file:// or http:// or https://) are supported.
The file name 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://)
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"
Using a registered partial in a template
Registered partials (see How to register a partial) can be referred to by name instead of file name, using the following syntax: {{> partialName}}.
For example, a partial that is registered as follows:Handlebars.registerPartial('clauses', 'snippets/partials/clauses.hbs');
can be included in a Handlebars template with the following expression: {{> clauses}}.
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.
For example: {{#> myPartial }} Failover content {{/myPartial}} will render "Failover content" if the "myPartial" partial is not registered.
Dynamic partials
To load partials dynamically, based on data, you could use a block helper (see Using logic in a Handlebars template: helpers and Using Handlebars templates: 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 Adding custom helpers.
Inline partials
Inline partials are supported as well. See the two examples at https://docs.w3cub.com/handlebars/partials#inline-partials.
Unsupported features
The following Handlebars partials features are not supported in OL Connect.
-
Passing a custom context as a parameter.
-
Passing hash parameters (seehttps://devdocs.io/handlebars/partials#partial-parameters).