90.48%
Search · Index

V.12 Tcl for Web Use

In most cases, rather than typing Tcl expressions at the interpreter you'll be using Tcl to generate dynamic Web pages. AOLserver provides two mechanisms for doing this:
  1. .tcl pages are Tcl programs that are executed by the webserver (in this case AOLServer). They typically generate character strings that are sent to the client browser with ns_write.
  2. .adp pages are like ordinary HTML pages, but they contain escapes to Tcl commands that are evaluated by the server.

Which option you choose depends largely on whether static HTML or Tcl constitutes the majority of the page you are creating. If you're printing fixed character strings with Tcl, you'll need to "quote" any embedded quotes by preceding them with backslash. E.g., to print to a Web page the string <a href="goodies.html"> you would use the Tcl command

ns_write "<a href=\"goodies.html\">"

 or simply

ns_write "<a href='goodies.html'>"

So if the majority of your Tcl expressions are simply printing strings, you'll find it more convenient to avoid the hassles of quoting by using .adp pages rather than complete .tcl programs.

Why the use of ns_write instead of puts? The puts command writes to a program's standard output stream. If you were writing a Tcl CGI script, standard output would be linked through the Web server to the Web client and you could indeed use puts. This idea doesn't make sense with AOLserver, where all the Tcl scripts are running inside the Web server process. If the Web server is serving 100 clients simultaneously, to which client should bytes written with puts be sent?

Anyway, if you want to have some fun one night, try redefining puts to forward its argument to ns_write and see what happens.