Skip to content

Commit

Permalink
expose the printf function in handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
RFSH committed Mar 29, 2024
1 parent 919c80b commit 47d10f6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
21 changes: 20 additions & 1 deletion docs/user-docs/handlebars.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This document summarizes the key concepts of Handlebars that are relevant to Der
- [$location](#location)
- [$session](#session)
- [Helpers](#helpers)
- [printf helper](#printf-helper)
- [formatDate helper](#formatdate-helper)
- [humanizeBytes helper](#humanizebytes-helper)
- [Math Helpers](#math-helpers)
Expand Down Expand Up @@ -485,9 +486,27 @@ A Handlebars helper call is a simple identifier, followed by zero or more parame
{{HELPER_NAME PARAM1 PARAM2 }}
```

### printf helper

You can use the `printf` helper to format a value. The expected format follows the same syntax as [PreFormat](pre-format.md#syntax).

Syntax:
```
{{printf value format }}
```

Example:
```
{{printf 3.1415 "%.1f" }} ==> "3.1"
{{printf 43 "%4d" }} ==> " 43"
```

Keep in mind that `printf` doesn't check the validity of the given values. So for example if the value might not be a number, you cannot blindly use the `d` type and should guard against it.

### formatDate helper

You can use the `formatDate` helper to take any `date` or `timestamp[tz]` value and format it according to the [Pre Format Guide](pre-format.md#syntax-for-dates-and-timestamps).
You can use the `formatDate` helper to take any `date`, `timestamp`, or `timestamptz` value and format it according to the [Pre Format Guide](pre-format.md#syntax-for-dates-and-timestamps).

Syntax:
```
Expand Down
2 changes: 1 addition & 1 deletion js/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
"eq", "ne", "lt", "gt", "lte", "gte", "and", "or", "not", "ifCond",
"escape", "encode", "formatDate", "encodeFacet",
"regexMatch", "regexFindFirst", "regexFindAll",
"jsonStringify", "toTitleCase", "replace", "humanizeBytes",
"jsonStringify", "toTitleCase", "replace", "humanizeBytes", 'printf',
// math helpers
"add", "subtract"
];
Expand Down
7 changes: 7 additions & 0 deletions js/utils/handlebar_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
return module.encodeFacetString(options);
},

/**
* {{printf value "%4d" }}
*/
printf: function (value, format) {
return module._printf({ format: format }, value);
},

/**
* {{formatDate value format}}
*
Expand Down
8 changes: 7 additions & 1 deletion test/specs/print_utils/tests/01.print_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.execute = function (options) {
var printInteger = formatUtils.printInteger;
expect(printInteger(null)).toBe('');
expect(printInteger(12341234)).toBe('12,341,234');
expect(printInteger(0001)).toBe('1');
expect(printInteger('0001')).toBe('1');
expect(printInteger(233)).toBe('233');
expect(printInteger(23.)).toBe('23');
expect(printInteger(23.000000000)).toBe('23');
Expand Down Expand Up @@ -600,6 +600,12 @@ exports.execute = function (options) {
expect(module.renderHandlebarsTemplate("{{formatDate date 'YYYY'}}", {date: null})).toBe("");
});

it('printf helper', function () {
const render = (t, v) => module.renderHandlebarsTemplate(t, v);
expect(render('{{printf 3.1415 "%.1f" }}', {})).toBe('3.1');
expect(render('{{printf num "%4d" }}', { num: 43 })).toBe(' 43');
});

it('humanizeByte helper', function () {
// test overloading
expect(module.renderHandlebarsTemplate('{{humanizeBytes data }}', {data: 12345678})).toBe('12.3 MB');
Expand Down

0 comments on commit 47d10f6

Please sign in to comment.