This article explains what the DataSources[] function is and provide examples of how it can be used with the binding syntax in smart templates.
Prerequisites
|
What is the DataSources[] function?
DataSources[] is a function that returns the values from a specific data source row.
DataSources[] logic
Syntax | {{DataSources.DataSourceName[NameValueToLookUp].DataSourceColumnName}} |
---|
NoteThe DataSources[] function is used for (local) data sources while the ExternalDataSources[] function is used to capture data from an external data source row. |
DataSources[] function examples
In the examples below these data sources are used. The column Country of the Offices data source is a reference to the column Name of the Countries data source:
ImportantPlease note that creating data sources requires owner or admin rights to the tenant. |
Example 1
Using the DataSources[] function with plain text to retrieve the postal code of the Eindhoven office.
Binding | {{DataSources.Offices["Office Eindhoven"].PostalCode}} |
---|---|
Input | n/a |
Output | 5611 ZB |
Important
|
Example 2
Using the DataSources[] function with the "OfficeName" flexible dropdown question originating from the response form to retrieve the correct postal code for the office.
Binding | {{DataSources.Offices[Form.OfficeName].PostalCode}} | |
---|---|---|
Input | Office Copenhagen | Office Eindhoven |
Output | 1403 | 5611 ZB |
Example 3
Using the DataSources[] function with the "OfficeName.Name" dropdown question originating from the response form to retrieve the correct postal code for the office.
Binding | {{DataSources.Offices[Form.OfficeName.Name].PostalCode}} | |
---|---|---|
Input | Office New York | Office Copenhagen |
Output | 10007 | 1403 |
ImportantPlease note, in the binding you need to use {{Form.OfficeName.Name}}, because it's a dropdown field. |
Example 4
Using the DataSources[] function with the "OfficeName" flexible dropdown question originating from the response form to retrieve the country of the office.
Binding | {{DataSources.Offices[Form.OfficeName].Country.Name}} | |
---|---|---|
Input | Office Berlin | Office Sydney |
Output | Germany | Australia |
ImportantPlease note that the Country is stored in the Name column of the Countries data source and is used as a reference in the Offices data source. |
Example 5
Using the DataSources[] function with the "OfficeName" flexible dropdown question originating from the response form to retrieve multiple fields from the data source row.
Binding | Location Templafy {{DataSources.Offices[Form.OfficeName].City}}: {{DataSources.Offices[Form.OfficeName].Address}}, {{DataSources.Offices[Form.OfficeName].PostalCode}}, {{DataSources.Offices[Form.OfficeName].City}}, {{DataSources.Offices[Form.OfficeName].Country.Name}} | |
---|---|---|
Input | Office Sydney | Office Berlin |
Output | Location Templafy Sydney: 151 Clarence Street, NSW 2000, Sydney, Australia | Location Templafy Berlin: Lützowstr. 100, 10785, Berlin, Germany |
ImportantPlease note, for all examples explained, if no value is resolved then "" is returned. |
Example 6
Using the DataSources[] function to insert an image stored in a data source.
Binding | {{DataSources.DataSourceName["ImageName"].DataSourceColumnName}} |
---|
Example 7
If you want your email signature or Word document to contain a 'tip of the day' (showing a different tip every day) then create a data source containing all 366 possible dates (format 'month/day') and texts like this:
Insert a tip based on today's date:
Binding | {{DataSources.TipOfTheDay[FormatDateTime(Now(),"MM-dd","en-US")].Tip}} | |
---|---|---|
Input | Today's date = "January 2" | Today's date = "December 31" |
Output | "To increase the font size, highlight the text and then press Ctrl+]." | "To insert a non-breaking hyphen press Ctrl+Shift+_." |
Insert a tip based on the value of the "Date" question originating from the response form:
Binding | {{DataSources.TipOfTheDay[Form.Date,"MM-dd","en-US")].Tip}} | |
---|---|---|
Input | Date = "January 2" | Date = "December 31" |
Output | "To increase the font size, highlight the text and then press Ctrl+]." | "To insert a non-breaking hyphen press Ctrl+Shift+_." |
Example 8
Using the DataSources[] function with a HostSystem binding.
This example shows how the DataSources[] function can be used to add a specific image to a document, while the external data originating from the App connector or API request only contains text.
In the example this JSON file is used:
{
"email": "john@mycompany.com",
"data": {
"Language": "en-us",
"OrderId": 12345,
"Product": "Apple"
}
}
The product can be any fruit, like 'Apple', 'Banana', 'Orange' etcetera.
The template currently only contains fields to display the Order ID and Product. Both fields are configured like this:
To add a field showing an image of the selected fruit in the document, follow the steps below:
- In the Admin Center, create a Products data source.
- Add a field of Type 'Image' and the name 'Image'. The result looks like this:
- In the Products data source add the value 'Apple' to the Name column and a picture of an apple to the Image column. Do the same for the other types of fruit. The result looks like this:
Important
- Values in the Name column must match the names used in the external data source.
- Values are case sensitive.
- In the template position the cursor below the Product field.
- In Template Designer click + Add smart field.
- Click Custom image binding.
- In the Display name field enter a name, for example 'Product image'.
- In the Image field use the binding
{{DataSources.Products[HostSystem.Product].Image}}
. - Optional: set the Height to '2 cm'. The result looks like this:
- Save the template and upload it to the Admin Center.
- When a user creates a document using the Templafy API, the document now also shows an image of the selected fruit:
Comments
Article is closed for comments.