ANNOUNCE: HStringTemplate -- An Elegant, Functional, Nifty Templating Engine for Haskell
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
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Sterling Clover wrote:
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.
Ah! Finally! They invented Lambda Calculus! Perhaps after some more decades they invent Typed Lambda Calculus as well! (sorry, couldn't resist - Best regards, J.W.) -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHiywR3ZnXZuOVyMIRAtGtAKCzfpu9PktmsatKHlM5I2CH4ysR8gCgibat eznCg3FyVJ4tsg/tOvRV8Qs= =4MoX -----END PGP SIGNATURE-----
Sterling Clover wrote:
HStringTemplate is a port of Terrence Parr’s lovely StringTemplate (http://www.stringtemplate.org) engine to Haskell.
Reading Terrence Parr describe StringTemplate, at http://www.stringtemplate.org/about.html is amusing:
The fact that StringTemplate does not allow such things as assignments (no side-effects) should make you suspicious of engines that do allow it.
and
Another distinctive StringTemplate language feature lacking in other engines is lazy-evaluation. StringTemplate's attributes are lazily evaluated in the sense that referencing attribute "a" does not actually invoke the data lookup mechanism until the template is asked to render itself to text. Lazy evaluation is surprising useful in both the web and code generation worlds because such order decoupling allows code to set attributes when it is convenient or efficient not necessarily before a template that references those attributes is create
and
Just so you know, I've never been a big fan of functional languages and I laughed really hard when I realized (while writing the academic paper) that I had implemented a functional language. The nature of the problem simply dictated a particular solution. We are generating sentences in an output language so we should use something akin to a grammar. Output grammars are inconvenient so tool builders created template engines. Restricted template engines that enforce the universally-agreed-upon goal of strict model-view separation also look remarkably like output grammars as I have shown. So, the very nature of the language generation problem dictates the solution: a template engine that is restricted to support a mutually-recursive set of templates with side-effect-free and order-independent attribute references.
-- Chris
On Jan 14, 2008 9:47 AM, Sterling Clover <s.clover@gmail.com> wrote:
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/
Template systems have been a crucial missing part of Haskell web development. I am very happy to hear about this project, and will definitely be looking at this in the near future! Thanks, Bit
HStringTemplate is a general purpose templating system, geared especially towards HTML and based on Terrence Parr’s Java library. On Hackage at: http://hackage.haskell.org/cgi-bin/hackage-scripts/ package/HStringTemplate-0.2 Development version at: darcs get http://code.haskell.org/ HStringTemplate/ Haddocks at: http://code.haskell.org/HStringTemplate/dist/doc/html/ HStringTemplate/Text-StringTemplate.html Additional documentation on the grammar at: http://www.antlr.org/wiki/ display/ST/StringTemplate+3.1+Documentation as well as in some posts at http://fmapfixreturn.wordpress.com Lots of cleanup, lots of additions, hopefully this is an extremely usable release. Still no group or interface files, but I suspect that those are mainly useful for code-generation, which seems like something Haskell programmers would want to use something other than a templating system for in any case. On to the good stuff: * Now on hackage! * Generics. Not one but two types. One set of simple bindings for the standard Data class, and one for syb-with-class. (Alex Drummond’s RJson library, which is really cool, was very helpful in figuring out how to do this). * A withContext method that turns any set of name-value bindings into the context for a StringTemplate. Along with the syb-with-class bindings, this should make for relatively seamless interoperability with HAppS. * Lots of other additional bindings for working with standard time formats, numeric types, etc. * Encoders. A standard mechanism for HTML-escaping strings (or javascript-escaping, or urlencoding them, or etc.) that A) ensures it happens uniformly and B) ensures it happens no more than once. * Improved pretty printing support, eliminating corner-cases where wrapping did not occur. * Creation of directory groups is now done properly in the IO monad, with caching functionality moved to the scarily-named unsafeVolatileDirectoryGroup. * 80% Top-Level testing coverage in the base. (And more to come, but I only have so much time). And of course, patches and feedback always welcome. --Sterl
participants (4)
-
Bit Connor -
Chris Kuklewicz -
Johannes Waldmann -
Sterling Clover