Logical operations in DOCX templates

Logical operations determine how the markup in your template is generated based on the data provided. Unlike calculated properties, which are initialized upfront, logical operations are handled in real time as the document is generated.

You can use the following operators with the logical operations:

  • > greater than,

  • >= greater than or equal to,

  • < less than,

  • <= less than or equal to,

  • == equal to,

  • != not equal to,

  • && logical AND,

  • || logical OR.

List of logical operations

if

The #if logical operator creates a conditional block, allowing content to be displayed or hidden based on specific conditions.

Example structure:

{{#if property == null}}
Content
{{#else}}
Else content
{{/if}}

Examples

Template

Data

{{#if customer.vatRegistered}}
VAT ID: {{customer.vatID}}
VAT Amount: ${{customer.vatAmount|format(C)}}
{{/if}}
{
    "customer": [
        {
            "vatRegistered": true,
            "vatID": "12345678",
            "vatAmount": "36.60"
        }
    ]
}
{{#if customer != null}}
Name: {{customer.name}}
Address: {{customer.address}}
{{#else}}
No customer information provided.
{{/if}}
{
    "customer": [
        {
            "name": "John Doe",
            "address": "215 W 5th St, New York"
        }
    ]
}
{{#if isActive}}Active{{#else}}Disabled{{/if}}
{
    "isActive": false
}

Hint

For more information on conditional hiding, check out this article.

hide-if

The #hide-if logical operation hides the closest repeatable objects such as tables or list items based on specified conditions.

Note

To hide items from an array, we recommend using the filter function instead.

Examples

Template

Data

{{count}} {{#hide-if count == 1}}
{
    "count": 1
}
{{employee.name}} {{#hide-if employee.name == "Jessica"}}
{
    "employee": {
        "name": "Jessica"
    }
}
{{items.title}} {{#hide-if items|count() == 0}}
{{items.description}} {{#hide-if items.category == "Software" or delivery != true}}
{
    "items": [
        {
            "title": "Keyboard",
            "category": "Accessory",
            "description": "Sleek & Silent"
        }
    ],

    "delivery": true
}

Hint

For more information on conditional hiding, check out this article.

merge-left-if

The #merge-left-if logical operation merges the cell to the left based on specified conditions.

Examples

Template

Data

Result


#merge-left-if operation template
{
    "items": [
        {
            "name": "Computer desk",
            "type": "Accessory",
            "sold": 2
        },
        {
            "name": "Keyboard",
            "type": "Accessory",
            "sold": 0
        },
        {
            "name": "Mouse",
            "type": "Accessory",
            "sold": 5
        },
        {
            "name": "Laptop",
            "type": "N/A",
            "sold": 0
        }
    ]
}

#merge-left-if operation result