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 a specific type of the formatter: hide-block-if-empty. The formatter hides parts of a document if some value or collection of values is null or empty.

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 into 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-empty}. The hide-block-if-empty formatter works the same way as in the example for table rows above.

The result will look like this:

hide arbitrary block result