Expressions
Built-in Functions
REST API Endpoints
REST API Objects

# Built-in Helpers

# #if

You can use the if helper to conditionally render a block. If its argument returns false, undefined, null, "", 0, or [], DocumentEngine will not render the block.

template

When you pass the following input to the above template

input

This will produce the result as below:

output

If the input is an empty JSONObject {}, then author will become undefined and if condition fails, resulting in the output as follow:

When using a block expression, you can specify a template section to run if the expression returns a falsy value. The section, marked by else is called an "else section".

template

# includeZero

The includeZero=true option may be set to treat the conditional as not empty. This effectively determines if 0 is handled by the positive or negative path.

# Sub-Expressions

Helpers are the proposed way to add custom logic to templates. You can write any helper and use it in a sub-expression.

For example, in checking for initialization of a variable the built-in #if check might not be appropriate as it returns false for empty collections (see Utils.isEmpty).

You could write a helper that checks for "undefined" such as:

preparationScript

Then use your helper as a sub-expression:

template

# #unless

You can use the unless helper as the inverse of the if helper. Its block will be rendered if the expression returns a falsy value.

template

If looking up license under the current context returns a falsy value, DocumentEngine will render the warning. Otherwise, it will render nothing.

# #each

You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over.

template

when used with this context:

input

will result in:

output

You can use the this expression in any context to reference the current context.

You can optionally provide an else section which will display only when the list is empty.

template

When looping through items in each, you can optionally reference the current loop index via {{@index}}.

Additionally for object iteration, {{@key}} references the current key name:

The first and last steps of iteration are noted via the @first and @last variables when iterating over an array.

Nested each blocks may access the iteration variables via depth based paths. To access the parent index, for example, {{@../index}} can be used.

# #with

The with-helper allows you to change the evaluation context of template-part.

template

when used with this context:

input

will result in:

output

with can also be used with block parameters to define known references in the current block. The example above can be converted to

template

Which allows for complex templates to potentially provide clearer code than ../ depthed references allow for.

You can optionally provide an {{else}} section which will display only when the passed value is empty.

template
input

# lookup

The lookup helper allows for dynamic parameter resolution using DocumentEngine variables.

This is useful for resolving values for array indexes.

template

It can also be used to lookup properties of object based on data from the input. The following is a more complex example that uses lookup in a sub-expression to change the evaluation context to another object based on a property-value.

template

# log

The log helper allows for logging of context state while executing a template.

template

It delegates to DocumentEngine.logger.log which may be overridden to perform custom logging.

Any number of arguments may be passed to this method and all will be forwarded to the logger.

template

The log level may be set using the level hash parameter. Supported values are debug, info, warn, and error. When omitted, info is the default value,

Logging is conditional based on the level and to value set in DocumentEngine.logger.level, which defaults to info. All log statements at or above the current level will be output.

template

# is

The is helper is a function that you can use in your DocumentEngine templates to compare two values and return the result of the comparison.

Usage

To use the is helper, you can include it in your template like this:

This example uses the is helper within an if block, which allows you to specify different output depending on the result of the comparison. If the comparison returns true, the output of the options.fn function (the code within the if block) is returned. If the comparison returns false, the output of the options.inverse function (the code within the else block) is returned.

The is helper takes three arguments:

  • value1: The first value to compare.
  • operator: The comparison operator to use. This can be one of the following values:
    • ==: Returns true if value1 is equal to value2.
    • ===: Returns true if value1 is strictly equal to value2.
    • >: Returns true if value1 is greater than value2.
    • <: Returns true if value1 is less than value2.
    • >=: Returns true if value1 is greater than or equal to value2.
    • <=: Returns true if value1 is less than or equal to value2.
    • !=: Returns true if value1 is not equal to value2.
    • !==: Returns true if value1 is not strictly equal to value2.
    • $>: Returns true if value1 contains value2, case-insensitive.
    • <$: Returns true if value2 contains value1, case-insensitive.
    • $$>: Returns true if value1 contains value2, case-sensitive.
    • <$$: Returns true if value2 contains value1, case-sensitive.
    • <#: Returns true if value1 is contained in an array value2.
    • &&: Returns true if arrays value1 and value2 have at least one element in common.
  • value2: The second value to compare.

Examples

Here are some examples of how you might use the is helper in your templates:

# and

The and helper is a function that you can use in your DocumentEngine templates to perform a logical AND operation on any number of arguments. It returns true if all of the arguments are truthy, and false otherwise.

Usage

To use the and helper, you can include it in your template like this:

This example uses the and helper within an if block, which allows you to specify different output depending on the result of the AND operation.

The and helper takes any number of arguments, and returns true if all of them are truthy.

# or

The or helper is a function that you can use in your DocumentEngine templates to perform a logical OR operation on any number of arguments. It returns true if at least one of the arguments is truthy, and false otherwise.

Usage

To use the or helper, you can include it in your template like this:

This example uses the or helper within an if block, which allows you to specify different output depending on the result of the OR operation.

The or helper takes any number of arguments, and returns true if at least one of them is truthy.

# formatNumber

The formatNumber helper is a function that you can use in your DocumentEngine templates to format a number to a specified string format. It uses the numeral.js library to perform the formatting.

Usage

To use the formatNumber helper, you can include it in your template like this:

This example outputs a <p> element containing the formatted number.

The formatNumber helper takes two arguments:

  • number: The number to be formatted. This can be a JavaScript number or a string that can be parsed by the numeral.js library.
  • format: The string format to use for the number. This can be any valid numeral.js format string, such as 0,0 for a number with thousands separators, or 0.00% for a number with a percentage symbol.

Examples

Here are some examples of how you might use the formatNumber helper in your templates:

In the first example, the formatNumber helper is used to format the number variable using the 0,0 format, which will output something like The number is 10,000. In the second example, the formatNumber helper is used to format the number variable using the 0.00% format, which will output something like The number is 10.00%. In the third example, the formatNumber helper is used to format the number variable using the $0,0.00 format, which will output something like The number is $10,000.00.

# date

The date helper is a function that you can use in your DocumentEngine templates to format a date to a specified string format. It uses the moment.js library to perform the formatting.

Usage

To use the date helper, you can include it in your template like this:

This example outputs a <p> element containing the formatted date.

The date helper takes two arguments:

  • date: The date to be formatted. This can be a JavaScript Date object or a string that can be parsed by the moment.js library.
  • format: The string format to use for the date. This can be any valid moment.js format string, such as DD/MM/YYYY for a date in the format dd/mm/yyyy, or dddd, MMMM Do YYYY, h:mm:ss a for a full date and time in a long format.

Examples

Here are some examples of how you might use the date helper in your templates:

In the first example, the date helper is used to format the date variable using the DD/MM/YYYY format, which will output something like The date is 10/12/2022. In the second example, the date helper is used to format the date variable using the dddd, MMMM Do YYYY, h:mm:ss a format, which will output something like The date is Tuesday, December 10th 2022, 5:15:30 pm. In the third example, the date helper is used to format the date variable using the YYYY-MM-DD format, which will output something like The date is 2022-12-10.

# filterKey

The filterKey helper is a function that you can use in your DocumentEngine templates to filter an array of objects based on a key and a comparison operation. It uses the is helper to perform the comparison.

Usage

To use the filterKey helper, you can include it in your template like this:

This example outputs a <p> element for each item in the filtered array.

The filterKey helper takes four arguments:

  • array: The array to be filtered. This should be an array of objects.
  • key: The key to use for the comparison. This can be a string representing a property of the objects in the array.
  • op: The comparison operator to use. This can be any valid operator that the is helper supports.
  • value: The value to compare the object's property to.

# getKey

The getKey helper is a function that you can use in your DocumentEngine templates to get an array of values from a specific key in an array of objects.

Usage

To use the getKey helper, you can include it in your template like this:

This example outputs a <p> element for each value in the array.

The getKey helper takes two arguments:

  • array: The array to get values from. This should be an array of objects.
  • key: The key to get the value from. This can be a string representing a property of the objects in the array.

# count

The count helper is a function that you can use in your DocumentEngine templates to count the number of items in a list.

Usage

To use the count helper, you can include it in your template like this:

This example outputs a <p> element containing the count of the items.

The count helper takes any number of arguments, each of which should be an item you want to count.

Examples

Here are some examples of how you might use the count helper in your templates:

In this example, the count helper is used to count the number of items in the list 'apple', 'banana', 'cherry', which will output something like The count is 3.

# sum

The sum helper is a function that you can use in your DocumentEngine templates to calculate the sum of a list of numbers.

Usage

To use the sum helper, you can include it in your template like this:

This example outputs a <p> element containing the sum of the numbers.

The sum helper takes any number of arguments, each of which should be a number or a string that can be parsed as a number.

Examples

Here are some examples of how you might use the sum helper in your templates:

In this example, the sum helper is used to calculate the sum of the numbers 1, 2, 3, 4, and 5, which will output something like The sum is 15.

# avg

The avg helper is a function that you can use in your DocumentEngine templates to calculate the average of a list of numbers.

Usage

To use the avg helper, you can include it in your template like this:

This example outputs a <p> element containing the average of the numbers.

The avg helper takes any number of arguments, each of which should be a number or a string that can be parsed as a number.

Examples

Here are some examples of how you might use the avg helper in your templates:

In this example, the avg helper is used to calculate the average of the numbers 1, 2, 3, 4, and 5, which will output something like The average is 3.

# median

The median helper is a function that you can use in your DocumentEngine templates to calculate the median of a list of numbers.

Usage

To use the median helper, you can include it in your template like this:

This example outputs a <p> element containing the median of the numbers.

The median helper takes any number of arguments, each of which should be a number or a string that can be parsed as a number.

Examples

Here are some examples of how you might use the median helper in your templates:

In this example, the median helper is used to calculate the median of the numbers 1, 2, 3, 4, and 5, which will output something like The median is 3.

# list

The list helper is a function that you can use in your DocumentEngine templates to create an array from the provided arguments and return a string representation of this array.

Usage

To use the list helper, you can include it in your template like this:

This example uses the list helper to create an array from its arguments. The helper then joins these items into a string, separated by commas, for display purposes.

The list helper can take any number of arguments:

  • item1, item2, item3, ...: Each item represents a value to be included in the list. You can pass as many items as you need, and they will be concatenated into an array by the helper.

Here is a practical example of using the list helper within a Handlebars template:

In this example, the list helper takes three string arguments ('Apple', 'Banana', 'Cherry') and returns the list "Apple, Banana, Cherry".

# multiline

The multiline helper is a function that can be used in your DocumentEngine templates to preserve newlines.

Usage

To use the multiline helper, include it in your template like this:

This example outputs a <p> element where the newlines in textVariable are preserved.

Examples

Here are some examples of how you might use the multiline helper in your templates:

In this example, the multiline helper is used to preserve the newlines between 'Line 1', 'Line 2', and 'Line 3', which will output each line on a new line.

# markdown

The markdown helper is a function that allows you to use Markdown syntax in your DocumentEngine templates.

Usage

To use the markdown helper, include it in your template like this:

This example shows how you can output a <p> element that renders the Markdown content in markdownVariable as HTML.

Examples

Here are some examples of how you might use the markdown helper in your templates:

In this example, the markdown helper is used to render Markdown syntax. It would render a heading and a bold text.

# barchart

The barchart helper is a function that creates a bar chart.

Usage

To use the barchart helper, include it in your template like this:

This example shows how you can output a <div> element that contains a bar chart representing the data in chartData.

Data Example

Example data for the barchart helper:

Examples

Here's how you might use the barchart helper in your templates with the provided data:


Output

abcdef0.00.20.40.60.81.01.21.41.61.82.0

Option List

The barchart helper accepts several options to customize the appearance and behavior of the bar chart.

  • width: Specifies the width of the chart.
  • height: Specifies the height of the chart.
  • margin: Defines the margins around the chart.
  • barColor: Sets the color of the bars in the chart.
  • labels: Object containing labels for the x-axis and y-axis.

# linechart

The linechart helper is a function that creates a line chart.

Usage

To use the linechart helper, include it in your template like this:

This example shows how you can output a <div> element that contains a line chart representing the data in chartData.

Data Example

Example data for the linechart helper:

Examples

Here's how you might use the linechart helper in your templates with the provided data:


Output

1.01.52.02.53.03.54.04.55.05.56.01.01.11.21.31.41.51.61.71.81.92.0

Examples

Here's how you might use the linechart helper in your templates with the provided data:


Output

1.01.52.02.53.03.54.04.55.05.56.01.01.11.21.31.41.51.61.71.81.92.0

Option List

The linechart helper provides various options to tailor the line chart.

  • width: Sets the width of the chart.
  • height: Sets the height of the chart.
  • margin: Determines the chart's margins.
  • lineWidth: Defines the thickness of the line.
  • lineColor: Specifies the color of the line.
  • lineColors: An array of colors for multiple lines.
  • isCurve: Determines if the line should be curved.
  • tickSize: Size of the ticks on the axes.
  • tickPadding: Padding around the ticks on the axes.

# piechart

The piechart helper is a function that creates a pie chart.

Usage

To use the piechart helper, include it in your template like this:

This example shows how you can output a <div> element that contains a pie chart representing the data in chartData.

Data Example

Example data for the piechart helper:

Examples

Here's how you might use the piechart helper in your templates with the provided data and color options:


Output

abcdef

Option List

The piechart helper offers a range of options for customizing the pie chart.

  • width: Determines the width of the chart.
  • height: Determines the height of the chart.
  • colorRange: Array of two colors defining the color range for the pie slices.
  • radius: Sets the radius of the pie chart.

# qrcode

The qrcode helper is a function that creates a qrcode.

Usage

To use the qrcode helper, include it in your template like this:

Examples

Here's how you might use the qrcode helper in your templates with the provided data and size options:


Output



Option List

The qrcode helper offers a range of options for customizing the pie chart.

  • size: Determines the size of the qrcode. If omitted the qr is full sized and needs a style with a size

# @data variables

The following @data variables are implemented by DocumentEngine.

# @root

Initial context with which the template was executed.

Unless explicitly modified, this value is consistent across all portions of the page rendering, meaning it can be used within partials where depthed parameters are unable to reference their parent templates.

# @first

Set to true by the each helper for the first step of iteration.

# @index

Zero-based index for the current iteration step. Set by the each helper.

# @key

Key name for the current iteration step. Set by the each helper when iterating over objects.

# @last

Set to true by the each helper for the last step of iteration.