Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/karmaniverous/handlebars

Exposes Lodash & Numeral.js as Handlebars helpers, plus some other goodies.
https://github.com/karmaniverous/handlebars

handlebars lodash numeraljs terraform

Last synced: about 8 hours ago
JSON representation

Exposes Lodash & Numeral.js as Handlebars helpers, plus some other goodies.

Awesome Lists containing this project

README

        

# Handlebars Goodies

This repo adds a handful of very useful helpers to Handlebars.js.

## dataAnchor

Renders an anchor tag with data attributes. Syntax:

```handlebars
{{dataAnchor '' '' '' '' '' ...}}
```

This template...

```handlebars
{{dataAnchor 'anchor text' 'merchantId' 'abc123' 'userId' 'xyz789'}}
```

... renders this HTML:

```html
anchor text
```

## lodash & numeral

These helpers expose the [Lodash](https://lodash.com/) and [Numeral.js](http://numeraljs.com/) libraries to your templates. Syntax:

```handlebars
{{lodash '' ...}}
{{numeral '' ...}}
```

Here's a combined example. If `amount = 1000000` then:

```handlebars
{{numeral 'format' (lodash 'divide' amount 100) '$0,0.00'}}
```

renders:

```html
$10,000.00
```

## params

This helper converts its arguments into an array. Very useful for Lodash functions that expect an array argument. For example:

```handlebars
{{lodash 'get' object (params 'a' 'b' 'c')}}
```

## logic

Performs logical operations on the arguments. Syntax:

```handlebars
{{logic '' ...}}
```

For example, all of these return `true`:

```handlebars
{{#if (logic 'and' true true true)}}
{{#if (logic 'or' true true false)}}
{{#if (logic 'not' false)}}:
{{#if (logic 'xor' true false false)}} (odd number of trues)
```

Parameters are evaluated for truthiness. Supported operations are `and`, `or`, `not`, and `xor`.

## ifelse

A ternary operator. Syntax:

```handlebars
{{ifelse }}
```

## json2tf

Renders an object as a Terraform literal using [json2tf](https://github.com/karmaniverous/json2tf). The syntax is:

```handlebars
{{json2tf [offset] [tabWidth]}}
```

For example:

```ts
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"
}
]
}
}
}
*/
```

## namify

Converts a string into a form valid for a particular target, substituting an optional delimiter for sequences of invalid characters. Syntax:

```handlebars
{{namify '' [delimiter]}}
```

If no `delimiter` is supplied, it will default to a `target`-specific value.

For example:

```handlebars
{{namify 's3' 'My Resource Name'}}

# my-resource-name
```

These targets are currently available:

| `target` | Description | default `delimiter` |
| -------- | ----------------------------------------------------------------------------------------------- | ------------------- |
| `s3` | [S3 bucket names](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html) | `'-'` |

## throwif

Checks an condition. If true, throws an error before evauating the block content. Syntax:

```handlebars
{{#throwif }}No Error!{{/throwif}}
```