[Haskell-cafe] ANNOUNCE: HStringTemplate -- An Elegant, Functional, Nifty Templating Engine for Haskell

Sterling Clover s.clover at gmail.com
Mon Jan 14 02:47:40 EST 2008


HStringTemplate is a port of Terrence Parr’s lovely StringTemplate  
(http://www.stringtemplate.org) engine to Haskell.

It is available, cabalized, at:
darcs get http://code.haskell.org/HStringTemplate/

As interest has grown in using Haskell for web applications, there  
has been an increasing buzz about the need for a good templating  
engine in Haskell. Why might we need this? After all, Haskell has  
lovely combinator libraries for generating HTML programmatically,  
enforcing good form through its type system. But sometimes, we don’t  
want well-formed HTML. We want the ugly stuff that floats around to  
deal with eight varieties of browser incompatibilities and the latest  
silly ajax trick. Or sometimes we’re working with a team of graphic  
designers, and they want to work in almost-HTML. Or sometimes we just  
want to be able to change the design of a web application on the fly,  
completely independent of our program logic, and of, heavens forbid,  
recompiling and possibly messing with a live application.

So template engines are popular, and indeed, considered a key part of  
most web frameworks out there. One problem — they’re mainly awful,  
imperatively-conceived behemoths that capriciously mix program logic  
with display and, consequently, entail a great deal of overhead.  
Enter StringTemplate, a nifty and fairly-well developed template  
format that’s both pure and functional, and therefore pretty much the  
only one of its kind. Indeed, it also seems to be getting heavy use  
in code generation because its paradigm maps neatly to traversing  
parse-trees.

HStringTemplate is not feature-complete, and indeed is only at  
version 0.1. But it should implement pretty much everything in the  
standard StringTemplate 3.0 grammar, only nicer, because it’s in  
Haskell. There are scads of different recursive constructs and ways  
to handle inclusion and inheritance. Furthermore, HStringTemplate  
handles conditionals, and sports also a very Haskellish  
implementation of custom rendering.

Templates can be constructed that return strings, ShowSs,  
bytestrings, or even pretty printer Docs that handle wrapping,  
indentation, and fill elegantly. Even better, these templates are  
parsed and compiled only once, after which point there isn't a syntax  
tree anymore, just a function that operates on the environment of  
attributes that have been passed to it.

Where I take it from here depends in part on what sort of response I  
get, so patches, gripes, API comments and feature requests are all  
more than welcome.

Please note that I'm still working in 6.6.1. Everything should be in  
place to compile properly with the base split, but if it isn't,  
again, patches more than welcome.

Full announcement at: http://fmapfixreturn.wordpress.com/

--SC


More information about the Haskell-Cafe mailing list