|
| 1 | +--- |
| 2 | +title: Handlebars Chart |
| 3 | +hide_title: true |
| 4 | +sidebar_position: 10 |
| 5 | +version: 1 |
| 6 | +--- |
| 7 | + |
| 8 | +## Handlebars Chart |
| 9 | + |
| 10 | +The Handlebars chart lets you render query results using a custom [Handlebars](https://handlebarsjs.com/) template. This gives you full control over how your data is displayed — from simple tables to rich HTML layouts. |
| 11 | + |
| 12 | +### Basic Usage |
| 13 | + |
| 14 | +In the chart editor, write a Handlebars template in the **Template** field. Your query results are available as `data`, an array of row objects. |
| 15 | + |
| 16 | +```handlebars |
| 17 | +{{#each data}} |
| 18 | + <p>{{this.name}}: {{this.value}}</p> |
| 19 | +{{/each}} |
| 20 | +``` |
| 21 | + |
| 22 | +### Built-in Helpers |
| 23 | + |
| 24 | +Superset registers several custom helpers on top of the standard Handlebars built-ins. |
| 25 | + |
| 26 | +#### `dateFormat` |
| 27 | + |
| 28 | +Formats a date value using [Day.js](https://day.js.org/) format strings. |
| 29 | + |
| 30 | +```handlebars |
| 31 | +{{dateFormat my_date format="MMMM YYYY"}} |
| 32 | +``` |
| 33 | + |
| 34 | +| Option | Default | Description | |
| 35 | +|--------|---------|-------------| |
| 36 | +| `format` | `YYYY-MM-DD` | A Day.js-compatible format string | |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +#### `stringify` |
| 41 | + |
| 42 | +Converts an object to a JSON string, or any other value to its string representation. |
| 43 | + |
| 44 | +```handlebars |
| 45 | +{{stringify myObj}} |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +#### `formatNumber` |
| 51 | + |
| 52 | +Formats a number using locale-aware formatting. |
| 53 | + |
| 54 | +```handlebars |
| 55 | +{{formatNumber myNumber "en-US"}} |
| 56 | +``` |
| 57 | + |
| 58 | +| Option | Default | Description | |
| 59 | +|--------|---------|-------------| |
| 60 | +| `locale` | `en-US` | A BCP 47 language tag | |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +#### `parseJson` |
| 65 | + |
| 66 | +Parses a JSON string into an object that can be used in your template. |
| 67 | + |
| 68 | +```handlebars |
| 69 | +{{parseJson myJsonString}} |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +#### `groupBy` |
| 75 | + |
| 76 | +Groups an array of objects by a key, powered by [handlebars-group-by](https://github.com/nicktindall/handlebars-group-by). |
| 77 | + |
| 78 | +```handlebars |
| 79 | +{{#groupBy data "department"}} |
| 80 | + <h3>{{value}}</h3> |
| 81 | + {{#each items}} |
| 82 | + <p>{{this.name}}</p> |
| 83 | + {{/each}} |
| 84 | +{{/groupBy}} |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +### Helpers from just-handlebars-helpers |
| 90 | + |
| 91 | +Superset also registers all helpers from the [just-handlebars-helpers](https://github.com/leapfrogtechnology/just-handlebars-helpers) library. These include a wide range of comparison, math, string, and conditional helpers. Commonly used ones include: |
| 92 | + |
| 93 | +#### Comparison |
| 94 | + |
| 95 | +| Helper | Description | Example | |
| 96 | +|--------|-------------|---------| |
| 97 | +| `eq` | Strict equality | `{{#if (eq status "active")}}` | |
| 98 | +| `eqw` | Weak equality | `{{#if (eqw count "5")}}` | |
| 99 | +| `neq` | Strict inequality | `{{#if (neq role "admin")}}` | |
| 100 | +| `lt` | Less than | `{{#if (lt score 50)}}` | |
| 101 | +| `lte` | Less than or equal | `{{#if (lte score 100)}}` | |
| 102 | +| `gt` | Greater than | `{{#if (gt price 0)}}` | |
| 103 | +| `gte` | Greater than or equal | `{{#if (gte age 18)}}` | |
| 104 | + |
| 105 | +#### Logical |
| 106 | + |
| 107 | +| Helper | Description | Example | |
| 108 | +|--------|-------------|---------| |
| 109 | +| `and` | Logical AND | `{{#if (and isActive isVerified)}}` | |
| 110 | +| `or` | Logical OR | `{{#if (or isAdmin isMod)}}` | |
| 111 | +| `not` | Logical NOT | `{{#if (not isDisabled)}}` | |
| 112 | +| `ifx` | Inline conditional | `{{ifx isActive "Yes" "No"}}` | |
| 113 | +| `coalesce` | Returns first non-falsy value | `{{coalesce nickname name "Anonymous"}}` | |
| 114 | + |
| 115 | +#### String |
| 116 | + |
| 117 | +| Helper | Description | Example | |
| 118 | +|--------|-------------|---------| |
| 119 | +| `capitalize` | Capitalizes first letter | `{{capitalize name}}` | |
| 120 | +| `uppercase` | Converts to uppercase | `{{uppercase status}}` | |
| 121 | +| `lowercase` | Converts to lowercase | `{{lowercase email}}` | |
| 122 | +| `truncate` | Truncates a string | `{{truncate description 100}}` | |
| 123 | +| `contains` | Checks if string contains substring | `{{#if (contains tag "urgent")}}` | |
| 124 | + |
| 125 | +#### Math |
| 126 | + |
| 127 | +| Helper | Description | Example | |
| 128 | +|--------|-------------|---------| |
| 129 | +| `add` | Addition | `{{add a b}}` | |
| 130 | +| `subtract` | Subtraction | `{{subtract total discount}}` | |
| 131 | +| `multiply` | Multiplication | `{{multiply price quantity}}` | |
| 132 | +| `divide` | Division | `{{divide total count}}` | |
| 133 | +| `ceil` | Ceiling | `{{ceil value}}` | |
| 134 | +| `floor` | Floor | `{{floor value}}` | |
| 135 | +| `round` | Round | `{{round value}}` | |
| 136 | + |
| 137 | +For the full list of available helpers, see the [just-handlebars-helpers documentation](https://github.com/leapfrogtechnology/just-handlebars-helpers). |
| 138 | + |
| 139 | +### Tips |
| 140 | + |
| 141 | +- Use raw blocks to escape Handlebars syntax if you need to display double curly braces literally. |
| 142 | +- Comparison helpers like `eq` must be wrapped in a subexpression when used with `#if`: `{{#if (eq myVal "foo")}}`. |
| 143 | +- HTML output is sanitized by default based on your Superset configuration (`HTML_SANITIZATION`). |
0 commit comments