Conditional blocks allow you to display different content in your documents based on data values. GroupDocs.Assembly for Python via .NET supports if/elseif/else syntax, 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 expressions that return boolean values
Provide content for each condition branch
Assemble the document with your data
Note
Conditional expressions must return boolean values. The template writes them with C# operators, also when Python calls the assembler: comparison operators (==, !=, <, >, <=, >=), logical operators (&&, ||, !), and method calls that return bool. A Python bool in the data is a boolean in the template.
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>>
<<if [report.Items.Any()]>>
Items:
<<foreach [item in report.Items]>>- <<[item]>>
<</foreach>><<else>>
No items available.
<</if>>
The items are strings, so the data band names its iteration variable (item) and outputs it with <<[item]>>. The band opens in the item’s paragraph and closes at the start of the next one, so it repeats one paragraph per item; a <<foreach>> tag on a line of its own would repeat an empty paragraph too.
Conditional Content in Data Bands
Use conditional blocks within foreach loops:
fromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerproducts=[{"Name":"Harbor Blend whole bean coffee","Price":Decimal("18.40"),"InStock":True},{"Name":"Lighthouse Espresso beans","Price":Decimal("21.75"),"InStock":False},{"Name":"Chestnut honey","Price":Decimal("7.80"),"InStock":True},]assembler=DocumentAssembler()assembler.assemble_document("product-stock-status.docx","product-stock-status-report.docx",DataSourceInfo(products,"products"))print("Conditional content in data band generated successfully.")
A condition can call any method of a value that returns bool, such as Items.Any() or Name.StartsWith("Q"). A Python dict carries data, not methods, so a test that a .NET class would write as its own method (order.HasItems()) is computed in Python and passed as a key:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembleritems=[{"Name":"Smoked sea salt"}]order={"Items":items,"HasItems":len(items)>0,}assembler=DocumentAssembler()assembler.assemble_document("order-contents.docx","order-contents-report.docx",DataSourceInfo(order,"order"))print("Conditional with methods processed successfully.")
<<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. A block closed with <<if>> instead fails with “Tag ‘if’ is not well-formed. Tag end is unexpected.”
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: