Conditional blocks allow you to display different content in your documents based on data values. GroupDocs.Assembly for .NET supports if/elseif/else syntax similar to C#, enabling you to create dynamic documents that adapt their content based on conditions.
The main syntax elements are:
<<if [condition]>> - display content if condition is true
<<elseif [condition]>> - display content if previous conditions are false and this condition is true
<<else>> - display content if all conditions are false
<<if>> - closing tag
Here are the steps to use conditional content:
Create a template with conditional blocks
Define conditions using C# expressions that return boolean values
Provide content for each condition branch
Assemble the document with your data
Note
Conditional expressions must return boolean values. You can use comparison operators (==, !=, <, >, <=, >=), logical operators (&&, ||, !), and method calls that return bool.
Customer: <<[customer.Name]>>
<<if [customer.Balance > 1000]>>
Status: Premium Customer
You have a high balance and qualify for special benefits.
<<else>>
Status: Standard Customer
Thank you for your business.
<<if>>
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicclassProduct{publicstringName{get;set;}publicdecimalPrice{get;set;}publicboolInStock{get;set;}}publicstaticvoidConditionalInDataBand(){List<Product>products=newList<Product>{newProduct{Name="Laptop",Price=999.99m,InStock=true},newProduct{Name="Mouse",Price=29.99m,InStock=false},newProduct{Name="Keyboard",Price=79.99m,InStock=true}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ConditionalTemplate.docx","ConditionalOutput.docx",newDataSourceInfo(products,"products"));Console.WriteLine("Conditional content in data band generated successfully.");}
Template:
<<foreach [in products]>>
<<[Name]>> - <<[Price]:"C">>
<<if [InStock]>>
✓ In Stock
<<else>>
✗ Out of Stock
<<if>>
<</foreach>>
usingGroupDocs.Assembly;usingSystem.Collections.Generic;publicclassOrder{publicList<OrderItem>Items{get;set;}publicboolHasItems(){returnItems!=null&&Items.Count>0;}}publicclassOrderItem{publicstringName{get;set;}}publicstaticvoidConditionalWithMethods(){varorder=newOrder{Items=newList<OrderItem>{newOrderItem{Name="Product"}}};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("ConditionalTemplate.docx","ConditionalOutput.docx",newDataSourceInfo(order,"order"));Console.WriteLine("Conditional with methods processed successfully.");}
Template:
<<if [order.HasItems()]>>
Order contains items.
<<else>>
Order is empty.
<<if>>
Warning
Conditional blocks must be properly closed with <<if>>. Nested conditional blocks are supported, but ensure proper nesting and closing of all blocks.
Advanced Usage Topics
To learn more about table-row conditional blocks, conditional formatting in different document types, and advanced conditional scenarios, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: