Overview | Functions & Macros | Presentation Functions | Presentation Types

W3P Overview


  1. Introduction
  2. Presentation Type Specifiers
  3. Examples
  4. W3P and Dynamic Forms

Introduction

W3P is an abstract, extensible Common LISP system for manipulating input and output as CLOS objects, allowing simpler applications with less code duplication. To facilitate compatibility with existing LISP applications, W3P implements a subset of the LISP interface to the Common LISP Interface Manager. W3P, however, was conceived to specifically address the requirements of a stateless model of interaction such as the World Wide Web. Once a CLOS class has been profiled with its essential parameters and visual attributes, input type-checking and output rendering can be performed automatically. Developers can utilize standard interface characteristics or choose to define their own visual representations of objects and input parsing routines. W3P represents an effort to create a streamlined, highly portable, non-proprietary presentation system tailored to the stateless model of the World Wide Web.

A paper describing the system, W3P: A Portable Presentation System for the World Wide Web, was presented at the Dynamic Objects Workshop at Object World East, May 1996.

You can find relevant discussions on presentation types and examples with similar syntax in the Common Lisp Interface Manager 2.0 User's Guide at Xanalys.

Presentation Type Specifiers

There is a simple, precise syntax for specifying a presentation type. The W3P specifier syntax is compatible with that of CLIM. The following three forms are valid presentation type specifiers:

name

(name parameters...)

((name parameters...) options...)

Name is the name of a presentation type. Parameters are used to determine if an object satisfies the constraints of a presentation type. Options specify visual attributes or rendition preferences.

Examples

Including some basic W3P functionality in a CL-HTTP application should require minimal new code. Providing an abstract mechanism for parsing and rendering CLOS objects simplifies the implementation of user interfaces, helping to eliminate unnecessary code duplication. The following examples illustrate how W3P fits into the CL-HTTP model of computing HTML and responding to forms.

Computed URLs: Defining a presentation type for an application object allows consistent visual rendering, specialized over stream or view. This can be helpful when writing response functions for a url:http-computed-url. To be able to call w3p:present on an application object, it must have a presentation type and a present method. Note the use of a presentation type option to control visual rendering preferences. The two calls to w3p:present below show possible HTML rendering of the scientist object.

(defclass scientist ()
  ((name :initarg :name :reader scientist-name)
   (photo-url :initarg :photo-url :reader scientist-photo-url)))

(setq *frank-ernest* (make-instance 'scientist 
                       :name "Frank Ernest"
                       :photo-url "http://www.foo.net/photos/frank.gif"))

(w3p:define-presentation-type scientist ()
  :inherit-from t
  :options ((show-photo t)))

(w3p:define-presentation-method w3p:present (object (type scientist)  stream (view w3p:html-view) &key)
  (when show-photo
    (html:image (scientist-photo-url object) "scientist photo" :stream stream)
    (html:break-line :stream stream))
  (html:with-rendition (:bold :stream stream)
    (write-string (scientist-name object) stream)))


(w3p:present *frank-ernest* 'scientist :view w3p:+html-view+)

<IMG SRC="http://www.foo.net/photos/frank.gif" ALIGN="TOP" ALT="scientist photo">
<BR>
<B>Frank Ernest</B>

(w3p:present *frank-ernest* '((scientist) :show-photo nil) :view w3p:+html-view+)

<B>Frank Ernest</B>

HTTP Forms: W3P can be used to simplify parsing and error-handling in response functions for url:http-form URLs. CL-HTTP currently provides such a response function with an alist composed of (query-identifier . value-string) pairs. Calling w3p:accept-from-string on value-strings allows abstract, consistent parsing control. This simple example demonstrates the use of w3p:accept-from-string in conjunction with the built-in type integer, which has parameters low and high.

Note that w3p:accept and w3p:accept-from-string return two values, an object and a presentation type. When invalid input is supplied, w3p:input-not-of-required-type is thrown. See w3p:handle-input-error for information on defining specialized error handling. In order to parse a new type of object, the developer must define a presentation type, and presentation methods for w3p:presentation-typep and w3p:accept.

(w3p:accept-from-string 'integer "27" :view w3p:+textual-view+)

27
INTEGER

(w3p:accept-from-string '(integer 0 10) "5" :view w3p:+textual-view+)

5
(INTEGER 0 10)

(handler-case 
   (w3p:accept-from-string '(integer 0 10) "12" :view w3p:+textual-view+)
   (w3p:input-not-of-required-type () (error "Bad input")))

LISP ERROR: Bad input

Computed Forms: W3P can also be used to automatically generate a prompt for an object inside an HTML form. When w3p:accept is called with the keyword :present-p set to t, it generates a prompt rather than trying to parse a value from the stream. The generation of the prompt is handled by a w3p:accept-present-default method. This type of functionality is useful when writing the form-function for a url:http-computed-form URL. This example shows the HTML prompts generated for a built-in presentation type. Note that the block of HTML returned only makes sense when invoked inside an HTML form. See the CL-HTTP remote configuration application for an example of using this syntax in a loop to efficiently generate a form.

(w3p:accept '(integer 0 168)
            :present-p t
            :view w3p:+html-view+ 
            :default 50
            :prompt "Number of hours worked:"
            :prompt-mode :raw
            :display-default nil
            :query-identifier "HOURS-WORKED"
            :insert-default t
            :active-p t)

<B>Number of hours worked: </B>
<INPUT TYPE="text" NAME="HOURS-WORKED" VALUE="50" SIZE="3" MAXLENGTH=3>

W3P and Dynamic Forms

W3P may be used as a vehicle for incorporating Dynamic Form Processing from the Communications Linker System into the portable web server. This would add another layer of abstraction to the current forms substrate, facilitating the definition of query and form objects. The dynamic forms system would automatically invoke the proper calls to the presentation system.


Christopher R. Vincent
Christopher_Vincent@nospam.alum.mit.edu