Thursday, November 20, 2008

Templates in the PLT Web Server

Almost all Web application frameworks have a templating system to allow the dynamic portion of a site to modify some semi-static content before displaying it. These frameworks are primarily useful because they allow the contributions of non-programmers to be more easily integrated into an application and remove a barrier to using traditional HTML design tools, like Amaya and Dreamweaver.

The PLT Web Server now provides a templating system of its own. It uses the syntax of Scribble to provide access to all features of PLT Scheme to template authors. Furthermore, it requires zero run-time parsing of templates—the templates your servlet uses are loaded and compiled when your servlet is compiled.

The documentation has all the details about using templates in your applications. But here's a short example:

template.html:

<table>
 @in[c clients]{
  <tr><td>@(car c), @(cdr c)</td></tr>
 }
</table>

servlet.ss:

(let ([clients (list (cons "Young" "Brigham")
                     (cons "Smith" "Joseph"))])
 (include-template "template.html"))

Evaluates to:

<table>
  <tr><td>Young, Brigham</td></tr>
  <tr><td>Smith, Joseph</td></tr>
</table>

0 comments: