[plt-scheme] Parameters and servlets

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Thu Feb 16 09:47:31 EST 2006

Jay McCarthy wrote:
> This is an example of the failure of parameters in the web server.
> 
> Your command interaction is not what the web-server does, what the
> web-server does is:
> 
> (define start #f)
>   (thread-wait
>    (thread
>     (lambda ()
>       (set! start (dynamic-require "jas01.ss" 'start)))))
>   (thread-wait
>    (thread
>     (lambda ()
>       (printf "~S~n" (start 'foo)))))
> 
> Which gives the same error.
> 
> ``A parameter is a setting that is both thread-specific and
> continuation-specific, such as the current output port or the current
> directory for resolving relative file paths.''
> 
> Thus, if a parameter is set in one thread, then another thread that is
> not a child of that thread will not have the new setting.
> 
> What are you actually trying to do? Obviously this example is
> contrived. I have a system called web-cells that solve the problems I
> was having with what I thought I wanted parameters to do.

Thanks for the explanation. Your web-cells might do the trick.

I am trying to make a small library that uses site-wide defaults.
E.g in "view.scm" I'd like to write

   (require "html.scm")

   ;;;
   ;;; SITE WIDE DEFAULTS
   ;;;

   (current-page-title       "Foo 2.0")
   (current-page-header      '(h1 ((class "page_header")) "Foo 2.0!"))
   (current-page-style-sheet "http://localhost/stylesheet.css")

   (define (html-a-page)
     (html-page #:body  "Foo bar baz"))

where html-page is defined "html.scm".


At the same time, I want the ability to override the defaults
for a single request in "control.scm" with, say,

   (parameterize ([current-page-title "Foo 3.0"])
     (html-a-page))

Apart from the losing the settings when a new thread is started,
it works nicely.


Here is html.scm:

(module html mzscheme
   (provide (all-defined))

   (require (lib "kw.ss"))

   (define current-page-title        (make-parameter "A title"))
   (define current-page-header       (make-parameter '(h1 "A Header")))
   (define current-page-body         (make-parameter "A body"))
   (define current-page-content-type
      (make-parameter "text/html;charset=UTF-8"))
   (define current-page-style-sheet  (make-parameter ""))

   ; html-page
   ;   Puts all the pieces together to make a complete html page.
   ;   Defaults are taken from site-wide parameters
   (define/kw
     (html-page #:key
                (title         (current-page-title))
                (title-atts    #f)
                (header        (current-page-header))
                (head-atts     #f)
                (body          (current-page-body))
                (body-atts     #f)
                (style-sheet   (current-page-style-sheet))
                (content-type  (current-page-content-type)))
     (let ([title-atts (if title-atts (list title-atts) '())]
           [head-atts  (if head-atts  (list head-atts)  '())]
           [body-atts  (if body-atts  (list body-atts)  '())])
       `(html (head  (title , at title-atts ,title)
                     ; external stylesheet
                     (link ((rel "stylesheet")
                            (type "text/css")
                            (href ,style-sheet)))
                     (meta ((http-equiv "Content-Type")
                            (content ,content-type)))
                     , at head-atts)
              (body , at body-atts
                    ,header
                    ,body))))

-- 
Jens Axel Søgaard




Posted on the users mailing list.