Custom Handlebar Helpers - blacklight-cms/blacklight-core GitHub Wiki
Below is a list of the custom Handlebar helpers available in Blacklight. With the exception of these custom helpers, the use of Handlebars templates within Blacklight predominantly follows usage as described on the main Handlebars web site.
Finds the template associated with the resource type in context._meta._sling_resourceType or resourceType, if provided, evaluates the template with the provided context, and inserts the resulting HTML into the invoking template.
context (Object): The data context to be used when evaluating the template associated with the determined resource type.
resourceType (String): Optional. Specifies the resource type to be used in looking up the template to render the provided context. If this isn't provided then the resource type will be pull from context._meta._sling_resourceType. If the resource type can't be determined from either source then the context is output in a tabular format. The value "raw" can be explicitly passed as well to force the tabular format. "raw" can be useful during development and debugging, but should not be used in production code.
/* Example context provided to HBS template*/
{
title: "Top Level Title",
header: {
_meta: {_sling_resourceType: "fshr/demo/content/header"},
title: "Landing Page",
name: "Herald"
},
customHeader: {
title: "Personal Landing Page",
name: "Jane"
}
}
Helper block that allows outputting of content only in author mode. The {{else}} block is optional.
none
Checks the truthiness of the result of comparing valueOne against valueTwo. The type of comparison done is based on the operand provided.
valueOne (String|number) : First value in the comparison
operand (String) : Must be one of the follwing values:
- "==" : Returns true if valueOne and valueTwo are abstractly equal (uses == JS operator)
- "!=" : Returns true if valueOne and valueTwo are not equal
- "===" : Returns true if valueOne and valueTwo are strictly equal (uses === JS operator)
- "<" : Returns true if valueOne is less than valueTwo
- "<=" : Returns true if valueOne is less than or equal to valueTwo
- ">" : Returns true if valueOne is greater than valueTwo
- ">=" : Returns true if valueOne is greater than or equal to valueTwo
- "&&" : Returns true if valueOne and valueTwo are truthy
- "||" : Returns true if either valueOne or valueTwo is truthy
- "contains" : Returns false if valueOne does not have a
indexOf
function otherwise the result ofvalueOne.indexOf(valueTwo)
is returned - "nonmeta" : Returns true if valueOne is String and does not match
/^(jcr|bl|cq|sling)\:/
. valueTwo is ignored. - "nonblank" : Returns true if valueOne is truthy. valueTwo is ignored.
- "iseven" : Returns true if valueOne is an even number. valueTwo is ignored.
- "isstring": Returns true if valueOne is a String. valueTwo is ignored.
- "in" : Returns true if valueOne is a member of the array resulting from splitting valueTwo on ','.
valueTwo (String|number) : Second value in the comparison. May not be required depending on the operand used.
Same as ifop, but it asserts that the negative of the expression is true.
See ifop Arguments. The same list of arguments apply, but the inverse will be returned for each operand.
Translates the the given text. The langugage can optionally be specified. If a language is not provided then the langauage will be inferred from the page, if possible. Parameters can be specified in the text using '{number}'. If a translation for the text isn't found in the dictionary associated with the determined language then the provided text will be rendered.
language (String) : Optional. The language to translate the text into. If no language is directly provided then it tries to determine the page language by checking the following properties in the root template context in the specified order:
- @root.meta.iso
- @root.meta.language
0,1...n (String) : The associated parameter in the text will be replaced with these values.
Replaces all matches of find
in text
with replaceWith
. Regexes can be used as the value of find
.
text (String) : Text to modify.
find (String) : The pattern to find.
replaceWith (String) : Matches in text will be replaced with this value.
Trims whitespace from the provided text
text (String) : Text to modify.
Counts the number of properties in the provided toCount
. Does not include properties that have a key that matches /^(jcr|bl|cq|sling)\:/
.
toCount (Object|Array) : Object to count properties of. If an array is provided this will return the length of the array.
type (String) : Optional. Mechanism to filter which child members are included in the count. Currently the only supported type is components. This will only include members that are Objects and have a child property called sling:resourceType.
/* context provided to handlebar template */
{
children: ["one", "two", "three"],
header: {
"jcr:title" : "Home Run",
"sling:resourceType": "fshr/demo/content/header",
link: "www.google.com",
count: 4
},
nodes: {
node000: {
"sling:resourceType": "fshr/demo/content/node",
"key": "value"
},
node001: {
"sling:resourceType": "fshr/demo/content/node",
"key": "value"
},
node003: {
"key": "value"
}
}
Returns the remainder of divident divided by divisor.
dividend (number) : Number to be divided.
divisor (number) : Number to divide by.
Appends a revision number before the extension of the specified asset. The revision number only changes when the asset itself is modifie making this very useful for ensuring unique URLs for caching purposes.
staticAsset (String) : Reference to a valid static asset. Should begin with '/alt/'.
Output the specified logMessage
to the node console. The log message will be prepended with 'TEMPLATE_LOG'. Useful for debugging and development, but should not be used in production.
logMessage (String) : Message to output to the console.
Rewrites the assetPath
to use the /alt/img-opt route which caches and optimizes raw images from the repository.
assetPath (String) : Path to the asset to be included. Must start with '/content/dam'. If the path matches /\.(png|svg|gif)$/
it will be returned unmodified.
quality (number) : Optional. The compression quality to be used. Defaults to 70.
size (String|number) : Optional. Indicates the desired geometry of the final image. Any valid geometry defined here can be specified.
Returns a data attribute that can be added to a DOM element. This data attribute is only rendered in author. Holding alt+clicking the DOM element with the data attribute will open the Edit UI with focus on the configuration associated with the current component context.
none
Compiles the provided markdownText
and returns the resultant HTML
markdownText (String) : Text in Markdown format.
Returns the first non empty value found. Primarily used as a mechanism to provide a default value if the preferred value is empty.
valueOne...valueN (String|number|Object|Array) : List of values to pick from.
/* context provided to handlebar template */
{
title: "Big Screen"
}
{{{{raw-helper}}}}
{{! Escaped Handlebar template}}
{{{{/raw-helper}}}}
The body of the raw-helper will be completely ignored when evaluating the Handlebar template. Useful if you need to have a client side Handlebar template. Note the need for double, double brackets.
none
<script id="header" type="text/x-handlebars-template">
{{{{raw-helper}}}}
<div> Name: {{ headerTitle }} </div>
{{#each items}}
<p>{{.}}</p>
{{/each}}
{{{{/raw-helper}}}}
</script>
{{!-- RESULT:
<script id="header" type="text/x-handlebars-template">
<div> Name: {{ headerTitle }} </div>
{{#each items}}
<p>{{.}}</p>
{{/each}}
</script>
--}}