TX Text Control .NET Server

Template Assistant

Topic

Learning how merge fields, merge blocks and data-shaping rules fit together is easier with a live template in front of you. The Mail Merge Template Assistant is an online demo in which you design a template in the Document Editor step by step, from connecting JSON data to nested blocks with sorting and filter rules, and every step creates real TX Text Control fields and blocks.

The Template Assistant is a demo application, not a component of TX Text Control .NET Server. It's built on the Document Editor and MailMerge, so your application can do what it does with the same APIs.

What the Template Assistant Is

The Mail Merge Template Assistant is one of the technical demos of Text Control, which run on ASP.NET Core with .NET 10 and the Core version of TX Text Control .NET Server. Like the other demos, it has three areas: Try it to use the assistant, Learn to read how it works and Code to see the code behind it.

In the demo, the Document Editor has an additional Template Assistant group in its Reporting ribbon tab. The group opens a panel next to the document, which overlaps the editor instead of resizing it, so the document keeps its layout. The blog article Learning Mail Merge Templates from Basic Fields to Advanced Reporting Structures introduces the assistant.

Document Editor with the Template Assistant group in the Reporting ribbon tab and the assistant panel at the right, showing the Fields step with the fields available at the input position

The Steps of the Assistant

StepWhat you doDocumented in
1 DataLoad a JSON file or use the sample data of sales orders with products, customers and images. The assistant shows the structure of the data, and the same JSON fills the Reporting tab of the editorData Sources
2 FieldsInsert the fields that are available at the input position: fields of the document level outside blocks and fields of the current record inside a block. Values that contain image data become image placeholders, and barcodes can be inserted for values. IF, DATE and INCLUDETEXT fields are available as wellMerge Fields, Images, Barcodes
3 BlocksDesign a merge block from a collection of the data, with nested child collections and the fields of each level, and style its tableMerge Blocks, Nested Blocks
4 RulesSort the rows of a block, filter them and make a nested block depend on its parent recordSorting and Filtering
5 OutlineSee the fields and blocks of the document with their nesting and levels and jump to an elementTemplate Design

In the Blocks step, a dialog designs a merge block: you choose the outer collection and its nested child collections, check the fields of each level and pick a style for the tables, and Insert at caret inserts the nested blocks into the document:

Design a merge block dialog with the block hierarchy, the fields checked for each level and a preview of the parent and nested tables in the selected style

The Rules step sets the sorting and filter rules of a block and, for a nested block, a condition on its parent row; Apply rules to block stores them with the block:

Rules step of the Template Assistant with a sort order by SalesOrderDetailID, a filter for UnitPrice greater than 200 and a disabled conditional block option

When the input position is in a field that the assistant supports, the field's properties open in the panel, such as the name, the text before and after and the formats of a merge field, and changes apply immediately. Two more actions complete the workflow:

  • Preview merge merges the template with the JSON data on the server and shows the result in the editor. Restore template brings the template back.
  • Download template saves the template in the TX Text Control format with its fields, blocks, table styles and rules, ready for MailMerge.

The merged preview shows the nested blocks filled with the sample data, one table with its product row for each order detail:

Merged preview in the Document Editor, in which the order detail block and its nested product block are repeated with values from the sample data

How the Assistant Works

The assistant keeps no model of the template apart from the document itself. Fields are application fields, merge blocks are subtext parts, tables are regular tables and the sorting and filter rules are stored with the merge blocks, so the downloaded template works with MailMerge without the assistant. The work is divided between the browser and the server:

WhereWhat happensAPIs
BrowserReading the structure of the JSON, finding the fields for the input position, inserting and editing fields, showing the panelTXTextControl.loadJsonData, TXTextControl.addMergeField, TXTextControl.saveDocument
ServerChecking a block design against the data, generating nested blocks, styling tables, storing rules and merging the previewDataSourceManager.InsertMergeBlock, MergeBlockInfo, TableCell.CellFormat, MailMerge.MergeJsonData

For a new block, the browser saves the document in the TX Text Control format and sends it to the server. The server inserts the block with InsertMergeBlock, a MergeBlockInfo whose ChildBlocks describe the nested levels and a MergeBlockSettings for a table row block with a header row, and the browser loads the returned document. In the Document Editor section, Loading and Saving Documents shows how documents travel between the editor and a controller.

Reuse What the Assistant Shows

The Code area of the demo shows the code of the assistant: its JavaScript, its controller and its sample data. It's the actual code behind the demo, and you can study it and apply its patterns in your own application. Parts of it, such as the analysis behind the outline and helpers shared by all demos, aren't included in the code shown.

The documentation covers each pattern the assistant uses:

The demo site has related demos, such as the Nested Merge Block Designer and Merging Sub-Templates.

Next Steps