Description |
The SUBST function is a universal function. It is multiple overloaded that means it can be called with quite different parameters. Here the simplest form:
SUBST(target,string) replaces the first appearance of target by string
Example: We have a template in which we want insert the actual date and time (of server). There to in the template the following text is written:
A Template for the tdbengine
Date: #date# Time: #time#
That is an HTML site which was edited by the tdbengine
(the welt of targets with '#' chars has exposed as quite advantageous because in this way the danger of confusion is very small)
The template is stored at "templates/my_site.html". The following program solves our problem:
PROCEDURE Main
cgiwriteln('content-type: text/html')
cgiwriteln('')
IF LoadTemplate('templates/my_site.html')=0 THEN
subst('#date#',datestr(today))
subst('#time#',timestr(now,0))
CGIWriteTemplate
ELSE
cgiwriteln('template not found')
END
ENDPROC
If the inserted string starts with »extern:«, this string is not inserted but the rest of the string is interpreted as path of a textfile. The content of this replaces then the target. Corresponding a string that starts with »ramtext:« refers to an internal text file.
Just if the target shall be replaced by whole text files the kind of substitution is important: What happens with word wraps? In which character set shall the substitution executed? That can specified with another parameter, the »mode«.
SUBST(target,string,modus)
Replaces the target ...
Modus
0 (default) -> Substitution without any further editing
1 -> All chars are converted to HTML
2 -> All chars are converted to ANSI
4 -> Hard word wraps are replaced by » «
16 -> The external text is there in ASCII format (instead of ANSI)
32 -> Only the BODY of an external HTML site is read
These modes can be added to reach the wished aim. The mode 5 (=1+4) means for example that the chars are converted to HTML and hard word wraps are replaced by » «.
Instead of raplacement string there can also be a combination of table handle and table field. Then the content of the specified field is inserted.
Example: The following code replaces all fields of the database in the template:
VAR i, db : INTEGER;
...
db:=OpenDB(...)
nloop(i,maxlabel(db)-1,subst('#'+label(db,i+1)+'#',db,i+1,5))
|