HTML Macros

Another feature of FOO is that it allows you to define HTML “macros” that can translate arbitrary forms into HTML s-expressions that the html macro understands. For instance, suppose you frequently find yourself writing pages of this form:

  1. (:html
  2. (:head (:title "Some title"))
  3. (:body
  4. (:h1 "Some title")
  5. ... stuff ...))

You could define an HTML macro to capture that pattern like this:

  1. (define-html-macro :standard-page ((&key title) &body body)
  2. `(:html
  3. (:head (:title ,title))
  4. (:body
  5. (:h1 ,title)
  6. ,@body)))

Now you can use the “tag” :standard-page in your s-expression HTML, and it’ll be expanded before being interpreted or compiled. For instance, the following:

  1. (html (:standard-page (:title "Hello") (:p "Hello, world.")))

generates the following HTML:

  1. <html>
  2. <head>
  3. <title>Hello</title>
  4. </head>
  5. <body>
  6. <h1>Hello</h1>
  7. <p>Hello, world.</p>
  8. </body>
  9. </html>