In several applications, it is required to render or to remove a complete text block in a template based on specific conditions. The following screenshot shows a template with a merge block (highlighted in red) that should be rendered, if the merge data contains data for this specific block with the name conditional:

ReportingCloud: Conditional text blocks based on merge blocks

Consider, we have the following classes as the data source in C# .NET:

public class Report
{
public string name { get; set; }
public Conditional conditional { get; set; }
}
public class Conditional
{
public string test { get; set; }
}
view raw data.cs hosted with ❤ by GitHub

The following code creates a new instance of the data class Report and an empty Conditional class. In the MergeSettings object, which is part of the ReportingCloud MergeBody, the RemoveEmptyBlocks property is set to true.

Report report = new Report();
report.name = "Name";
report.conditional = new Conditional();
ReportingCloud rc = new ReportingCloud("username", "password");
MergeBody body = new MergeBody();
body.MergeData = report;
body.MergeSettings = new MergeSettings() {
RemoveEmptyBlocks = true
};
List<byte[]> docs = rc.MergeDocument(body, "conditional_template.tx", ReturnFormat.TX);
view raw test.cs hosted with ❤ by GitHub

In case, the data for the merge block conditional is empty, the complete merge block gets removed, if RemoveEmptyBlocks is set to true. The following animation shows the result for both cases:

ReportingCloud: Conditional text blocks based on merge blocks

The logic that defines which block gets rendered or removed is not part of the template, but your data source and therefore, part of your application.