((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/sxml-templates" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "eggs" "web" "spiffy" "html" "http" "sxml") (toc)) (section 2 "sxml-templates" (p "The " (tt "sxml-templates") " egg uses " (int-link "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.")) (section 2 "License" (p (tt "sxml-templates") " is in the Public Domain and may be reproduced or copied without permission.")) (section 2 "Authors" (p (int-link "/users/Moe Aboulkheir" "Moe Aboulkheir"))) (section 2 "Requirements" (ul (li (int-link "sxml-transforms")) (li (link "http://srfi.schemers.org/srfi-9/srfi-9.html" "SRFI 9") " (record types)"))) (section 2 "Examples" (section 3 "Uselessly simple example" (highlight scheme "(use sxml-templates)\n\n(printf \"~A\\n\" (sxml-template:fill-string\n                 (make-sxml-template '(%data key))\n                 '((key . \"value\"))))") (p "Will print the string " (tt "value") ".")) (section 3 "Using with Spiffy" (p (b "TODO") ": This is for Spiffy for Chicken 3.  Needs updating to a recent Spiffy.") (highlight scheme "(use sxml-templates spiffy)\n\n(define (my-app:read-sxml-template basename)\n  (make-sxml-template\n    (read (open-input-file (string-append \"templates/\" basename \".scm\")))))\n\n(http:add-resource\n  \"/login-form\"\n  (lambda (req args)\n    (let* ((login-form-template\n             (my-app:read-sxml-template \"login-form-body\"))\n           (login-form-namespace\n             '((form-method . \"POST\")\n               (form-action . \"/login-form-post-target\")\n               (username-input-name . \"username\")\n               (password-input-name . \"password\")))\n           (login-form-template\n             (sxml-templates:fill login-form-template login-form-namespace)))\n      ; write some response headers, etc\n      (printf\n        (sxml-templates:fill-string\n          (my-app:read-sxml-template \"shell\")\n          `((body . ,login-form-template)))))))") (p (tt "templates/shell.scm") " would look something like:") (highlight scheme "(html\n  (head (title \"My Application\"))\n  (body (%data body)))") (p (tt "templates/login-form-body.scm") " would look something like:") (highlight scheme "(form\n (@ (method (%data form-method))\n    (action (%data form-action)))\n (label \"Username\")\n (input (@ (type \"text\")\n           (name (%data username-input-name))))\n (label \"Password\")\n (input (@ (type \"password\")\n           (name (%data password-input-name))))\n (input (@ (type \"submit\"))))") (p "The resource handler in the example first subtitutes some variables into the " (tt "login-form-body") " template, and then substitutes the result into the " (tt "shell") " template."))) (section 2 "Constructors" (section 3 "make-sxml-template" (def (sig (procedure "(make-sxml-template sxml)" (id make-sxml-template))) (p "Where " (tt "sxml") " is an SXML expression.  Returns a " (tt "sxml-template") " record."))) (section 3 "sxml-template:fill" (def (sig (procedure "(sxml-template:fill sxml-template namespace)" (id sxml-template:fill))) (p "Where " (tt "sxml-template") " is a " (tt "sxml-template") " record and " (tt "namespace") " is an association list mapping variable names to values.  This will replace all instances of " (tt "(%data name)") " in " (tt "sxml-template") "'s underlying SXML with the result of " (tt "(alist-ref 'name alist)") ", and return a new " (tt "sxml-template") " record.  If a value in " (tt "namespace") " is a filled " (tt "sxml-template") ", then the right thing will happen.  If a value is a list, then it is assumed to be vanilla SXML and will be converted using SXML's " (tt "universal-conversion-rules") ". If a value is mentioned in the template's SXML which does not have a corresponding entry in the alist, then an error will be signalled.  See " (tt "sxml-template:fill-string") " for an equivalent which results in a string of XML, and " (tt "sxml-template->string") " for a procedure which will convert a filled " (tt "sxml-template") " record (as returned by this procedure) to a string of XML.")))) (section 2 "Predicates" (section 3 "sxml-template?" (def (sig (procedure "(sxml-template? x)" (id sxml-template?))) (p "Returns a boolean indicating whether " (tt "x") " is of the " (tt "sxml-template") " record type."))) (section 3 "sxml-template:filled?" (def (sig (procedure "(sxml-template:filled? sxml-template)" (id sxml-template:filled?))) (p "Returns a boolean indicating whether " (tt "sxml-template") " has been filled (see " (tt "sxml-template:fill") ").")))) (section 2 "Selectors" (section 3 "sxml-template:sxml" (pre "[procedure] (sxml-template:sxml sxml-template)") (p "Returns the value of the " (tt "sxml") " argument in the " (tt "make-sxml-template") " call which was used to construct the given " (tt "sxml-template") "."))) (section 2 "Conversion" (section 3 "sxml-template:fill-string" (def (sig (procedure "(sxml-template:fill-string sxml-template namespace)" (id sxml-template:fill-string))) (p "Similar to " (tt "sxml-template:fill") ", but results in an XML string.  Defined as " (tt "(compose sxml-template->string sxml-template:fill)") "."))) (section 3 "sxml-template->string" (def (sig (procedure "(sxml-template->string sxml-template)" (id sxml-template->string))) (p "Converts a filled " (tt "sxml-template") " record (as returned by " (tt "sxml-template:fill") ") into an XML string.")))))