Back to all How-tos

Load a snippet based on a data field value

In this How-To, we will personalize a letter for each customer using snippets that contain text in the customer’s language.

We will set the letter’s text dynamically based on a value stored in the data field “language”. In Connect, we use the snippet resource for the letter text in different language. We suppose that we have these customer’s language in the record: english (en), french() or dutch(nl). So, we create a snippet that contains the letter text in each language. The contents of these snippets can be retrieved with a script.

Quick steps

  1. Drag the three letter text html files to the Snippets folder located in the Resources Pane
  2. View the snippet content by double clicking on it
  3. In the template section, add a placeholder text in the paragraph, e.g. [letter text] and set the alignement of this text to justify and its padding to 20px
  4. Type the paragraph ID in the Attributes Pane, e.g. contents
  5. Right click on this paragraph and select New script…
  6. Open the script in Scripts pane. We see that the paragraph’s ID is writen in Selector
  7. Type in a name for the script such as Language Snippet and add in the following script contents:
    // Assign the data field language value to a variable e.g. lang
    var lang = record.fields.language;
    // Load a snippet based on variable lang value
    results.loadhtml('Snippets/Letter-'+lang+'.html');
    
  8. Click OK to save the script.
  9. Switch to Preview mode to view the result

Notes: if in the letter text we have for example the placeholder @firstname@ that value is given by the text script on data field firstname, we have either to:

  • Order the scripts in script pane. The @firstname@ text script must be after the script of the load snippet
  • Or, in the script load snippet, write this line:
    results.loadhtml('Snippets/Letter-'+lang+'.html').find('@firstname@').text(record.fields.firstname);

API features used in the script

Leave a Reply

Your email address will not be published. Required fields are marked *