[edit] Storing and retrieving data from variables
Variables are simply place holders to hold a variety of data. You can store data into variables you create and then retreive the content of the variable later. For the purposes of survey creation, you will most likely NOT be creating variables, but just retrieving data from a set of pre-set variables we made available to you.
Let's start by taking a look at variable creation and storing data into them:
[% apples = 5; oranges = 10; question = 'How many apples and oranges are there?'; %]
In the example above, 3 variables were created: apples, oranges and question. apples and oranges were both set to store numbers, while the question variable was set to store a string. So far, nothing was printed out, so when this TT code is evaluated, it will just be an empty string. Let's add more to the code above:
[% apples = 5; oranges = 10; question = "How many apples and oranges are there?\n"; question; 'There are ' _ apples _ ' apples and '; oranges _ 'oranges.'; %]
The 3 new lines above prints out the following results:
How many apples and oranges are there? There are 5 apples and 10 oranges.
The first new line prints out the contents of the variable question. Then the 2nd and 3rd lines print the string "There are " followed by the contents of the variable apples, then the string " apples and ", and so on. The underscore character _ is known as a concatenator, which basically glues strings together into one giant string. As you may have already figured out, when you simply list the name of an existing variable such as question and apples, TT will print the contents of the variable.
[edit] Using variable interpolation
Strings are identified by looking for text surrounded by single quotes ' or double quotes " in TT, and there is a big difference between the two. If you look at the string stored in question, you will notice that there is a \n at the end of the string. This is a newline symbol which indicates that there will be a line break there. That is why there is a line break immediately after the question in the output. Without the newline character, the output would have been the following:
How many apples and oranges are there? There are 5 apples and 10 oranges.
Here is a list of other special characters you may want to use in TT:
\b - backspace character \e - escape character \n - new line character \r - return character (we recommend \n instead) \s - space character \t - tab character \v - vertical tab character
The special characters such as \n will only work in strings that are defined by double quotes. If you were to put \n in single quote strings, it will be displayed exactly as how it is. So if we modified the previous code as such:
[% apples = 5; oranges = 10; question = 'How many apples and oranges are there?\n'; question; 'There are ' _ apples _ ' apples and '; oranges _ 'oranges.'; %]
Now that the variable question has been defined with single quotes, so the content of the string is stored as is and not interpolated. Only double quoted strings are interpolated, meaning the content of strings are interpreted and parsed. We will get into string interpolation soon, but the example above will print out the following:
How many apples and oranges are there?\nThere are 5 apples and 10 oranges.
With double quote strings, you can utilize interpolation to embed variables into strings directly, instead of joining strings and variables together like in the previous examples. The example below will be using interpolation:
[% apples = 5; oranges = 10; question = "How many apples and oranges are there?\n"; question; "There are ${apples} apples and ${oranges} oranges."; %]
The example will print out the same output as the previous example:
How many apples and oranges are there? There are 5 apples and 10 oranges.
Within double quote strings, to embed variables, use a dollar sign $ in front of the variable name with braces {} surrounding the name. Although technically you do not need the braces (you can just use $apples), it is a good habit to develop now. This is because later on, to embed hashes or arrays (which are more complex variables), you will need to use the braces.
[edit] Variable naming restrictions
In terms of naming variables in TT, you are restricted to using alphabet letters, numbers and the underscore character _. TT also requires you to start the variable name with an alphabet letter, so 1abc would not be a valid name. It will actually cause an error during evaluation process of the TT code. TT also will not accept TT reserved variables such as template and component (since these variables are predefined by TT). Directives and control structure keywords such as PROCESS and IF (all uppercases) are not allow as variables either. For more information regarding TT variable naming restrictions, Click Here.