Dhandlers

These files complement autohandlers. Their purpose is similar, in that they will automatically kick in to handle certain requests. Say you have a blog with URL-s like this: /blog/yyyy/mm/dd/post-title. This URL would normally map to the following template:

ROOT/blog/yyyy/mm/dd/post-title.syt

but for making a blog it's best to store entries in a database, so you wouldn't really have that directory structure and template.

Dhandlers exist to facilitate this. You'd place a file called dhandler.syt in ROOT/blog/ and the content might be like the following:

;; you'll have to supply the function fetch-blog-entry from Common Lisp
;; we'll look later into how to do this.
{(let ((entry (fetch-blog-entry (http/script-name))))
   (if entry
     ;; display blog post
     {
       <div class="blog-post">
         <h1>{\entry.title}</h1>
         <div class="body">{entry.body}</div>
       </div>
     }
     ;; not found, show error page
     {
       <h1>404 Not found</h1>
       <p>
         We couldn't find this entry:
         <tt>{\(http/script-name)}</tt>.
       </p>
       {[http/set-status 404]}
     }))}

The function http/script-name will return the request path, i.e. blog/yyyy/mm/dd/post-title, so using that you can locate in the database the entry. On the other hand, if it's not found we're setting the 404 status using http/set-status.

That's just one way to do it; there are multiple variants, for example you can easily install a custom handler for the "/blog/..." URLs from Common Lisp; there you'd fetch the entry from DB and delegate to a proper template if it found, or show the 404 otherwise.

Fork me on Github