|
Embedding Code Within an HTML File |
Top Previous Next |
As covered in Understanding the Scope of Variables, each HTML file has a special scope, and all code within the file resides within this scope.
To begin a block of Tibbo Basic code within an HTML file, you must use an escape sequence -- <? . To close the section of code, use the reverse escape sequence -- ?> .
When the embedded HTTP server receives a GET (or POST) request, it begins to output the requested HTML file. It simply reads the HTML file from top to bottom, and transmits its contents with no alteration. However, the moment is encounters a block of Tibbo Basic code, it begins executing it.
Tibbo Basic code inside HTML files does not differ from the code in "basic" files, but it may not contain procedures. This is because the Tibbo Basic code in the HTML file is considered to constitute a procedure in itself. Notice, that all code in one HTML file is considered to be a single procedure, even if there are several fragments of code in this HTML file. Consider this example:
<!DOCTYPE HTML public "-//W3C//DTD W3 HTML//EN"> <HTML> <BODY>
BEGINNING OF OUTPUT<br>
<? '<--------------- BASIC procedure starts here dim i as integer for i = 1 to 10 ?>
<i> Foo </i> <br>
<? next i '<--------------- procedure ends here ?>
<br>end OF OUTPUT<br>
</BODY> </HTML>
|
There two code fragments, yet they both form one procedure. For example, variable i declared in the first fragment is still visible in the second fragment.
The fact that entire code within each HTML file is considered to be a part of a single procedure has implications in the way events are handled (reminder: there is a single queue for all events). The next event waiting in the event queue won't be executed until the end of the HTML procedure is reached. Just because the HTML procedure consists of two or more fragments does not mean that other events will somehow be able to get executed while the HTTP server outputs the static data between those fragments ("<i> Foo </i> <br>" in our example). Use doevents if you want other event handlers to squeeze in!
Tibbo Basic code in the code fragments may include decision structures or loop structures that may cause various segments of HTML code to be output more than once, to be skipped altogether, or to be output only when certain conditions are true or false. In the above example the line "<i> Foo </i> <br>" will be output 10 times because this line resides between two code fragments that implement a cycle!
The same result could be achieved in a different manner:
<!DOCTYPE HTML public "-//W3C//DTD W3 HTML//EN"> <HTML> <BODY>
BEGINNING OF OUTPUT<br>
<? dim i as integer dim s as string
s="<i> Foo </i> <br>" for i = 1 to 10 while sock.txfree<len(s) 'these free lines can be omitted for simple doevents 'tests but are actually essential for wend 'reliable output of large data chunks
sock.setdata(s) 'this prepares data for output sock.send 'and this commits it for sending (see sock object) next i ?>
<br>end OF OUTPUT<br>
</BODY> </HTML> |
Here we have a single code block and "printing" the same line several times is achieved by using sock.setdata and sock.send methods of the sock object.
So, which of the two examples shows a better way of coding? Actually, both ways are correct and equally efficient. The first way will have an advantage in case you have large static blocks that may be harder to deal with when you need to print them using sock.setdata method.
Further information about creating HTML files with dynamic content can be found in Using HTTP (part of the sock object documentation).