Welcome to Islandnet.com  
Locally owned and operated since 1993  
Live Help
LOG IN:  Username:    Password:    

Templates

A template file is an ordinary text file that may or may not contain special placeholders. Web gadgets such as Mailit can use template files to customize a form response, format an email message, or customize a data file format.

A web gadget will read in a template file, and replace any placeholders it finds with real values, extracted from form input variables. The simplest template file contains no placeholders at all, in which case the output that is generated is an exact copy of the template file.

Placeholders are anything that is enclosed in a pair of curly braces. For example, if you placed {name} in a template file, then it would be replaced by the value of the "name" form variable. If there is no such variable then the placeholder is ignored.

Here's a sample auto-responder (refer to the Mailit gadget) template file that uses values extracted from the form to make a form letter more personalized. It assumes you have asked the user to enter their name and select a topic of intrest.

Dear {firstname},

Thank you for filling out our form,
we will be contacting you shortly with
additional information on {topic}.

Sincerely,

J. Smith
In addition to specifying form variable names, you can reference any HTML environment variables by adding a $ to the variable name. For example, {$REMOTE_ADDR} will be replaced by the host name or IP address of the user's computer.


Value Justification

Since you normally do not know the length of a variable's value, you may have a little trouble getting things to line up nicely. Consider the example where you are using a template file to write form data into a database file, and you want each field to line up nicely in it's own column. You might start by trying a template file like this:

{lastname} {firstname} {address} {phone}
But the results would end up looking like this:

Smith John 123 Anywhere Street 555-1234
Doe Jane 100 My Street 555-9987
By adding a simple formatting control to each placeholder, you can get results that look like this instead:

Smith     John      123 Anywhere Street      555-1234
Doe       Jane      100 My Street            555-9987
The template file to produce this output would look like this:

{<10 lastname} {<10 firstname} {<25 address} {phone}
Notice how a special formatting control is inserted after the opening brace and before the variable name itself. Also note that a space is required after the control and before the variable name. Here is a list of the various formatting controls:

Left Justify: {<n varname}
This formatting control will left-justify the value within a specified width of spaces.

Right Justify: {>n varname}
This formatting control will right-justify the value within a specified width of spaces.

Center: {-n varname}
This formatting control will center the value within a specified width of space.

Floating Point: {.w,d varname}
This control treats the value of varname as a floating point number and displays it to 'd' decimal places in an overall field width of 'w'. For example, if the variable X equals 1.234 and you use the control {.5,2 X} it would display 1.23


Conditional Clauses

Occasionally it is useful to be able to include or ignore sections of a template file based on the value of a form variable. Consider the example of a mailing list owner who wants to allow people to both subscribe and unsubscribe from her mailing list via a form. The mailing list requires that the body of the message contains this line to subscribe:

subscribe listname firstname lastname
And this line for unsubscribing:

signoff listname email
This is trivial using the following template file:
{? command = subscribe}
subscribe listname {firstname} {lastname}
{!}
signoff listname {email}
{}
This example uses three special "placeholders" to implement conditional inclusion. The {? command=subscribe} tag performs a test to see if the value of the "command" variable equals "subscribe". If the test is true, then everything up to the {!} tag is included, and everthing after the {!} and up to the {} is ignored. If the test is false, then everything up to the {!} is ignored and averything up to the {} is included.

So a {? ...} tag means "If this is true do this". The {!} means "else do this". The {} tag marks the end of the conditional clause.

There are various tests you can perform on a variable:

=Equal
!=Not Equal
<Less than (numbers only)
<=Less than or equal to (numbers only)
>Greater than (numbers only)
>=Greater than or equal to (numbers only)
<<Less than (text only)
<<=Less than or equal to (text only)
>>Greater than (text only)
>>=Greater than or equal to (text only)

You may place the conditional clause controls at any point within a template file, they do not need to be on separate lines as shown above. You may not nest one conditional clause within another, but you may have as many clauses as you like.


Math Calculations

A very powerful feature of template files is the ability to perform math calculations. This can be very useful for calculating taxes, mortgage rates, currency exchange rates, and so on without the need for a customized gadget.

There are two variations on this feature. The first simply performs a calculation and outputs the result. The second performs the calculation, but stores the value into a new variable instead of outputting it. This allows you to reference the new variable in subsequent calculations.

For example, assuming that we have asked the user to enter a value for "X" and a value for "Y", and the user entered "10" and "6", this template file:

X + Y = {= X+Y}
X * Y = {= X*Y}
Would produce this output:

X + Y = 16.00
X * Y = 60.00
And

{= Z=X+Y}
Z = {= Z*10}
Would set the variable Z to 16 and produce this output:

Z = 160.00
Note that any placeholder that starts with {= will be evaluated as a math expression. You may reference any form variables that have numeric content (non-numeric variables are qual to 0.0). All the standard math operators are supported:

+, -, *, /, % (mod), and ^ (to the power of)
An expression may also reference any of the following mathematical functions:

sin( n )Sine of n
cos( n )Cosine of n
tan( n )Tangent of n
asin( n )Arc sine of n
acos( n )Arc cosine of n
atan( n )Arc tangent of n
sinh( n )Hyperbolic sine of n
cosh( n )Hyperbolic cosine of n
tanh( n )Hyperbolic tangent of n
log( n )Log (base 2) of n
log10( n )Log (base 10) of n
sqrt( n )Square root of n
floor( n )Nearest integral value less than or equal to n
ceil( n )Nearest integral value greater than or equal to n
abs( n )Absolute value of n
hypot( a, b )Hypotenuse: sqrt( a * a + b * b)
deg( n )Converts n radians to degrees
rad( n )Converts n degrees to radians
log10( n )Log (base 10) of n

Note that all trigonometric functions assume radians.

Another example: Assuming you have asked the user to enter a value for "A" and "B", here are various ways to calculate the hypotenuse of a right triangle:

{= hypot( a, b )}
{= sqrt( a * a + b * b )}
{= sqrt( a^2 + b^2 )}


Page generation time: 0.02 seconds