Skip to content

Commit a2a4cf9

Browse files
committed
Document calc variable helper
1 parent f3aa0b4 commit a2a4cf9

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

docs/templates/template-variables.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,55 @@ Uses [@epilot/pricing](https://github.com/epilot-dev/pricing) to format the amou
360360

361361
```handlebars
362362
{{asCurrency 100.50 "EUR"}}
363-
```
363+
```
364+
365+
## Excel-like Formulas
366+
367+
You can also use Excel-like formulas in your templates with the `calc` helper.
368+
369+
Examples:
370+
371+
```handlebars
372+
Price: {{price}}
373+
Qty: {{qty}}
374+
375+
Total (rounded 2): {{ calc "ROUND(price * qty, 2)" }}
376+
Discounted: {{ calc "ROUND((price * qty) * (1 - discount), 2)" }}
377+
378+
Caps with IF: {{ calc "IF(qty > 10, 10, qty)" }}
379+
380+
Using named args override:
381+
{{ calc "ROUND(a + b, 0)" a=fee b=shipping }}
382+
383+
Min/Max:
384+
{{ calc "MAX(price1, price2, price3)" }}
385+
```
386+
387+
## Available formulas
388+
389+
### Arithmetic operators
390+
391+
- `+` - Addition
392+
- `-` - Subtraction
393+
- `*` - Multiplication
394+
- `/` - Division
395+
- `^` - Exponentiation
396+
- `%` - Modulus (remainder of division)
397+
- `()` - Parentheses for grouping expressions
398+
- `-number` - Negation (unary minus)
399+
400+
### Formula functions
401+
402+
- `ABS(number)` - Returns the absolute value of a number.
403+
- `AND(condition1, condition2, ...)` - Returns `true` if all conditions are truthy.
404+
- `AVERAGE(number1, number2, ...)` - Returns the average of a set of numbers.
405+
- `CEIL(number)` - Rounds a number up to the nearest integer.
406+
- `FLOOR(number)` - Rounds a number down to the nearest integer.
407+
- `IF(condition, value_if_true, value_if_false)` - Returns one value if condition is truthy and another value if it's falsey.
408+
- `MAX(number1, number2, ...)` - Returns the largest number in a set of numbers.
409+
- `MIN(number1, number2, ...)` - Returns the smallest number in a set of numbers.
410+
- `NOT(condition)` - Reverses the value of its argument. Returns `true` if its argument is truthy and `false` if its argument is falsey.
411+
- `OR(condition1, condition2, ...)` - Returns `true` if any condition is truthy.
412+
- `ROUND(number, [places])` - Rounds a number to a specified number of decimal places. If places is omitted, it defaults to 0.
413+
- `SUM(number1, number2, ...)` - Returns the sum of a set of numbers.
414+
- `RAND()` - Returns a random number between 0 (inclusive) and 1 (exclusive).

0 commit comments

Comments
 (0)