How to produce HTML from a list

249 Views Asked by At

The usual way to generate HTML using CL-WHO is by using the macros with-html-output and with-html-output-to-string. This uses special syntax. For example:

(let ((id "greeting")
      (message "Hello!"))
  (cl-who:with-html-output-to-string (*standard-output*)
    ((:p :id id) message)))

Is it possible to write the data ((:p :id id) message) as a list instead of using the macro syntax shown above? For example, I would like to define the HTML as a list like this:

(let* ((id "greeting")
       (message "Hello!")
       (the-html `((:p :id ,id) ,message)))
  ;; What should I do here to generate HTML using CL-WHO?
  )

Can CL-WHO take a normal Lisp list and produce HTML from the list?

1

There are 1 best solutions below

2
On

You want to insert code into an expression.

Actually you would need eval:

(let* ((id "greeting")
       (message "Hello!")
       (the-html `((:p :id ,id) ,message)))
  (eval `(cl-who:with-html-output-to-string (*standard-output*)
    ,the-html)))

But this is not good to use eval. But a macro contains an implicite eval. I would define a macro for this and call the macro:

(defun html (&body body)
  `(cl-who:with-html-output-to-string (*standard-output*)
    ,@body))

;; but still one doesn't get rid of the `eval` ...
;; one has to call:
(let* ((id "greeting")
       (message "Hello!")
       (the-html `((:p :id ,id) ,message)))
  (eval `(html ,the-html)))