Functions

Functions add functionality to your template.

Documint uses an enhanced version of Handlebars as its templating engine. This means all valid Handlebars syntax is valid Documint syntax. Below is a list of custom functions that can be used in your Documint templates.

Combining Functions

You can combine functions by wrapping the inner function in parenthesis.

Example:

{{$link ($get "path.to.website_url") "Go to website"}}

Here we're using the $get function within the $link function by wrapping it inside of parenthesis.

General

$get

Returns the value of the given path.

Signature

{{$get path [defaultValue] [global=true|false]}}

Arguments

NameDescriptionTypeRequired

path

The path to value you are trying to display.

String

defaultValue

Value that is displayed if no value is found at the given path.

NumberString

global

If true, the given path will be relative to the global context. If false, the path will be relative to the current context. For example, if you are using the this function within a loop, the context will be the current item in the loop. If global is set to true, then the context will be set to the root of your data.

Boolean

Returns

The value at the given path or, if there is no value at the path and a default value is provided, the default value will be returned.

Example

{{$get "customer.company_name"}}
{{$get "customer.contacts[0].name"}}
{{$get "customer.contacts[1].email"}}
{{$get "customer.address.country" "United States"}}

To access a specific item in an array/list, add the position of the item in square brackets [] like so: [0]. Indices are zero-based which means the first item in the array/list is at position 0, the second item is at position 1 and so on.

This method always accesses data from its current context. For example, if it's used within a loop, then the context of the path is set to the current item in the loop.

$var

Creates a custom variable in your template which you can access from other parts of your template

Signature

{{$var name value}}

Arguments

NameDescriptionTypeRequired

name

The name of the variable. This will be used to access it later. Must be a valid JavaScript key

String

value

The value of the variable

Any

Returns

This function does not have a return value.

Example #1 - Basic

{{$var "custom_var" "Custom value"}}
...
{{custom_var}} // Used somewhere else in the template after the above declaration

Example #2 - Advanced

Create a custom variable and set the value to the results of the filter function where we filter items that have a quantity of 10 or more.

Repeat the section for each item in the custom variable

Variables can only be accessed after the point where they've been declared.

Content

$iframe

Documentation coming soon!

Creates a link to a given URL

Signature

{{$link url [text]}}

Arguments

NameDescriptionTypeRequired

url

URL value of the link

String

text

Text to display in document. If no value is provided the URL of the link will be displayed

String

Returns

Link to the given URL.

Example

{{$link company.website}}
{{$link company.website company.name}}

$md

Renders markdown content

Signature

{{$md content}}

Arguments

NameDescriptionTypeRequired

content

Markdown content

String

Example

{{$md notes}}

List/Arrays

#each

Loops over an array (list) of items. This is different than the other functions because it has an opening ({{#each}}) and closing ({{/each}}) tag and uses an # instead of a $ to prefix the function name. The content within the opening and closing tags is set to the context of the current item in the loop. This means that any variables used within the opening and closing tags are relative to the current item in the loop. If the list of items being looped over is a list of objects, then variable names will represent properties on that object. See example below.

Signature

{{#each array }}{{this}}{{/each}}

Arguments

NameDescriptionTypeRequired

array

List of items to repeat/loop over

String ArrayNumber ArrayBoolean ArrayObject ArrayArray Array

Returns

Content within the opening and closing tags, for each item in the given array/list.

Example - Array of Objects

In this example we're looping over an array/list of Objects. We can access the properties of the current object in the loop simply by using the name of the property. In the case below, we're displaying the name property of each object using the {{name}} token. This works because the within the opening and closing tags of the #each function, the context is set to the current item in the loop.

{{#each characters}}{{name}}, {{/each}}

Example - Array of Strings

In this example, we're looping over an array/list of Strings. We use the this keyword to reference the current item in the loop. this always represents the current item in the loop, even when looping over data types other than Strings.

{{#each characters}}{{this}}, {{/each}}

$filter

Filters an array/list of items.

Signature

{{$filter array pathoperator [value]}}

Arguments

NameDescriptionTypeRequired

array

Accepts either an array of data or a relative path, as a String, to an array.

StringString ArrayNumber ArrayObject Array

Path to property if looping over an array/list of Objects.

Note: If looping over a list of Strings, Numbers or Booleans then this should be the operator.

String

The operator used to evaluate each item in the array/list.

Note: If looping over an array/list of Strings, Numbers or Booleans then this should be the value to check against.

String

value

The value to compare variable's value to. Required when using an operator that requries two arguments.

StringNumberBoolean

Example - Array of Objects

{{$filter products "category" "isIn" "Disguise,Explosive"}}

$group

Documentation coming soon.

$join

Concatenates an array (list) of items into a string.

Signature

{{$join array [path] [separator] [final] }}

Arguments

NameDescriptionTypeRequired

array

Accepts either an array of data or a relative path, as a String, to an array.

StringString ArrayNumber ArrayObject Array

path

If the array is a collection (an array of objects) then use this path to specify which property in the object should be concatenated.

Default value: ", "

String

separator

String that separates each item in the array.

String

final

The string that's used to separate the last two items.

String

Example #1 - Array of strings

{{$join categories ", " " & "}}

Example #2- Collection (array of objects)

{{$join items "name" ", " " & "}}

Example #3 - Path to array

{{$join "company.employees" "name" ", " " & "}}

Helpers

$extname

Returns the extension name of a filename.

Signature

{{$extname filename }}

Arguments

NameDescriptionTypeRequired

filename

The name of the file that you're trying to get the extension name of.

String

Example

{{$extname filename}}

$inspect

Documentation coming soon..

Last updated