This repo adds a handful of very useful helpers to Handlebars.js.
Renders an anchor tag with data attributes. Syntax:
This template...
... renders this HTML:
<a data-merchantId="abc123" data-userId="xyz789">anchor text</a>
These helpers expose the Lodash and Numeral.js libraries to your templates. Syntax:
Here's a combined example. If amount = 1000000
then:
renders:
$10,000.00
This helper converts its arguments into an array. Very useful for Lodash functions that expect an array argument. For example:
Performs logical operations on the arguments. Syntax:
For example, all of these return true
:
Parameters are evaluated for truthiness. Supported operations are and
, or
, not
, and xor
.
A ternary operator. Syntax:
Renders an object as a Terraform literal using json2tf. The syntax is:
For example:
import { Handlebars } from '@karmaniverous/handlebars';
const data = {
amount: 1234.567,
anchorText: 'anchor text',
merchantId: 'abc123',
userId: 'def456',
extra: { a: [1, 2, { c: 'd' }] },
};
const template = `
output "config" {
description = "Global config."
value = {{json2tf (lodash "omit" this "merchantId" "userId") 4 4}}
}`;
console.log(Handlebars.compile(template, { noEscape: true })(data));
/*
output "config" {
description = "Global config."
value = {
amount = 1234.567
anchorText = "anchor text"
extra = {
a = [
1,
2,
{
c = "d"
}
]
}
}
}
*/
Converts a string into a form valid for a particular target, substituting an optional delimiter for sequences of invalid characters. Syntax:
If no delimiter
is supplied, it will default to a target
-specific value.
For example:
These targets are currently available:
target |
Description | default delimiter |
---|---|---|
s3 |
S3 bucket names | '-' |
Checks an condition. If true, throws an error before evauating the block content. Syntax: