You can build templates in your Afterpattern apps that produce Word documents (.docx files). These are known as Word templates.
Word templates function through the use of variables. Any text that is meant to be included, edited, or removed from your final document must be represented as a variable:
This lesson will teach you everything you need to know to create powerful Word templates that allow you, your clients, or the public to use your Afterpattern apps to automate almost any document.
Create a new Word template
There are two methods for creating a Word template:
- Online with the Afterpattern Word template editor; or
- Directly in Microsoft Word with the Afterpattern Word add-in.
Online Word templates
To create a template online with the Afterpattern Word template editor, go to the templates tab of your app and click "Create online":
To enter the template editor, click "Edit" on your new template.
You are now ready to start adding variables to your template.
Microsoft Word add-in templates
You can build Word templates directly in Microsoft Word with our free add-in. This gives you the full power of Microsoft's formatting features and the ability to convert the .docx file your template produces into PDFs.
Insert the add-in
From within Microsoft Word, click "Insert" and then "Get Add-ins":
Search for "Afterpattern".
Once you insert the Afterpattern add-in, you'll see the Afterpattern logo in the "ribbon" of your Microsoft Word editor:
How it works
Building a template with the Word add-in is the exact same procedure as building your template online with the Afterpattern Word editor (i.e. you add variables to the template). However, the Word add-in requires extra steps for saving your template to a particular app. We've create a standalone article to explain this process:
Intro to adding variables
Variables are how data gets populated into your template.
Before you can add variables to your template, you must have variables defined inside your app. Most of your variables will be defined inside your app either by a question or by pulling from your database.
Understand your variable's datatype
Every variable has a datatype, e.g. text, number, date, etc. It's important to consider your variable's datatype before adding it to a template. For example, do you know what type of variable a checkbox question creates? It's not a text-type variable, but instead a series of boolean (true/false) variables. Therefore, you likely do not want to insert a checkbox question variable onto your template (watch the video under Add variables with conditional logic for a demonstration of how to work with checkbox questions).
Adding variables directly vs. using conditional logic
There are two methods for adding variables to your template:
- Directly; and
- With conditional logic
If you add a variable directly, when the template produces the final document it will replace that variable with the variable's value (essentially performing a "find & replace" function).
For example, the variable user-name (see below), defined by a question, is placed directly onto the template. When the template produces the final document, user-name will be replaced with whatever answer the end-user gives to the question that defines it:
If you add a variable with conditional logic, you can write an expression that determines whether or not the variable is included in the final document.
For example, this allows you to conditionally insert the words "shall" or "shall not", or conditionally insert a clause into a list of clauses:
In the example above, if clause #2 was not inserted the list would automatically renumber itself to account for its absence (learn more about conditionally insert multi-level lists in Word templates).
Continue reading to learn how to add variables directly and with conditional logic.
Add variables directly
To add a variable directly, simply click "Insert variable" (if using the online Word editor) or "Insert existing variable" (if using the Microsoft add-in):
This 1 minute video provides a demonstration:
Add variables with conditional logic
You can add conditional logic to any variable on your Word template. To add conditional logic, click on that variable and then click "Conditionally insert":
Clicking "Conditionally insert" gives you access to an expression editor (this is the same expression editor for conditionally displaying pages, conditionally showing questions, etc).
What is unique about creating conditional logic on Word template variables is that you can define what's known as an "else statement": an alternate definition for the variable to be used if the conditional logic expression is not satisfied.
Typically, you will leave the else statement empty, which amounts to telling your app "insert this variable if the following conditional expression is true, else insert nothing."
This 10 minute video demonstrates how to add conditional logic to variables:
What to do with your document
You must determine what to do with the document your template produces, i.e. the final document is not automatically given to the end-user.
Your options are:
- Give the document to the end-user to download, either as a .docx file or as a PDF
- Email the document
- Send the document to your Afterpattern database
You can use any combination of the above options.
Give document to end-user to download
To give the document to the end-user to download, you must attach it to a page block. To do this, on your page block, click the "Document" button and select your template:
If the Word template was created via our Microsoft Word add-in, when you attach it to a page block you can choose to convert it to a PDF.
Email the document
To email the document, either to the end-user and/or a 3rd party, you must first create an email template and then fire that email template from a page block (learn how). Once you've done that, you can include your Word template as an attachment to the email:
Send the document to your Afterpattern database
To send the document to your Afterpattern database, you must first build a database schema with a file-type column. Once you've done that, use a send data function to send your document to that file-type column.