![]() | Welcome to Islandnet.com Locally owned and operated since 1993 |
| Create an Account • Pricing & Features • Domain Registration • Customer Sites • Help & Info | |
Friday May 16 2008 | |
TemplatesA 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 JustificationSince 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-9987By 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-9987The 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:
Conditional ClausesOccasionally 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 lastnameAnd this line for unsubscribing:
signoff listname emailThis 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:
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 CalculationsA 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.00And
{= Z=X+Y}
Z = {= Z*10}
Would set the variable Z to 16 and produce this output:
Z = 160.00Note 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:
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 )}
|
| Home • About Us • Contact Us • Terms of Use • Privacy Policy • Help Documents |
| Page generation time: 0.02 seconds |