Conditionally hide blocks in DOCX templates

There are two types of blocks for hiding: single and repeatable. Use a hide-block-if formatter for the single blocks and a filter operation for the repeatable ones. Here, we will learn how it works, but additionally, review the demo for such case.

Hide table rows

Let us assume we have a collection of employees. We are going to render a table with information about them, but we want to hide employees from a specific department (development).

This is JSON representation of employees data:

{
  "employees": [
    {
      "name": "Derek Clark",
      "department": "marketing"
    },
    {
      "name": "Jessica Adams",
      "department": "sales"
    },
    {
      "name": "Anil Mittal",
      "department": "development"
    }
  ]
}

We will use the template like this:

Hide table row template

The filter operation should be placed before the table:

{{nonDev = employees|filter(value.department != "development")}}

The token {{nonDev}} does not contain the object of Anil Mittal. He was excluded because working in the development department. After that, the alias token can be used for rendering table rows.

The result table will look like this:

Hide table row result

Hide bullet list items

We will use the same JSON data as in the example for table rows above.

Our template will look like this:

Hide bullet list template

The filter operation should be placed before the list:

{{nonDev = employees|filter(value.department != "development")}}

The token {{nonDev}} does not contain the object of Anil Mittal. He was excluded because working in the development department. After that, the alias token can be used for rendering list items.

The result list will look like this:

Hide bullet list result

Hide arbitrary block

If you want to hide arbitrary section that is not a table row or a bullet list item, we recommend you to wrap it into a table cell with invisible borders.

In this example, we will use information about a company as source data for the template and the formatter hide-block-if. The formatter hides parts of a document if some value or collection of values meets the condition in it.

This is JSON representation of company data:

{
    "companyName": "Plumsail",
    "site": "http://plumsail.com",
    "contacts": null
}

We want to display company name, site and contacts and hide contacts if they are empty. We will just wrap all text related to contacts with a table cell with transparent borders:

hide arbitrary block template

As you can see, we added this string to the template for contact information: {{contacts}:hide-block-if(value == null)}. The hide-block-if formatter works the same way as in the example for table rows above.

The result will look like this:

hide arbitrary block result

Hide arbitrary block within paragraph

If you want to hide only a part of a paragraph, we advise you to use a rich text content control. You can find it on the “Developer” tab in Word:

Hide bullet list result

If you do not have such a tab, add it to your ribbon (File > Options > Customize Ribbon):

Hide bullet list result

In this example, we will use order information as source data for the template and the formatter hide-block-if. The formatter hides parts of a document if some value or collection of values meets the condition in it.

This is JSON representation of company data:

{
    "orderNumber": "NL-3752",
    "delivery": {
      "address": "",
      "date": ""
    },
    "phoneNumber": "+16102458741"
}

We want to display all the text in the template except the delivery details if they are empty. Thus, we need just to wrap them with a rich text content control:

Hide bullet list result

This token hides the content control if its condition is resolved as true: {{delivery}:hide-block-if(value.address == "")}. The hide-block-if formatter works the same way as in the example for table rows above.

The result will look like this:

Hide bullet list result