Templating
Template in OpenAF uses the HandleBars javascript library which has very good documentation. Nevertheless here is a collection of tricks that might be useful and most used helpers.
Name | Expression | Description |
---|---|---|
Escaping content | {{{text}}} | Without three braces HandleBars will replace ‘&’ with the HTML escape sequence as well as other characters. |
Escaping expression | \{{something}} | To escape a HandleBars expression just add ´\´ before the first character |
Accessing an element of an array | {{lst.[0]}} | Accessing a position of a provided array. |
Accessing an object property with symbols | {{obj.[something else].[item-name]}} | To access any object property with a space or other symbols use ‘[]’. |
Accessing a property from a different level | {{#each list}}Name = {{name}}, Company = {{../company}}\n{{/each}} | Consider the following map = { company: "myC", list: [ { name: "john", name: "anne" } ]} . You can access “parent” levels as if they were a folder hierarchy (e.g. concatenating “../” as needed) |
Using helpers within helpers | {{$f '%20s' ($env 'LANG')}} | The first helper ‘$env’ will run retrieving the current value for the environment variable ‘LANG’ and the result is used by the second helper, ‘$f’, that will format the resulting string in a right-justified line with 20 characters long (‘%20s’) |
OpenAF helpers
In some cases it might be necessary to load them using
ow.loadTemplate().addOpenAFHelpers()
before using them.
Helper | Example | Description |
---|---|---|
$$ | {{$$ aProperty aDefaultValue}} | Outputs the value of aProperty or, if not defined, returns the defaultValue. |
$debug | {{$debug __flags}} | The same as stringifyInLine for the provided argument. |
$stringify | {{{$stringify __flags}}} | Given a map will output the stringify version of it. |
$stringifyInLine | {{{$stringifyInLine __flags}}} | Given a map will output the stringify version without new-lines. |
$toYAML | {{$toYAML __flags}} | Given a map will output the YAML version of it. |
$env | {{$env 'PATH'}} | Given an environment variable will output the corresponding value. |
$escape | {{{$escape aString}}} | Given a string value will return the escaped version of it. |
$f | [{{$f '%20s' aString}}] | Performs a $f operation with the provider parameter for the provided aString. |
$ft | [{{$ft '%20s' aString}}] | Performs a $ft operation with the provider parameter for the provided aString. |
$path | {{$path '@' anArray}} | Executes the function $path over the provided element. |
$toSLON | {{$toSLON aMap}} | Given a map will output the SLON version of it. |
$get | {{$get 'res'}} | Performs the equivalent operation to OpenAF’s $get. |
$getObj | {{$getObj 'res' 'elements'}} | Performs the equivalent operation to OpenAF’s $get and applies the function $path using the second parameter. |
$dateDiff | {{$dateDiff aDate 'days'}} | Given aDate will output the time difference (using the third optional argument (seconds (default), minutes, hours, days, months, weeks or years)) |
$switch, $case, $default | {{#$switch value}}{{#$case 'a'}}value a{{/$case}}{{$default 'no value'}}{{/$switch}} | Implements a switch function with case and default helpers. |
Conditional helpers
In some cases it might be necessary to load them using
ow.loadTemplate().addConditionalHelpers()
before using them.
The conditional helpers included mimic the ones provided by Assemble for comparison
Helper | Example |
---|---|
{{#isnt}} | {{#isnt number 5}}Kiss my shiny metal ass!{{else}}Never mind :({{/isnt}} |
{{#and}} | {{#and great magnificent}}Kiss my shiny metal ass!{{else}}Never mind :({{/and}} |
{{#contains}} | {{#contains truth "best"}}Absolutely true.{{else}}This is a lie.{{/contains}} |
{{#gt}} | {{#gt number 8}}Kiss my shiny metal ass!{{else}}Never mind :({{/gt}} |
{{#gte}} | {{#gte number 8}}Kiss my shiny metal ass!{{else}}Never mind :({{/gte}} |
{{#if_gt}} | {{#if_gt x compare=y}} ... {{/if_gt}} |
{{#if_gteq}} | {{#if_gteq x compare=y}} ... {{/if_gteq}} |
{{#is}} | {{#is x compare=y}} ... {{/is}} |
{{#ifeq}} | {{#ifeq x compare=y}} ... {{/ifeq}} |
{{#compare}} | {{#compare [leftvalue ] [operator ] [rightvalue ]}}foo{{else }}bar{{/compare }} |
{{#lt}} | {{#lt number 3}} ... {{else}} {{/lt}} |
{{#lte}} | {{#lte number 3}} ... {{else}} {{/lte}} |
{{#or}} | {{#or great magnificent}}Kiss my shiny metal ass!{{else}}Never mind :({{/or}} |
{{#unless_eq}} | {{#unless_eq x compare=y}} ... {{/unless_eq}} |
{{#unless_gt}} | {{#unless_gt x compare=y}} ... {{/unless_gt}} |
{{#unless_gteq}} | {{#unless_gteq x compare=y}} ... {{/unless_gteq}} |
{{#unless_lt}} | {{#unless_lt x compare=y}} ... {{/unless_lt}} |
{{#unless_lteq}} | {{#unless_lteq x compare=y}} ... {{/unless_lteq}} |
Format helpers
In some cases it might be necessary to load them using
ow.loadTemplate().addFormatHelpers()
before using them.
The format helpers are all the ow.format.* functions available. For example:
templify("")
// 9.77 MB
templify("")
// 1111011
Adding a helper
Example of adding a simple template helper:
ow.template.addHelper("publicip", (aIP, aElement) => $$(ow.loadNet().getPublicIP(aIP)).get(aElement) )
templify("{{ip}} is in {{publicip ip 'country'}}", { ip: "1.1.1.1" })
// 1.1.1.1 is in Australia