Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I create HTML-generating macros or functions? #24

Open
iyra opened this issue Oct 28, 2016 · 2 comments
Open

How do I create HTML-generating macros or functions? #24

iyra opened this issue Oct 28, 2016 · 2 comments

Comments

@iyra
Copy link

iyra commented Oct 28, 2016

Take the following as an example:

(defmacro with-html (&body body)
  `(cl-who:with-html-output-to-string (*standard-output* nil :prologue t)
                      ,@body))

(defmacro html-standard-page (&body body)
  `(with-html (:body ,@body)))

(defmacro test-g (id)
  `(:table (:tr (:td ,id))))

With this code, I would expect (html-standard-page (test-g "15")) to output something like:

`

15
`

Why doesn't it? Instead I get warnings that :td etc. are not defined. What am I doing wrong?

I hope this is an OK place to post this, as I wasn't sure where I should go with support questions. Thank you.

@ckonstanski
Copy link

ckonstanski commented Dec 23, 2016

I'm not running your code to actually see what the debugger says. But I can see things that jump out:

  • in `test-g': it's a macro. You need two backquotes if you want it to return a backquoted list. The first backquote is "used up" by the macro.

  • in `html-standard-page': The form (:body ,@Body) needs to be backquoted. And then ,@Body needs to become ,,@Body.

Once you fix those two things, you will be where I am at: your with-html will not work, but for a different reason that I cannot yet explain. I'm about to create an issue of my own. Unless defining `with-html' as a macro is a potential workaround. I will actually try that first.

@svetlyak40wt
Copy link

@iyra that is because you are trying to make everything a macro. You don't need a macro for test-g.

Here is the working code:

(defmacro with-html (&body body)
  `(cl-who:with-html-output-to-string (*standard-output* nil :prologue t)
                      ,@body))

(defmacro html-standard-page (&body body)
  `(with-html (:body ,@body)))

(defun test-g (id)
    (cl-who:with-html-output (*standard-output*)
        (:table (:tr (:td (cl-who:esc id))))))

I've only modified a test-g.

You might also define a small helper for test-g:

POFTHEDAY> (defmacro html (&body body)
             `(cl-who:with-html-output (*standard-output*)
                ,@body))
HTML
POFTHEDAY> (defun test-g2 (id)
             (html
               (:table (:tr (:td (cl-who:esc id))))))
TEST-G2
POFTHEDAY> (html-standard-page (test-g2 "15"))
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<body><table><tr><td>15</td></tr></table></body>"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants