Thursday, July 29, 2010

New version of the template library

Sometime in 2007 I wrote a small Haskell library for simple string substitution: given a string with placeholders, e.g. “Hi, $name!”, and a context that maps placeholders to values you can easily substitute the placeholders for the values.

After receiving some user feedback I’ve now updated the library and released version 0.2 on Hackage. Here's what's new in this release:

  • The String type has been replaced with the new Text type from the text library.
  • The API has been generalized to support more sophisticated error reporting, using an applicative functor interface.
  • The I/O API has been removed; use the I/O API in the text library API instead.
  • The context that maps placeholder to values can now be any function from keys to values, instead of just a Map.

Here’s a usage example:


  1. Cool - neat library!

    Any plans/thoughts on supporting other placeholder patterns than "$var", e.g. "@VAR@". I don't have a urgent use-case in mind but just wondering if the library may evolve more. Perhaps I am just unsuspectedly volunteering myself to write a patch one day... ;)

  2. Jens,

    Perhaps, if I could do it without introducing too much complexity in the API. The Python equivalent of this library

    which this library is modeled on, supports changing the delimiter (i.e. "$") and the regex that defines what characters are valid in a placeholder.

  3. dyk if haskell committee has any plans to support string substitution in the language itself?

    it seems like there isn't any reason they couldn't support something like:

    "hello! #{some expression that evaluates to a string} more string here"

    since all they have to do is turn it into:

    "hello! " ++ (some expression that evaluates to a string) ++ " more string here"

  4. I haven't seen any proposals. The list of current Haskell language related proposals is here: