/jhtml

Common Lisp html from list generator. Useful for templates.

Primary LanguageCommon LispOtherNOASSERTION

jhtml: simple html generator

jhtml:jhtml is a function that takes in a list and spits out a string of tightly packed html.

Usage examples

First steps

Take a sexp - it returns html! Intuitive, too.

(asdf:load-system :jhtml)

(jhtml:jhtml '(p "hello!")) ; => "<p>hello!</p>"
(jhtml:jhtml '((h1 "hi") (p "hey"))) ; => "<h1>hi</h1><p>hey</p>"

A little package construct

(jhtml:jhtml
 `(div :class "package"
   (h2 :class "package-name" "jhtml")
   (p :class "package-description" "sexp to html converter!")))

Special rules mini intro

Special rules are “special” tags that behave differently than one would expect any other tag to behave, for example the DOCTYPE. It doesn’t have a closing tag, nor does it have attributes, just one parameter, the document type.

(jhtml:jhtml '(doctype)) ; => "<!DOCTYPE html>"
(jhtml:jhtml '(doctype "another")) ; => "<!DOCTYPE another>"

Combine it!

(jhtml:jhtml
 '((jhtml:doctype) ; special rule! (they belong to packages)
   (html
    (head
     (title "Hello")
     ;; self-closing tags are keywords
     ;; this is because they're all special rules in the :KEYWORD package
     (:link :rel "stylesheet" :type "text/css" :href "link/to/css"))
    (body
     (h1 "Hi!!")
     (nil You can use a nil tag for comments!)
     (p "whadup?")))))

The previous code will return: (formatted for clearness)

<!DOCTYPE html>
<html>
  <head>
    <title>Hello</title>
    <link REL="stylesheet" TYPE="text/css" HREF="link/to/css" />
  </head>
  <body>
    <h1>Hi!!</h1>
    <p>whadup?</p>
  </body>
</html>

Built-in special rules

jhtml:doctype

inserts a doctype directive

(jhtml:jhtml '(jhtml:doctype)) ; => "<!DOCTYPE html>"
(jhtml:jhtml '(jhtml:doctype "another")) ; => "<!DOCTYPE another>"

jhtml:insert-html

because jhtml filters its strings to escape them, you need INSERT-HTML to insert raw html

;; without INSERT-HTML
(jhtml:jhtml '(head "<title>helo</title>")) ; => "<head>&lt;title&gt;helo&lt;/title&gt;</head>"

;; with INSERT-HTML
(jhtml:jhtml
 '(jhtml:insert-html
   "<head><title>helo</title></head>")) ; => "<head><title>helo</title></head>"

mostly, it’s intended to be used for using jhtml within another jhtml call

(defun maths (a b)
  (declare (ignorable a))
  (jhtml:jhtml
   `(html
     (head (title "maths"))
     (body
      (jhtml:insert-html
       ,(when (> (+ a b) 10)
          (jhtml:jhtml '((h1 "BIG!") (:hr)))))
      (p ,a " + "
         ,b " = "
         ,(+ a b))))))

Why the `J’?

Because it sounds nice.