How to create a mail merge mustache template?

By Varun Satish | Automation

How to create a mail merge mustache template?

What is Mail Merge?

Mail merge is a method of reading data from a data source and replacing corresponding variables in another document. It usually requires two files, one for storing the merge fields - the variable for the data to be inserted. The other contains both the instructions for formatting the variable data and the information that will be identical across each result of the mail merge.

E.g, data is read from a database, JSON, CSV, Excel spreadsheets, google sheets, or other forms of structured data and inserted into an existing document template for e-mails, Invoices, or any other that requires dynamic data to be populated. The templates can be often Word documents, PDFs, emails, form letters, etc.

How to create a template using mustache syntax?

Creating Word templates in Mustache requires a bit of understanding of basic codes. Even though mustache isn’t a templating syntax, it can be valuable in creating advanced templates that can be used for mail merge. Let us look at the following example to understand how using mustache syntax can help us create some great templates.

Mustache sample invoice template

In the above template of an Invoice, you can its entirely designed using the mustache syntax. This word template uses mustache to design dynamic headers, body, footer, and even tables inside the template.

Let us look at how each syntax helps populate data for generating a dynamic invoice.


{{#foreach header}} - Marks the beginning of a header. Syntax to be added inside the header region
{{/foreach header}} - Closing the header

Body - Mandatory

{{#foreach body}} - Marks the beginning of the contents in the document body
{{/foreach body}} - Marks the end of the document body


{{#foreach footer}} - Sets the beginning of the document footer. Syntax is to be added inside the footer region.
{{/foreach footer}} - Sets the end of the document footer

Tables inside a template

How to dynamically generate a table?
The foreach should start and end inside the same row for dynamically generating rows. For generating a column, the foreach should start and end in the same cell.

Mustache table syntax

You can pass the data to the mustache template as JSON for the above sample invoice. Following is the sample data for generating documents for the above template.

    "Documents": [
            "header": {
                "docType": "Original Copy"
            "body": {
                "Column1": "Quantity",
                "Column2": "Description",
                "Column3": "Unit Price",
                "Column4": "Total",
                "Invoice": {
                    "number": 14957,
                    "date": "25-02-2022",
                    "instructions": "Instruction Invoice 1",
                    "subTotal": 200150,
                    "tax": "18%",
                    "dueDate": "25-03-2022",
                    "shipping": "200"
                "Biller": {
                    "phone": "989797",
                    "fax": "998463",
                    "email": "",
                    "web": "",
                    "address": {
                        "company": "Organization 1",
                        "street": "Street 1",
                        "city": "City 1",
                        "zip": "123456"
                "Recepient": {
                    "name": "recepeint 1",
                    "address": {
                        "street": "testveien 3 ",
                        "city": "City 1",
                        "zipcode": "0555",
                        "country": "Country 1"
                "Product": [
                        "quantity": 1,
                        "description": "item 1",
                        "unitPrice": 45,
                        "totalPrice": 867
                        "quantity": 2,
                        "description": "item 2",
                        "unitPrice": 123,
                        "totalPrice": 3464
                        "quantity": 3,
                        "description": "item 3",
                        "unitPrice": 323,
                        "totalPrice": 6564
            "footer": {
                "phone": "phone123",
                "fax": "fax123",
                "email": "",
                "web": ""

IF statements in Templates

Microsoft Word allows you to use mail merge fields and Mustache tags with the IF statement. They are useful while generating documents with conditional values. The IF merge field prevents unwanted spaces when a condition is not satisfied or a value is empty.

{ IF [Condition] [Display Value 1] [Display Value 2] }


Samples to begin with

Download the sample template
Download the sample data JSON file

Related Blog Posts