Iteration in herml

by Sean Cribbs

herml is a Haml-like template language for Erlang that I’ve been developing with Kevin Smith. One of the major hurdles we’ve had to tackle is iteration, which is a pretty common idiom in template languages, but not so much in Erlang. In Erlang, one normally uses a tail-recursive function, lists:foreach(), or a list comprehension to iterate over a collection (list). Initially, Kevin had planned something like this:

[%tr
  %td
    @User || User <- @Users]

However, that proved to be too difficult to parse:

  1. The opening bracket breaks with the style previously set forward.
  2. The comprehension piece comes at the end, making it hard to know ahead of time what is being iterated over

Instead, on Thursday (erloungeRDU hack night), we agreed to do this simpler, but still LC-like syntax:

- [@User] <- @Users
  %td
    @User

This proved to be much easier to parse and implement. We even have structured decomposition, like so:

- [{@Id, @Login}] <- @Users
  %td
    @Id
  %td
    @Login

And match ignores with _:

- [{_, @Login}] <- @Users
  %td
    @Login

There’s still a lot to do, but it’s shaping up nicely. Check it out!

© 2006-present Sean CribbsGithub PagesTufte CSS