Data and Text: Combining Text
Description
LiveCode's uniquely powerful text handling capabilities comes in handy for a wide range of applications. Common tasks include:
- Mail merging a form email
- Creating a leaflet for print
- Building an invoice
In this tutorial, you'll learn how to:
- Work with words and lines,
- combine texts together,
- make decisions
- and insert variables into your output.
Tutorial
Data and Text: Combining Text
LiveCode's uniquely powerful text handling capabilities comes in handy for a wide range of applications. Common tasks include:
* Mail merging a form email
* Creating a leaflet for print
* Building an invoice
In this tutorial, you'll learn how to:
* Work with words and lines,
* combine texts together,
* make decisions
* and insert variables into your output.
Set up your Fields
in this example, I have a template email, which I've placed in a field called template (1), a record, which I've placed in a field called record (2), and an output field, which I've called output (3). I also have a button called "Create email" (4). To follow along, you can either recreate this yourself by dragging out fields from the Tools Palette and naming them using the Property Inspector, or you can download the associated stack for this example, here.
In future videos I'll show you how to download records directly from a database or a web service, and how to send the output through an email, rather than just displaying it on the screen.
Addressing by Name
To start our email with Dear Mr Miller, we need to add some code to the button. Select the "create email" button and click on the Code icon in the toolbar to bring up the Code Editor. Enter this code:
on mouseUp
put field "template" into field "output"
put field "record" into theRecord
put line 2 of theRecord && word -1 of line 1 of theRecord & comma into theName
put theName after line 1 of field "output"
end mouseUp
The first line of this code is putting the contents of the template field (our email text) into the output field. Next, we are putting the contents of the record field into a variable we're calling theRecord - this makes it easier to work with. Next, we're taking the whole of line 2 of our record (Mr), plus the last word of line 1 of our record (Miller), adding a comma, and putting all of this into another variable we're calling theName. A single ampersand (&) tells LiveCode to combine text together and a double ampersand (&&) tells it insert a space when it does so. Finally, we put theName into the output field, after line 1.
Click Apply, and take a look at what this does by switching into Run mode on the Tools palette:
So far so good.
Decision Time
The next thing I want to do is to decide whether I want section (1) or section (2) of my email to display. Do we want to say "We noticed you watched X videos so far" or do we want to say "We notice you haven't watched any videos so far"?
Check the Record
The number of videos watched is the fifth line of my record. Lets add some more code to that button:
on mouseUp
put field "template" into field "output"
put field "record" into theRecord
put line 2 of theRecord && word -1 of line 1 of theRecord & comma into theName
put theName after line 1 of field "output"
if line 5 of theRecord > 0 then
delete line 9 to 12 of field "output"
put line 5 of theRecord into word 5 of line 5 of field "output"
else
delete line 5 to 8 of field "output"
end if
end mouseUp
If line 5 of theRecord is greater than zero we know that the customer has watched some videos, so we delete lines 9 to 12 of the email, which say "We notice you haven't watched any videos so far". Instead, we put line 5 of the record, the number of videos watched, into word 5 of line 5 of our email, replacing the placeholder "X" in "We noticed you watched X videos so far".
If line 5 of theRecord turns out not to be greater than zero, we delete line 5 to 8 of the output email.
Adding Tracking and an Email address
Now we want to add the customer ID to the end of the tracking code. put line 4 of theRecord after line 9 of field "output"
In this case our record contains a single word but if you had a tracking code that contained spaces or other characters that aren't URL friendly, you could write put URLEncode(line 4 of the record) after line 9 of field "output" instead.
Finally, we insert the email address into the footer.
replace "[email address]" with line 3 of theRecord in field "output"
Click apply, switch to Run mode, and you should see that the tracking code and email address are added to the output. To get the alternative version of this email, change the last line of the record field to 0 (zero) and you will see that you are now requesting feedback on why the customer has not watched any videos.