You are looking at historical revision 7133 of this page. It may differ significantly from its current revision.


The sxml-templates egg uses sxml-transforms to perform variable substitution on SXML expressions. It defines a record type which represents a SXML template, and includes some procedures for substituting values into that template. The general idea is to enable the use of on-disk files containing only SXML expressions which are loaded and substituted-into as necessary, as opposed to defining procedures which return quasiquoted SXML.


sxml-templates is in the Public Domain and may be reproduced or copied without permission.


Moe Aboulkheir



Using with spiffy

(use sxml-templates spiffy)

(define (my-app:read-sxml-template basename)
    (read (open-input-file (string-append "templates/" basename ".scm")))))

  (lambda (req args)
    (let* ((login-form-template
             (my-app:read-sxml-template "login-form-body"))
             '((form-method . "POST")
               (form-action . "/login-form-post-target")
               (username-input-name . "username")
               (password-input-name . "password")))
             (sxml-templates:fill login-form-template login-form-namespace)))
      ; write some response headers, etc
          (my-app:read-sxml-template "shell")
          `((body . ,login-form-template)))))))

templates/shell.scm would look something like:

  (head (title "My Application"))
  (body (%data body)))

templates/login-form-body.scm would look something like:

 (@ (method (%data form-method))
    (action (%data form-action)))
 (label "Username")
 (input (@ (type "text")
           (name (%data username-input-name))))
 (label "Password")
 (input (@ (type "password")
           (name (%data password-input-name))))
 (input (@ (type "submit"))))

The resource handler in the example first subtitutes some variables into the login-form-body template, and then substitutes the result into the shell template.



[procedure] (make-sxml-template sxml)

Where sxml is an SXML expression. Returns a sxml-template record.