Power up with enhanced syntax

Help Center

One of DocuGenerate’s most powerful features is the enhanced syntax option. This feature expands upon the default functionality, allowing for a richer, more versatile experience when crafting your templates.

By default, when creating a new template, the enhanced syntax option is disabled. To enable it, navigate to the template’s Settings panel and check the box labeled Enable the enhanced syntax.

Once enabled, the enhanced syntax offers several advanced functionalities.

Nested object properties

The enhanced syntax allows you to access nested object properties directly in the template.

For example, if you have this data:

[{
  "item": {
    "name": "Car"
  }
}]

You can access the `name property with this syntax:

[item.name]

Without the enhanced syntax, the data structure would need to look like this:

[{
  "item.name": "Car"
}]

Logical and mathematical expressions

With the enhanced syntax enabled, you can use logical and mathematical expressions directly in your template, such as +, -, *, /, >, <, ==, !=, <=, >=.

You can create complex conditional statements using the enhanced syntax:

[#items.length > 1]
There are multiple items.
[/]

The enhanced syntax also supports the following operators:

  • Ternaries: a ? b : c
  • Equality / Inequality: a == 1, a != 1
  • Relational: a > 1, a < 1, a >= 1, a <= 1
  • Logical AND: a && b
  • Logical OR: a || b
  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Modulo: a % b
  • Division: a / b
  • Assignments: a = 1
  • Operator precedence with parenthesis: (a && b) || c

Whitespace limitation

With the enhanced syntax enabled, you must be aware of how whitespace is interpreted in your JSON data.

In JSON data, a property containing whitespace, such as "First Name": "John", would typically correspond to the merge tag [First Name]. However, with enhanced syntax enabled, DocuGenerate does not recognize whitespace within merge tags. Therefore, properties containing whitespace must be written with a different notation in your JSON data.

For instance, instead of:

[{
  "First Name": "John"
}]

You should use:

[{
  "First_Name": "John"
}]

Then, in your template, you can use the merge tag [First_Name] to reference this data. If the property is nested within another object, you can access it like this: [User.First_Name].

Remember, this limitation only applies when the enhanced syntax is enabled. If it is disabled, whitespace within merge tags will be recognized as expected. This gives you the flexibility to choose the best approach for your data and your templates.

Examples

Example 1: Display content based on a boolean value

Template:

[#client.has_discount]
You have a discount of [client.discount] on your next purchase.
[/]

JSON Data:

[{
  "client": {
    "has_discount": true,
    "discount": "15%"
  }
}]

Result:

You have a discount of 15% on your next purchase.

Example 2: Display different content based on a value comparison

Template:

[#age >= 18]
You are eligible to vote.
[/]
[#age < 18]
You are not eligible to vote.
[/]

JSON Data:

[{
  "age": 2
}]

Result:

You are not eligible to vote.

Example 3: Display content based on a logical expression

Template:

[#client.is_vip && client.purchase_amount > 500]
As a VIP member who spent $[client.purchase_amount], you are entitled to a gift!
[/]

JSON Data:

[{
  "client": {
    "is_vip": true,
    "purchase_amount": 1000
  }
}]

Result:

As a VIP member who spent $1000, you are entitled to a gift!