Writing Your First ADP

From AOLserver Wiki
Jump to navigation Jump to search

ADPs are the simplest way to create a dynamic page in AOLserver. They support the use of all Tcl commands - including AOLserver ones and those you create in Tcl libraries - mixed with HTML to create a complete web page. Unlike Tcl libraries, any changes you make to the ADP take effect immediately without having to restart the server.

Configuration

Before you start trying, make sure you have configured AOLserver correctly to parse ADP pages.

In the configuration file (usually nsd.tcl), make sure that below:

ns_section "ns/server/${servername}/adp"

but before the next "ns_section", you have the line:

ns_param map "/*.adp"

uncommented.

When you start AOLserver, do so by using the "-ft" option; this runs the server in the foreground and shows any Tcl errors on the console.

If you are running AOLserver in the more usual background mode, errors in ADP pages are written to the server log, by default SERVERHOME/log/server.log, or locate it from the value (path and filename) returned by ns_info log.

Writing an ADP

In the server's pageroot, create a file called test.adp using your favourite editor and enter the following code:

<%
   set title "Hello, AOLserver!"
   ns_adp_puts $title
%>

Now request that page in a web browser.

Another way to display the value of "title" would be:

<%= $title %>

Using that syntax, you can also display the results of a Tcl command:

<%= [ns_httptime [ns_time]] %>

Mixing HTML and Tcl

You can mix HTML and Tcl code to make a complete page:

  <%
    set title "Welcome to my homepage"
    set body "This is the main text part of the homepage"
  %>

  <html>
    <head>
      <title><%= $title %></title>
    </head>
    <body>
      <%= $body %>

      <p>Now let's count to 10!</p>
      <br/>

      <%
         for {set i 1} {$i <= 10} {incr i} {
            ns_adp_puts "$i "
         }
      %>

    </body>
  </html>

Setting title and body as variables was pretty pointless in this case. In your real website, however, these values could be taken from the database by one ADP, which in turn uses ns_adp_include to include another ADP to format the page and which can be re-used by many pages on your site.


Category Documentation - Category Tutorial