Basic Template Syntax - Expression Tags and Fields
Leave feedback
Overview
GroupDocs.Assembly for .NET uses a simple, LINQ-based template syntax that allows you to embed C# expressions directly in your documents. Expression tags are the foundation of template syntax, enabling you to output data values and perform calculations.
When using a single data source, you can omit the data source name and use contextual access:
usingGroupDocs.Assembly;publicstaticvoidContextualAccess(){varproduct=new{Name="Laptop",Price=999.99m,Stock=50};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(product));Console.WriteLine("Document assembled with contextual access.");}
You can perform calculations directly in expression tags:
usingGroupDocs.Assembly;publicstaticvoidCalculationsInExpressions(){varorder=new{Quantity=5,UnitPrice=29.99m};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(order,"order"));Console.WriteLine("Document assembled with calculations.");}
Template with calculations:
Quantity: <<[order.Quantity]>>
Unit Price: <<[order.UnitPrice]>>
Total: <<[order.Quantity * order.UnitPrice]>>
Using String Methods
You can use .NET string methods in expressions:
usingGroupDocs.Assembly;publicstaticvoidStringMethods(){varcompany=new{Name="ABC Corporation",Website="www.example.com"};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(company,"company"));Console.WriteLine("Document assembled with string methods.");}
You can format expression results using format strings:
usingGroupDocs.Assembly;publicstaticvoidFormattedExpressions(){varinvoice=new{Amount=1500.50m,Date=newDateTime(2024,1,15),Percentage=0.15};DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(invoice,"invoice"));Console.WriteLine("Document assembled with formatting.");}
Format strings follow .NET formatting conventions. Use “C” for currency, “P” for percentage, and standard date/time format strings for dates.
Advanced Usage Topics
To learn more about data bands, conditional blocks, LINQ expressions, and advanced template syntax features, 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: