Saturday, June 7, 2008

Simple code snippet; with common lisp (sbcl) use html-template to generate html documents

As a compliment to my earlier post. Here is an example using html-template like any other server page like Active Server Pages or Java Server Pages. Pass variables to the template parser and the parser will output a full HTML document.

;; Berlin Brown
;; Simple prototype example; using commmon lisp and html-template,
;; Read an input html document template file and use the
;; template parser to generate a new html document with the
;; populated variables.
;; Filename: test_html_templ.lisp
;; Environment: (SBCL 1.0.14) common lisp with libraries.
;; References:
;; [1]

(require :html-template)

(defun parse-template ()
(let* ((rows
(loop for i below 49 by 7
collect (list :cols
(loop for j from i below (+ i 7)
for string = (format nil "~R" j)
collect (list :content string
(oddp j))))))
(values (list :rows rows)))
(with-open-file (stream "output.html"
:direction :output
:if-exists :supersede)
#p"./index_templ.html" values
:stream stream))))

(defun main ()
(format t "Running~%")
(format t "Done~%"))

;; End of the file.

No comments: