ErlTL 0.9.1

Posted by Yariv on October 21, 2006

In the past couple of days, I’ve done a lot of ErlTL hacking. Here’s what I’ve accomplished:

- I added support full Erlang function declarations. ErlTL now gives you unlimited flexibilty in function declarations. You can decide what the variables are, add guards, and use Erlang’s full pattern matching capabilities. With pattern matching, your don’t have to rely as much on cumbersome if-else statements. Here’s a sample function declaration that’s now legal in ErlTL:

<%@ foo(Bar, {Baz, Boing} = Data) when is_integer(Bar) %>

- I added support for top-level declarations. At the top of your template file, you can now declare Erlang module attributes, compiler directives, and even complete functions (although the latter is not advised as template files aren’t meant to contain logic that’s better put in a regular Erlang module). One of main benefits is that now you call functions from other templates with less code by importing them. The new syntax for top-level declarations is ‘<%~ .. %>‘. Here’s an example:

<%~
-author("Yariv Sadan").
-import(my_killer_widgets,
   [foo/1, bar/1, baz/2]).
 
%% this is allowed, but not advised
pluralize(Noun) -> [Noun, "s"].
%>
<% foo(Data) %>

- I fixed a bug that caused ErlTL to fail to parse multi-line Erlang expressions.
- I rewrote almost all the code. Yes, the last code worked, but it needed to be prettier :)

And finally, here’s an example of a complete ErlTL 0.9.1 template:

<%~
%% date: 10/21/2006
-author("Yariv Sadan").
-import(widgets, [foo/1, bar/2, baz/3]).
%>
<%!
   This is a sample ErlTL template that renders a
   list of albums in HTML
%>
<html>
<body>
<% [album(A) || A <- Data] %>
</body>
</html>
 
<%@ album({Title, Artist, Songs}) %>
Title: <b><% Title %></b><br>
Artist: <b><% Artist %></b><br>
Songs: <br>
<table>
<% [song(Number, Name) || {Number, Name} <- Songs] %>
</table>
 
<%@ song(Number, Name) when size(Name) > 15 %>
<%? <<First:13/binary, Rest/binary>> = Name %>
<% song(Number, [First, <<"...">>]) %>
 
<%@ song(Number, Name) %>
<%?
Class =
  case Number rem 2 of
    0 -> <<"even">>;
    1 -> <<"odd">>
  end
%>
<tr>
  <td class="<% Class %>"><% integer_to_list(Number) %></td>
  <td class="<% Class %>"><% Name %></td>
</tr>

Damn — now I really can’t wait to build those killer Erlang webapps! :)

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. mikkom Tue, 24 Oct 2006 03:15:24 EDT

    Sorry to say my opinion but that template syntax is almost exactly what web developers have tried to get away from in another languages. (that’s why java has custom tags and jstl instead of plain jsp, that’s why there are templating frameworks even for php).

    I really suggest studying evolution in java web templating frameworks, you will find gold nuggets of information there.

  2. Yariv Tue, 24 Oct 2006 09:17:58 EDT

    Yes… I know about some of those template languages. But I needed something that was quick to implement, that was an improvement over .yaws files and ehtml, that had good performance, and that would interact well with other Erlang code. I might add more high-level syntax in the future but I needed this starting point.

    Btw, Rails quite successfully uses ERb, which is similar in concept to ErlTL.

    Thanks for the feedback!

  3. Yariv Tue, 24 Oct 2006 09:24:32 EDT

    One more thing – Java and PHP don’t have pattern matching! :)

  4. mikkom Tue, 24 Oct 2006 09:58:26 EDT

    I might be a little bit biased because I have implemented my own web development environment (including a very, very simple template language) with erlang and many before that, even when cgi was a new thing and we did our web development with C.

    Anyways, I really recommend reading this document, it’s one of the best ever written about templates and I bet it will give you some great ideas:

    http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf

    It’s extremely important to understand the actual position of templates before you start implementing a language of your own because otherwise you will end up with something that will bind logic with presentation and (believe me, I have done this too many times) it’s really hard to separate afterwards.

    ps. I could not live without pattern matching anymore.

  5. Roberto Tue, 24 Oct 2006 21:29:58 EDT

    Now I started to use ErlTL, and I really like it. Works fine with Aptana-Eclipse-HTML-Editor and yes, pattern matching is the best thing since slide breed !!!

  6. Charles Fri, 27 Oct 2006 03:38:29 EDT

    Thank you for ErlTL, Yariv!
    I used it with yaws and appmods. It’s much simpler than sheer ehtml (In fact they can be combined). It’s also so handy when used in a home-grown embedded mini-httpd server to deliver dynamic pages.

Comments