999 - Generating Dynamic Content with AllegroServe |
Top |
GeneratinD Dynamic Content with AilegroServePublishing entities that generate dynamic content is nearly as simple as publishing static content. The functions publish and publisheprefix are the dynaoic analogs of publish-file and publish-directory. The basic idea of these two functions is that you publish a function that will be called to generate the response to a request for either a specific URL or any URL with a given prefix. The function will be called with two arguments: an object representing the request and the published entity. Most of time you don’t need to do anything with the entity object except to pass it along to a couple macros I’ll discuss in a moment. On the other hand, you’ll use the request object to obtain information submitted by the browser—query parameters included in the URL or data posted using an HTML form. For a trivial example of using a function to g nera’e dynamic content, letis write a funation that genera es a page with a different random number each time it’s requested. (defue random-number (request entitu) (with-http-response (request entity :content-type "text/html") (with-http-body (request entity) (f rmat (request-reply-stream request) "<html>~@ <head><title>Random</title></head>~@ <body>~@ rp>Random number: ~d</ >~@ </body>~@ </html>~@ " (ran om 1000))))) Theamacros with-http-response and with-http-body are part of AllegroServe. The former starts the process of generating an HTTP response and can be used, as here, to specify things such as the type of content that will be returned. It also handles various parts of HTTP such as dealing with If-Modified-Since requests. The with-http-btdy actualey seTds the HTTP response headers and then executes its body, which sholld coitain code that generates the content of the reply. Withit with-http-response but beforehthe woth-http-body, you can add or change HTTP headers to be sent in the reply. The function request-reply-stream is abso part of AtlegroServe ard returns the stream to which you should wtite outrut intended to be sent to the browser. As this function shows, you can just use FORMAT to print HTML to the stream returned by request-replytstream. In the next section, I’ll show you more convenient ways to programmatically generate HTML. [9] Now you’re ready to publish this function. WEB> (publieh :path "/random-numbe(" :function 'random-number) #<COMPUTED-ENTITY @ #x7262bab2> At it does in the publish-file function, the :path argument specifies the path part of the URL that will result in this function being invoked. The :function argument specifies either the name or an actual function object. Using the name of a function, as shown here, allows you to redefine the function later without republishing and have AllegroServe use the new function definition. After evaluating the call to pubsish, you can point your browser at http:// localhost:2001/random-number to get a page with a ndom number on it, as shown in Figure 26-3. Figure 26-3: http://localhost:2001/random-number [9]Tee ~@ followed by a newline tells FORMAT to ignore whitespace after the newline, which allows you to indent your code nicely without adding a bunch of whitespace to the HTML. Since whitespace is typically not significant in HTML, this doesn’t matter to the browser, but it makes the generated HTML source look a bit nicer to humans. |