2

I'm trying to generate the following html code using cl-who:

<html>
<body>
<div id="cnt_1"></div>
<div id="cnt_2"></div>
<div id="cnt_3"></div>
</body>
</html>

And here's the code that I thought would work:

(with-html-output-to-string (*standard-output* nil)
 (:html
  (:body
   (do ((cnt 1 (+ cnt 1)))
       ((> cnt 3))
     (htm (:div :id (format t "cnt_~A" cnt)))))))

But instead I get the following output:

<html><body><divcnt_1></div><divcnt_2></div><divcnt_3></div></body></html>

Seems like :id does not work with function calls. Does it mean that I can't use format in cl-who? What should I use instead?

Ben
  • 51,770
  • 36
  • 127
  • 149
mgs
  • 2,621
  • 2
  • 19
  • 14

1 Answers1

5

That's because you don't want to write directly in the stream.

CL-USER> (with-html-output-to-string (s) (:div :id "test"))
"<div id='test'></div>"

CL-USER> (with-html-output-to-string (s)
           (:html
            (:body
             (do ((cnt 1 (+ cnt 1)))
                 ((> cnt 3))
               (htm (:div :id (format nil "cnt_~A" cnt)))))))

"<html><body><div id='cnt_1'></div><div id='cnt_2'></div><div id='cnt_3'></div></body></html>"

By the way, if you want to write directly in the stream use CL-WHO:FMT.

Daimrod
  • 4,902
  • 23
  • 25
  • `FMT` works in the content section in the syntax, not the attribute value section. – Xach Jul 03 '12 at 16:25
  • @Xach: By work I suppose that you mean it does what you expect, because it works just like the op's FORMAT (write into the current stream). – Daimrod Jul 03 '12 at 16:38