36. Template handling

[<<<] [>>>]

CGI programs should output HTML text. Embedding this text into the code is a bad practice. In our practice we usually use HTML templates that the program reads, modifies, inserts values and outputs. The CGI module support the usage of such templates.

A template is an HTML text with parameters. The parameters are placed in HTML comments, therefore you can easily edit these templates with the HTML editor you like. Each comment may contain a symbol name. The program should specify the actual value for the symbol and the module reads the template with the actual values replacing the comments. For example the template:

This is a template text with a

<!--alma-->
symbol.

will be presented as

This is a template text with a defined symbol.
assuming that the actual value of the symbol alma is the string "defined". If the value of the symbol is not defined by the program the comment is replaced by an empty string.

To handle symbols, and templates there are several functions in ScriptBasic. You can define a symbol calling the function cgi::SymbolName. To define the symbol alma you have to write:

cgi::SymbolName "alma" , "defined"

You can also tell the module that the actual string of the symbol can be found in a file, saying:

cgi::SymbolFile "symbol","file name"

To get the template file already with resolved symbol values you should say:

HtmlTemplate$ = cgi::GetHtmlTemplate("filename")

or if you want to hard wire the template text into the code:

HtmlTemplate$ = cgi::ResolveHtml("template text to be resolved")

When you are finished sending a resolved template to the client you may want to define other symbols, but before doing that it is safe to undefine the symbols used by the previous template. You can do that calling the function

cgi::ResetSymbols

Calling this function also releases the space occupied by the symbols and their values. For more information see the sample code.

Note that modern approach to this issue is to generate XML format output from the program and use XSLT transformation to create the desired XHTML output.


[<<<] [>>>]