Handlebars helpers.
These functions are to be used in Handlebars templates.
Example:
// to register all the helpers at once
Handlebars.registerHelper(require('view/hbs-helpers'));
// to register a single helper
const HbsHelpers = require('view/hbs-helpers');
Handlebars.registerHelper('fieldOfType', HbsHelpers.fieldOfType);Methods
-
<static> arrayJoin(array, glue)
-
Joins all the elements of an array by a glue string.
Parameters:
Name Type Description arrayArray.<string> Array of strings to join.
gluestring The string to join them with.
Returns:
All of the strings joined with the given
glue.- Type
- string
-
<static> buildRoute(options)
-
Builds a route based on hashes sent on Handlebars helper.
Example:
{{buildRoute context=this.context}} {{buildRoute model=myModel action="create"}} {{buildRoute module="Employees" action="edit"}}If both
moduleandmodelare sent,modulewill take precedence overmodel.module. Similarly,idwill take precedence overmodel.id.Parameters:
Name Type Description optionsObject Handlebars options hash.
Properties
Name Type Description hashObject More parameters to be used by this helper. It needs at least one of
options.hash.module,options.hash.modeloroptions.hash.context.Properties
Name Type Argument Default Description modulestring <optional>
options.hash.model.module The name of the module.
modelData/Bean <optional>
options.hash.context.get('model') The model to extract the module and/or id.
contextCore/Context <optional>
A context to extract the module from.
idstring <optional>
options.hash.model.id The id of the bean record.
actionstring <optional>
The action name.
Returns:
The built route.
- Type
- string
-
<static> component(layout, name)
-
Creates a layout or a view in a given layout.
Parameters:
Name Type Description layoutView/Layout Layout that the new layout should be created in.
namestring Name of the component to be created.
Returns:
The placeholder text for the component.
- Type
- Handlebars.SafeString
-
<static> eachOptions(key, hbsOptions)
-
Iterates through options specified by a key.
The options collection is retrieved from the language helper.
Parameters:
Name Type Description keystring Options key.
hbsOptionsfunction HBS options.
Returns:
HTML string.
- Type
- string
-
<static> eq(val1, val2, options)
-
Executes a given block if given values are equal.
Parameters:
Name Type Description val1string First value to compare.
val2string Second value to compare.
optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if the given values are equal.
inversefunction The block to execute if the given values are not equal.
Returns:
Result of the
blockexecution if the given values are equal or the result of the inverse block.- Type
- string
-
<static> field(view [, options])
-
Creates a field widget.
Example:
{{field view model=mymodel template=edit parent=fieldset}}Parameters:
Name Type Argument Description viewView/View Parent view
optionsObject <optional>
Optional params to pass to the field.
Properties
Name Type Argument Description modelBackbone.Model <optional>
The model associated with the field.
templatestring <optional>
The name of the template to be used.
parentField <optional>
The parent field of this field.
Returns:
HTML placeholder for the widget.
- Type
- Handlebars.SafeString
-
<static> fieldOfType(type, label)
-
Creates a field widget.
This helper is used for fields that don't have a view definition.
Parameters:
Name Type Description typestring Field type.
labelstring Label key.
Returns:
HTML placeholder for the widget.
- Type
- Handlebars.SafeString
-
<static> fieldValue(bean, field [, options])
-
Extracts the value of the given field from the given bean.
Parameters:
Name Type Argument Description beanData/Bean Bean instance.
fieldstring Field name.
optionsObject <optional>
Additional options.
Properties
Name Type Argument Default Description hash.defaultValuestring <optional>
'' Default value to return if field is not set on the bean.
Returns:
Field value of the given bean. If field is not set the default value or empty string.
- Type
- string
-
<static> firstChars(text, n)
-
Formats a given string by returning the first n characters.
Parameters:
Name Type Description textstring The text to trim.
nnumber The number of characters to return.
Returns:
The first
ncharacters oftext.- Type
- string
-
<static> formatCurrency(number, currencyId)
-
Formats a given currency amount according to user preferences.
Parameters:
Name Type Description numbernumber The number to format.
currencyIdstring | Object The currency identifier.
Returns:
The formatted number.
- Type
- string
-
<static> formatDate(date [, options])
-
Formats a given date according to user preferences.
Parameters:
Name Type Argument Description dateDate | string The date to format.
optionsObject <optional>
More attributes to be used on this element for reuse.
Properties
Name Type Argument Description hashObject <optional>
More parameters to be used by this helper.
Properties
Name Type Argument Description dateOnlyboolean <optional>
Flag to determine whether to return just date current user date/time preference format.
Returns:
The formatted date.
- Type
- string
-
<static> getModuleName(module [, options])
-
Gets the translated module name (plural or singular).
For instance, to get the singular version of the module name (make sure
LBL_MODULE_NAME_SINGULARis defined in the module language strings of your module):{{getModuleName 'Accounts'}}To get the plural version, set
pluralto true and make sureLBL_MODULE_NAMEis defined in the module language strings of your module:{{getModuleName 'Accounts' plural=true}}You can pass a default value that will be returned in case the module language string of your module is not found. The following example will return 'Module' (since
LBL_MODULEis defined in the module strings or the app strings):{{getModuleName 'undefinedModule' defaultValue='LBL_MODULE'}}In the worst case scenario (the module language string is not found and no default value is specified), the module key name is returned. The following example will return 'undefinedModule':
{{getModuleName 'undefinedModule'}}Parameters:
Name Type Argument Description modulestring The module defined in the language strings.
optionsObject <optional>
Optional params to pass to the helper.
Properties
Name Type Argument Description hashObject <optional>
More parameters to be used by this helper.
Properties
Name Type Argument Description pluralboolean <optional>
Returns the plural form if
true, singular otherwise.defaultValuestring <optional>
Value to be returned if the module language string is not found.
Returns:
The module name.
- Type
- string
-
<static> has(val, array, options)
-
Executes a given block if a given array has a value.
Parameters:
Name Type Description valstring | Object Value to look for.
arrayObject | Array Array or hash object to check.
optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if
valis found.inversefunction The block to execute if
valis not found.Returns:
Result of the
blockexecution ifarraycontainsvalor the result of the inverse block.- Type
- string
-
<static> isSortable(module, fieldViewdef, options)
-
We require sortable to be the default if not defined in either field viewdefs or vardefs. Otherwise, we use whatever is provided in either field vardefs or fields viewdefs where the viewdef has more specificity.
Parameters:
Name Type Description modulestring Module name.
fieldViewdefObject The field view definition (e.g. looping through meta.panels.field it will be 'this').
optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if sortable.
Returns:
Result of the
blockexecution if sortable, otherwise empty string.- Type
- string
-
<static> log(value)
-
Logs a value.
Parameters:
Name Type Description value* The value to log.
-
<static> match(val1, val2, options)
-
Same as the
eqhelper, but the second value is a (string) regex expression. This works around Handlebars' lack of support for regex literals.Note that modifiers are not supported.
Parameters:
Name Type Description val1string The string to test.
val2string The expression to match against. It will be passed directly to the
RegExpconstructor.optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if
val1matchesval2.inversefunction The block to execute if
val1does not matchval2.Returns:
Result of the
blockexecution ifval1matchesval2or the result of the inverse block.- Type
- string
-
<static> nl2br(s)
-
Converts
\r\n,\n\r,\r, and\nto<br>.Parameters:
Name Type Description sstring The raw string to filter.
Returns:
The given
swith all newline characters converted to<br>tags.- Type
- Handlebars.SafeString
-
<static> notEq(val1, val2, options)
-
Opposite of
eqhelper.Parameters:
Name Type Description val1string first value to compare
val2string second value to compare.
optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if the given values are not equal.
inversefunction The block to execute if the given values are equal.
Returns:
Result of the
blockexecution if the given values are not equal or the result of the inverse block.- Type
- string
-
<static> notHas(val, array, options)
-
Executes a given block if a given array doesn't have a value.
Parameters:
Name Type Description valstring | Object Value to look for.
arrayObject | Array Array or hash object to check.
optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if
valis not found.inversefunction The block to execute if
valis found.Returns:
Result of the
blockexecution if thearraydoesn't containvalor the result of the inverse block.- Type
- string
-
<static> notMatch(val1, val2, options)
-
Same as the
notEqhelper but second value is a (string) regex expression. This works around Handlebars' lack of support for regex literals.Note that modifiers are not supported.
Parameters:
Name Type Description val1string The string to test.
val2string The expression to match against. It will be passed directly to the
RegExpconstructor.optionsObject Additional options.
Properties
Name Type Description fnfunction The block to execute if
val1does not matchval2.inversefunction The block to execute if
valmatchesval2.Returns:
Result of the
blockexecution if the given values are not equal or the result of the inverse block.- Type
- string
-
<static> partial(name, component [, properties] [, options])
-
Helper for rendering a partial template. This helper can load a partial from the
templateOptionsor from the same relative location as the current template.{{partial 'partial-name' componentFrom defaultProperties dynamicProperty=value}}The data supplied to the partial with be an object with the list of
dynamicPropertys merged intodefaultPropertiesobject (defaults to empty object if not explicitly passed).For fields:
{{partial 'edit' this properties fallbackTemplate='detail'}}For layouts:
{{partial 'ActivityStream' this properties}}For views:
{{partial 'record' this properties}}Parameters:
Name Type Argument Description namestring Name of the partial.
componentView/Component The view component.
propertiesObject <optional>
Data supplied to the partial.
options.hashis merged into this before it is used for the template. This allows the partial to provide dynamic parameters on top of the default properties. The original component is kept astemplateComponentin these properties.optionsObject <optional>
Optional parameters.
Properties
Name Type Argument Description hashObject <optional>
The hash of the optional params.
Properties
Name Type Argument Default Description moduleObject <optional>
component.module Module to use.
fallbackTemplateObject <optional>
Fallback template for field partials.
Returns:
The HTML of the partial template.
- Type
- Handlebars.SafeString
-
<static> relativeTime(iso8601, options)
-
Creates a relative time element to display the human readable related time.
To provide an automatic update of this relative time, use a plugin like sugar.liverelativetime.js.
Parameters:
Name Type Description iso8601string The ISO-8601 date string to be used for a new date.
optionsObject Handlebars options hash.
Properties
Name Type Description hashObject More parameters to be used by this helper.
Properties
Name Type Argument Description titlestring <optional>
The title attribute. Defaults to current user date/time preference format.
dateOnlyboolean <optional>
Setting this to
truewill format thetitleattribute with the user-formatted date only.Returns:
The relative time like
10 minutes ago.- Type
- string
-
<static> str(key [, module] [, content])
-
Retrieves an i18n-ed string by key.
Parameters:
Name Type Argument Description keystring Key of the label.
modulestring <optional>
Module name.
contentstring <optional>
Template content.
Returns:
The string for the given label key.
- Type
- string