Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/accordproject/template-playground/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Formulas allow you to execute JavaScript expressions within your templates to perform calculations, transformations, and complex logic.

Formula Syntax

Formulas are enclosed in {{% %}} and must start with return:
{{% return expression %}}
All formula expressions must begin with the return keyword.

Basic Calculations

Perform arithmetic operations on your data:
{{% return value1 + value2 %}}
{{% return amount * 1.1 %}}
{{% return total - discount %}}

Example: Simple Calculation

{
  "$class": "hello@1.0.0.HelloWorld",
  "name": "John Doe"
}

Financial Calculations

Formulas are perfect for computing taxes, totals, and other financial values:

VAT and Total Calculation

{
  "$class": "payment@1.0.0.PaymentReceipt",
  "amount": 250.00,
  "currency": "USD",
  "vatPercent": 10
}

String Operations

Manipulate strings using JavaScript string methods:
{{% return text.length %}}

Date and Time

The now Variable

Access the current date and time using the implicit now variable:
{{% return now.toISOString() %}}
Today is **{{% return now.toISOString() %}}**.

Date Methods

{{% return now.toISOString() %}}
Output: 2024-06-15T10:30:00.000Z

Number Formatting

Use JavaScript number methods for precise formatting:

Common Number Methods

{{% return value.toFixed(2) %}}
Formats to 2 decimal places: 123.46

Array Operations

Work with arrays using JavaScript array methods:
{{% return items.length %}}

Conditional Expressions

Use ternary operators for inline conditionals:
{{% return condition ? 'value if true' : 'value if false' %}}

Examples

Status: {{% return active ? '✓ Active' : '✗ Inactive' %}}

Complex Calculations

Combine multiple operations for sophisticated logic:

Multi-tier Discount Calculation

{{% return amount > 1000 ? amount * 0.85 : amount > 500 ? amount * 0.90 : amount * 0.95 %}}

Percentage Calculation

Progress: {{% return Math.round((completed / total) * 100) %}}}%

Duration Calculation

{{% return Math.floor(durationInMonths / 12) %}}} years and {{% return durationInMonths % 12 %}}} months

Mathematical Operations

Use the Math object for advanced calculations:
{{% return Math.round(value) %}}
{{% return Math.floor(value) %}}
{{% return Math.ceil(value) %}}

Best Practices

Formula expressions must start with return:
Good: {{% return amount * 1.1 %}}
Bad: {{% amount * 1.1 %}}
Use .toFixed() for consistent decimal places:
{{% return (price * quantity).toFixed(2) %}}
Check for undefined before calculations:
{{% return discount ? (amount * discount / 100).toFixed(2) : '0.00' %}}
Break complex calculations into multiple formulas:
Good:
- Subtotal: {{% return amount.toFixed(2) %}}
- VAT: {{% return (amount * 0.1).toFixed(2) %}}
- Total: {{% return (amount * 1.1).toFixed(2) %}}

Avoid:
- Total: {{% return (amount + (amount * vatRate / 100) + (amount * serviceFee / 100) - discount).toFixed(2) %}}
Division by zeroAlways check for zero before division:
{{% return total > 0 ? (completed / total * 100).toFixed(1) : 0 %}}%
Reuse variable formattingFor simple variable output, use the as formatter instead of formulas:
Preferred: {{amount as "0,0.00"}}
Also works: {{% return amount.toFixed(2) %}}

Variables

Learn about variable syntax and formatting

Conditionals

Use formulas in conditional expressions

Lists

Apply formulas within list iterations

Build docs developers (and LLMs) love