Templates

Helper Functions

The following helper functions are available in the template.ejs file of invoice and proposal templates.

#t

Returns a translated string for key.

t (key: string): string

#formatDate

Returns a formatted date string, respecting the language of the invoice/proposal.

formatDate (date: string): string

#formatMoneyValue

Returns a formatted money value, respecting the language of the invoice/proposal and the currency of the money value.

formatMoneyValue (value: MoneyValue): string

#formatQuantity

Returns a string containing the quantity of an item and its unit label in the correct language of the invoice or proposal. If the language of the invoice or proposal is not part of the unit's labels, it will fallback to show the unit in another language.

formatQuantity (quantity: number, unit: Unit | null): string

#Example

// item.unit is a unit for "hours":
formatQuantity(1, item.unit) // "1 Hour"

// item.unit is a unit for "days":
formatQuantity(2, item.unit) // "2 Days"

// item.unit is null
formatQuantity(2, item.unit) // "2"

#formatUnit

Returns a unit's plural or singular label in the correct language of the invoice or proposal. If the language of the invoice or proposal is not part of the unit's labels, it will fallback to show the unit in another language.

formatUnit is used internally by formatQuantity and can be used to show only the label of a unit.

formatUnit (quantity: number, unit: Unit | null): string

#Example

// item.unit is a unit for "hours":
formatUnit(1, item.unit) // "Hour"

// item.unit is a unit for "days":
formatUnit(2, item.unit) // "Days"

// item.unit is null
formatUnit(2, item.unit) // ""

#join

Joins together an array of strings by a given separator, skipping falsey values.

join (strings: Array, separator: string): string

#Example

join(['Hello', '', 'World', undefined], ' • ') // "Hello • World"

#joinAddress

Joins together the components of an address by a given separator, skipping falsey values.

This helper handles several known address formats, such as German, US and UK addresses.

join (strings: Array, separator: string): string

#Example

join(invoice.seller.address, ' • ') // "Company Name • Some street 76 • 99999 Berlin • Germany"

#countryCode

Returns the name of the country, respecting the language of the invoice/proposal.

countryCode (code: string): string

#field

Returns the value of the given custom field.

field (fieldName: string): any

#clsx

The popular clsx function for generating class names.