[Haskell-cafe] Templates as typeclasses?

Tobias Dammers tdammers at gmail.com
Fri May 9 07:51:52 UTC 2014

I think what you're after is a template language implemented as an EDSL,
that is, a domain-specific language embedded in the host language, such
that the template language's building blocks are language constructs in
the host language.

The various Haskell template systems take varying approaches here. The
ones I can think of are:

- EDSL's, like Blaze, where templates are Haskell functions composed of
  other Haskell functions. E.g.:
  `myTemplate = div $ do { h1 "hello"; p $ toHtml $ toNameCase username }`
- Quasi-quoters, where the parsed template gets injected into the host
  Haskell code at compile time, before type checking even. Some of these
  have syntax constructs for popping Haskell code back into the
  template. Example:
  `[template|<h1>Hello</h1><p>{{ toNameCase username }}</p>|]
- Preprocessors, where you run your Haskell source through an external
  tool before compiling it. Example:
  `template =
    <p>toNameCase username</p>`
- Full-on templates, where you feed template source to a template
  compiler or interpreter with an explicit context, e.g.:
  `runTemplate "myTemplate.tpl" [("username", toNameCase username)]`

I think it should even be possible to build some sort of hybrid that can
run in several modes, e.g. you'd run it through an interpreter during
development (for faster development cycles, skipping the build step),
but for production you compile them into your binary (for easier
deployment, better performance, and static type checks). I haven't seen
anything like that though so far, at least not industry-strength. Have
experimented with the idea myself, but then again, the result isn't
quite there yet.

On Thu, May 08, 2014 at 06:18:02PM -0500, Mike Meyer wrote:
> In going over yet again the many failings of template frameworks for
> building web apps, I recalled some of the features of the best of the lot,
> and wondered if those features might be useful in a Haskell template
> framework.
> Python's Cheetah tool makes templates almost fully functional members of
> Python's OO system.  A template can inherit from a Python class or another
> template, and a Python class can inherit from a template.
> Which makes me wonder if something similar might not be useful in a Haskell
> template framework. Possibly have each template create an appropriate
>  datatype and make it as an instance of the Template typeclass (and
> possibly others).
> Is there already a Haskell web framework that does something like this?
> Would it actually add value to a framework?
> Thanks,
> Mike

> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list