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 text using “map” formatter

You can hide a line of text depending on the token value.

In this example, we will use order information as source data for the template.

We want to have the option of sharing a coupon with the customer after an order has been fulfilled.

Our template will look like this:

../../_images/hide-arbitrary-text-map-template.webp

This is JSON representation of order data:

{
    "orderNumber": "NL-3752",
    "includeCoupon": "Yes"
}

The map formatter can be used as follows:

{{includeCoupon}:map("Yes" = "As a token of our appreciation, we'd like to offer you 10% off your next order. Just use the code DISCOUNT10 at checkout.", "No" = "")}

This way, the coupon information will be displayed if the token’s value is set to “Yes”, and hidden if the value is set to “No”.

The map formatter can accept an unlimited number of values. We can use this to provide different discount tiers:

{{includeCoupon}:map("10%" = "We'd like to offer you 10% off your next order. Use the code DISCOUNT10 at checkout.", "20%" = "We'd like to offer you 20% off your next order. Use the code DISCOUNT20 at checkout.", "No" = "")}

The result for the “20%” value will look like this:

../../_images/hide-arbitrary-text-map-result.webp

Hide arbitrary text using “if” formatter

You can hide a line of text depending on the token value. As in the previous example, we want an option of sharing a coupon with the customer after an order has been fulfilled.

Our template will look like this:

../../_images/hide-arbitrary-text-if-template.webp

This is JSON representation of order data:

The if formatter can be used as follows:

{{includeCoupon}:if(value == "Yes", "As a token of our appreciation, we would like to offer you 10% off your next order. Just use the code DISCOUNT10 at checkout.", "")}

This way, the coupon information will be displayed if the token’s value is set to “Yes” and hidden in case of any other value.

Hide arbitrary text using rich text content control

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