Mettle

Last updated 2008-02-24

If you've somehow happened across the page without knowing what Mettle is already, you should know that it's just something that I'm playing around with at this point. I'm not using it for anything serious.

If it looks interesting and you'd like to know more, feel free to email me, ozzi@this-domain, or talk to me in the #chicken channel on Freenode.

Libraries

Mettle makes use of three libraries, only the second of which have I gotten around to writing any docs for:

URL Handling

Every request that comes into Mettle is matched against the handlers that you define. So if, for example, you were going to create a blogging engine, you might want a url like "article/1" to point to your first post. So you'll set up a handler like so:

(mettle:handle-get '("posts" id) article-page)

That tells Mettle that every url that matches "article/id" will be rendered with the article-page function. id is a url variable, which in this case is the id of the post that's being viewed. The easiest way to get at url variables is to define article-page to accept a keyword variable with that name, like so:

(define (article-page #!key id) ...)

Templates

Mettle's philosophy on templates is inspired by Javascript, and in particular the jQuery library. It has no love for code of any kind embedded into HTML. In fact, there's nothing at all embedded into templates in Mettle. They're HTML, and nothing else.

So, to create dynamic pages, you process the HTML just like you would with jQuery: by manipulating the DOM. You search for the nodes you want to transform using XPath, and in the future, possibly jQuery-style queries.

So, for a simple example, we'll define a page that loads a template and adds a header and a footer to it:

(define (simple-page)
  (mettle:respond-template
   "page.html"
   (htmod:prepend "/html/body"
                  (mettle:load-template "header.html"))
   (htmod:append "/html/body"
                 '(div (@ (class "footer")) "Footer") )))

The mettle:respond-template function takes a filename and a number of transformations to apply to the template. A transformation takes an XPath expression, and (sometimes) nodes to be added to the template. In this case, our XPath query finds the body tag, and we're prepending the header, which we load from another file, and appending the footer, which we specify directly with SXML.

Note that both the mettle:respond-template and mettle:load-template parse their templates into SXML. Everything that Mettle does is in SXML, and any functions are libraries that work on SXML can be used within and alongside Mettle.

There's more!

But I haven't gotten around to writing it yet. Sorry.