hibase comes with a templating language that lets you manipulate pieces of data and inject them into SQL queries, API calls and more.
The hibase templating language is characterised by double curly braces, which can include expressions, mathematical calculations or functions.
Any of the following is valid syntax:
`2 + 3 = {{ 2 + 3 }}`
# => 2 + 3 = 5
`The date of today is {{ $today }}`
# => The date of today is 2018-07-27
`The date of yesterday is {{ $today - 1 }}`
# => The date of yesterday is 2018-07-26
`I have lived in {{ join(["Berlin", "London", "Paris"], ", ") }}`
# => I have lived in Berlin, London, Paris
Expression Parameters
Depending on the context of your current work, parameters might be provided automatically and / or defined by you. Parameters can be referenced within the hibase templating language by prefixing them with a dollar symbol ($).
For example, provided the following parameters are available...
{
"name": "t-shirt",
"price": 200,
"attributes": {
"color": "blue",
"size": "XL"
}
}
..., you could write any of the following as a hibase expression:
`Price with VAT: {{ $price * 1.19 }}`
# => Price with VAT: 238.0
`Color: {{ $attributes.color }}, Size: {{ $attributes.size }}`
# => Color: blue, Size: XL
Global Parameters
To make your life easier, hibase provides some global parameters that are always accessible within expressions. These are:
| Parameter | Example value | Supports math |
|---|---|---|
$now |
2018-07-27 13:08:08 +0000 |
Yes |
$nowInt |
1532696944 |
Yes |
$today |
2018-07-27 |
Yes |
Functions
The hibase templating language supports functions, which take one or more arguments and return some result, for example:
`{{ split("hello.world", ".") }}`
# => ["hello", "world"] (Array)
`{{ join(["hello", "world"], " ") }}`
# => `hello world` (String)
`{{ format_datetime( $now, '%m-%d' ) }}`
# => `07-27` (String)
Functions can be nested and combined with one another, for example:
`{{ join( split("hello.world", "."), " ") }}`
# => `hello world` (String)
String functions
split( string(String), separator(String)) => (Array)
Splits a string according to the provided separator and returns an array.
quote( string(String), quote_char(String) = '"') => (String)
Quotes the provided string with the provided character.
strip( string(String) ) => (String)
Removes trailing whitespaces from the passed string.
is_blank( string(String) ) => (String)
Returns true in case the passed string is blank (null or empty).
format_datetime( date_time(DateTime), directive(String)) => (String)
Formats the provided DateTime according to the provided directive (the syntax for which is available here)
len( string(String) ) => (Integer)
Returns the length of the passed string.
concat( value1(String), value2(String), [, ..., valueN(String)] ) => (String)
Concatenates the passed values into a single string.
replace( string(String), replace_string(String), with_string(String) ) => (String)
Replaces all occurrences of replace_string within string with with_string.
upcase( string(String) ) => (String)
Turns all letters within string to capital letters.
downcase( string(String) ) => (String)
Turns all letters within string to small letters.
snakecase( string(String) ) => (String)
Turns the passed string into its snakecase version (e.g. Hello World into hello_world).
string_includes( string(String), includes_string(String) ) => (Boolean)
Checks whether includes_string is included in string.
starts_with( string(String), starts_with_string(String) ) => (Boolean)
Checks whether string starts with starts_with_string.
strip_newlines( string(String) ) => (String)
Strips newline characters from the passed string.
to_datetime( string(String) ) => (Datetime)
Converts the passed string to a datetime value.
to_date( string(String) ) => (Date)
Converts the passed string to a date value.
min_datetime | max_datetime ( value1(String | Date | Datetime), value2(String | Date | Datetime) ) => (Datetime)
Compares the passed arguments, which can be strings, dates or datetimes, and returns the min / max value.
Mathematical Functions and Operators
All functions from Ruby's Math module are supported.
Additionally, the following operators are available: +, -, *, /, ^, together with the following custom functions:
min | max | sum | avg ( value1(Numeric), value2(Numeric), [, ..., valueN(Numeric)] ) => (Numeric)
Returns the minimum / maxumum / sum / average of the passed values.
round | roundup | rounddown ( value(Numeric), digits = 0 ) => (Numeric)
Rounds the value to the desired digits.
Logical Functions and Operators
The following logical operators can be used to compare values: <, >, <=, >=, !=, ==, || (or), && (and).
These can be used alone and / or in conjunction with the following logical functions:
if( condition(Boolean), valueIfTrue(Varying), valueIfFalse(Varying) ) => (Varying)
Checks a condition and returns a value according to the result, for example:
# { "param1": 5 }
`{{ if( $param1 < 10, "low", "high" ) }}`
# => `low`
and( condition1(Boolean), condition2(Boolean) [, ..., conditionN(Boolean)] ) => (Boolean)
Checks that all passed values equal true.
or( condition1(Boolean), condition2(Boolean) [, ..., conditionN(Boolean)] ) => (Boolean)
Checks that at least one of the passed values equal true.
not( condition(Boolean) ) => (Boolean)
Checks that the passed value equals false.
Array functions
join( json_array(Array), separator(String) = ', ' ) => (String)
Takes an array and joins it into a string.
take_index( json_array(Array), index(Integer)) => (Varying)
Takes an array and extracts the given index (akin to array[i]).
array_diff( json_array1(Array), json_array2(Array), [, ..., json_arrayN(Array)] ) => (Array)
Removes elements of json_array2 to json_arrayN from json_array1 and returns the resulting array.
array_union( json_array1(Array), json_array2(Array), [, ..., json_arrayN(Array)] ) => (Array)
Returns the unique elements of all passed arrays in the order they are first encountered.
array_concat( json_array1(Array), json_array2(Array), [, ..., json_arrayN(Array)] ) => (Array)
Concatenates the passed arrays into a single one without removing duplicate values.
array_intersection( json_array1(Array), json_array2(Array), [, ..., json_arrayN(Array)] ) => (Array)
Returns the common intersection of all passed arrays.
array_unique( json_array(Array) ) => (Array)
Returns a new array made of unique values of the passed array.
Hash functions
merge( json_object(Object), json_object2(Object), [, ..., json_objectN(Object)] ) => (Object)
Merges the passed objects into one Hash.
hash_compact( json_object(Object) ) => (Object)
Removes keys which have null values from the passed object.
hash_except( json_object(Object), key1(String), [, ..., keyN(String)] ) => (Object)
Removes the passed keys from the passed object.
take_key( json_object(Object), key(String) ) => (Varying)
Returns the value corresponding to the passed key.
to_url_query( json_object(Object) ) => (String)
Returns a URL query string generated from the passed object's keys and values.
Reducing functions
each( json_array(Array), function( $element, [ $element_index ] )(Function) ) => (Array)
This is a special function which iterates over arrays and processes each element within another iterating function, which must be passed as a second argument (akin to a map-reduce function). It returns an array of the processed elements.
Each element can be referenced within arguments of the iterating function as $element, for example:
`{{ each( [1.143, 2.261, 3.674], round($element, 1) ) }}`
# => [1.1, 2.3, 3.7] (Array)
The function also provides an $element_index value that can be used within the iterating function, for example:
`{{ each( [4 ,3, 2, 1], ($each_index) ) }}`
# => [0, 1, 2, 3] (Array)
As a shorthand, if the iterating function accepts the sole iteree as its argument, its name will suffice and $element will be passed implicitly:
`{{ each( [1.143, 2.261, 3.674], round ) }}`
# => [1, 2, 4] (Array)
each_in_range( from_value(Varying), to_value(Varying), [step(Integer)], function( $element, [ $element_index ] )(Function) ) => (Array)
The each_in_range function works similarly to the each function, but it allows to define a range of numbers, strings or dates upon which to iterate instead of requiring a predefined array.
Each element can be referenced within arguments of the iterating function as $element, for example:
`{{ each_in_range( 1, 5, ($element * 2) ) }}`
# => [2, 4, 6, 8, 10] (Array)
The function works with ranges of strings as well:
`{{ each_in_range( "a", "e", concat($element, $element) ) }}`
# => ["aa", "bb", "cc", "dd", "ee"] (Array)
An optional step parameter (default 1) can be passed to define the increment between each element in the range:
`{{ each_in_range( 1, 10, 2, ($element) ) }}`
# => [1, 3, 5, 7, 9] (Array)
The function also provides an $element_index value that can be used within the iterating function, for example:
`{{ each_in_range( 1, 5, ($each_index) ) }}`
# => [0, 1, 2, 3, 4] (Array)
Varying input functions
The following functions take varying input types.
ifnull(value(Varying), valueIfNull(Varying)) => (Varying)
Returns value if value is not null, otherwise valueIfNull.
ifnotnull(value(Varying), valueIfNotNull(Varying)) => (Varying)
Returns valueIfNotNull if value is not null, otherwise null.
Encoding functions
encode_for_uri(json_object(Object)) => (String)
Takes a key-value pair JSON object and converts it to a valid application/x-www-form-urlencoded MIME type
base64_encode(input(Varying)) => (String)
Returns the RFC 4648 compliant Base64-encoded version of the provided input. No line feeds are added.
jwt_encode(jwt_payload(Object), jwt_signing_key = null, jwt_encoding_algorithm = null) => (String)
Takes a key-value pair JSON object and encodes it into a JWT token according to the provided signing key and encoding algorithm. Valid encoding algorithms are: HS256, HS512256, HS384, HS512, RS256, RS384, RS512.