RSS Feed

Lisp Project of the Day


You can support this project by donating at:

Donate using PatreonDonate using Liberapay


Tests 🥺
CI 🥺

This is an HTML generator. This library is similar to cl-who or spinneret, but with two interesting differences.

The first distinction is that print-html is written in the literate programmings style and have very succinct code which can be read in 5-10 minutes.

The second distinction is that print-html is based on generics and you can define a method to print some classes into HTML.

Let's try this!

POFTHEDAY> (print-html:print-html-to-string
               ((:section :class "intro")
                (:p "Hello Lisp World!")))))
<section class=\"intro\">
<p>Hello Lisp World!</p>

Now I'll create a class of user which can be serialized into HTML:

;; This is our buisiness logic:
POFTHEDAY> (defclass user ()
             ((name :initarg :name
                    :reader get-name)))

POFTHEDAY> (defparameter *user*
             (make-instance 'user
                            :name "Sponge Bob"))

POFTHEDAY> (defun get-profile-uri (user)
             (format nil "/user/~A"
                     (get-name user)))

;; Here is the method to transform business logic
;; object into its HTML representation.
POFTHEDAY> (defmethod print-html:render ((user user))
               ((:a :href (get-profile-uri user))
                (get-name user))))

;; Note, it returns HTML nodes, not 
POFTHEDAY> (print-html:render *user*)
    :NAME :A
    :ATTRS (:HREF "/user/Sponge Bob")
    :CHILDREN ("Sponge Bob")))

;; Now we can use this kind of objects
;; directly when rendering HTML pages:
POFTHEDAY> (print-html:print-html-to-string
               ((:section :class "intro")
                (:p "Hello"
<section class=\"intro\">
<a href=\"/user/Sponge Bob\">Sponge Bob</a>

Seems this is an interesting approach to render HTML. What do you think?

Brought to you by 40Ants under Creative Commons License