|
 |
|
NOTE: This Design document is subject to change at any time. |
Template
Syntax: <TEMPLATE property-list>template-definition</THREAD>
Templates are a powerful mechanism for generating text based upon a standard template. This feature can be used for "mail merge" applications, or whenever you want to "fill in" values in a text string from variables in your script. This is similar to the Smarty system found in scripting languages such as PHP, but it more suited to the simpler syntax of zApp.
In it's simplest form, a template has a Template property that provides the definition of the template, and a Text property that has the output text with any references replaced with their values.
"References" can include any of the following:
- #field#
- This is replaced with the value of the current database field with the give name. Or, if this field has been added to the template using the Add method, the value of that field is returned.
- #context.field#
- Same as above, but references the "field" variable within a specific "context". A Context is a separate set of variable data. So you can have multiple fields with the same name, but in different contexts. You can also use the context to reference a different data source or query other than the default query assigned to the zTemplate
- {=expression}
- evaluates the expression in the language set for the zTemplate and returns the value as text. Any #field# references in the expression are replaced before the expression is evaluated.
- {$if expression}
- tests the expression and ignores the following text unless the expression is true.
- {$else}
- used in conjunction with the {$if} command to include the following text if the original expression was false.
- {$elseif expression}
- if the previous expression was false, this tests a new expression and includes the following text if the new expression is true.
- {$end}
- concludes an {$if} command (or any of the following commands such as {$case}, {$begin}, etc
- {$case expression}
- evaluates the given expression and saves the result for testing with the following {$is} commands
- {$is value}
- includes the following text if the value saved from the {$case} command is equal to the value. An {$else} clause can also be included to match any other value not specified. {$end} terminates the {$case} statement.
- {$begin context}
- begin a looping section. The following text (up to the {$end}) is included multiple times for each "section" that was added to the specified context using the BeginSection method.
- {$n}
- inserts a newline into the text
Here are some examples:
Code: |
set Template = core.CreateObject("template")
Template.Add "name", "Zugg"
Template.Template = "Hello #name#, this is zApp"
msgbox Template.Text |
The above example would display the text "Hello Zugg, this is zApp". If you then changed the "name" field with the script:
Template.Add "name", "Darker"
then the Template.Text property would return "Hello Darker, this is zApp". Note that all you had to do was change the value of the "name" variable. The Template property stays the same and just a new Text value is returned.
Additional examples are shown on the left. |
|