Handlebars Templates

This library was designed to be flexible and allow total customization of the markup used by all Components.

Overriding Templates

There are three ways to replace the Handlebars template used by a component:

  • Pass a template HTML string containing a Handlebars template as described in Installation.

  • Pass the ID of a template element containing a Handlebars template as described in Installation.

  • Define the new Handlebars template as the child content of a control

Passing an HTML string

In your HTML file, define the template content directly in the configuration options:

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Hawksearch Handlebars UI</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <script type="text/javascript"> var Hawksearch = Hawksearch || {}; Hawksearch.config = { clientId: "f51060e1c38446f0bacdf283390c37e8", templates: { pageSize: ` <div class='page-size'> <select hawksearch-page-size> {{#each options}} <option value='{{pageSize}}' {{attribute 'selected' selected}}>{{title}}</option> {{/each}} </select> </div>` } }; </script> <script src="node_modules/@bridgeline-digital/hawksearch-handlebars-ui/dist/handlebars-ui.js" defer ></script> </head> <body> <h1>Hawksearch Handlebars UI</h1> <hawksearch-search></hawksearch-search> <hawksearch-results></hawksearch-results> </body> </html>

Creating a template element

In your HTML file, add the custom markup to a template element and pass the ID in the configuration options:

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Hawksearch Handlebars UI</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <script type="text/javascript"> var Hawksearch = Hawksearch || {}; Hawksearch.config = { clientId: "f51060e1c38446f0bacdf283390c37e8", templates: { pageSize:'page-size-template' } }; </script> <script src="node_modules/@bridgeline-digital/hawksearch-handlebars-ui/dist/handlebars-ui.js" defer ></script> </head> <body> <h1>Hawksearch Handlebars UI</h1> <hawksearch-search></hawksearch-search> <hawksearch-results></hawksearch-results> <template id="page-size-template"> <div class='page-size'> <select hawksearch-page-size> {{#each options}} <option value='{{pageSize}}' {{attribute 'selected' selected}}>{{title}}</option> {{/each}} </select> </div> </template> </body> </html>

This approach tends to be easier to manage than the previous option as it is more compatible with popular HTML editors and templates can potentially be loaded from separate files using Server-side Includes (SSI).

Setting control content

<hawksearch-page-size> <div class='page-size'> <select hawksearch-page-size> {{#each options}} <option value='{{pageSize}}' {{attribute 'selected' selected}}>{{title}}</option> {{/each}} </select> </div> </hawksearch-page-size>

This approach is simplest only if you place all of the components manually and avoid using the Search Results Component as that wraps around most of the other components.

If you choose this approach to customization and need to define a template for a nested element, be aware that by default, Handlebars will try to apply the model from the top-level component to all nested components, which will not work. To guard against this behavior, use the exclude helper documented in Handlebars Helpers.

Handlebars Helpers

Default Templates

The following helper functions are available in this library by default:

add

This function returns the sum of two values:

and

This function returns whether or not all passed arguments resolve to true:

attribute

This function renders an attribute or attribute value if the given condition resolves to true:

concat

This function combines multiple values into a single string:

currency

This function formats a value as currency:

eq

This function returns whether two values are equivalent:

exclude

This function is used to prevent Handlebars from attempting to bind a data model to a nested control containing a custom template:

To better illustrate, see the following example:

html

This function is used to render a provided string as HTML rather than escape special characters:

if-else

This function is used to render different content inline depending on whether a condition resolves to true or false:

lt

This function returns whether the first provided value is less than the second provided value:

lte

This function returns whether the first provided value is less than or equal to the second provided value:

gt

This function returns whether the first provided value is greater than the second provided value:

gte

This function returns whether the first provided value is greater than or equal to the second provided value:

number

This function formats a number:

or

This function returns whether either provided value resolves to true:

string

This function displays a specified string with an optional default value:

subtract

This function returns the difference of two values:

Custom Templates

The Handlebars instance is exposed via Hawksearch.handlebars. You can use this reference to define your own helper functions as needed.