More herml Progress
Posted: December 13th, 2008 | Author: kevin | Filed under: Erlang | Comments OffI’ve had a bit of time to hack on herml and I’m happy to report good progress. The lexer and parser are now both written in Erlang. I figured out my problems with leex which allowed me to drop the C-based parser and write everything in Erlang.
More of the haml-like syntax is supported with the exception of iteration. I’ve got ideas on how to do that by introducing a list comprehension-like syntax.
In it’s current form, herml can take a template like this:
%html
%body
#header.message
@WelcomeMessage
#main[@example_driver:default_attr]
Welcome back!
and turn it into this:
<html>
<body>
<div id="header" class="message">
Welcome to herml!
</div>
<div class="content_area" id="main">
Welcome back!
</div>
</body>
</html>
herml uses the token @ to denote variable references and function calls. In the above example, the variable @WelcomeMessage is retrieved from the template’s environment and substituted into the template. As a bonus, if a variable reference refers to an Erlang fun, herml will invoke the function and substitute the return value into the template.
herml also understands qualified function calls using the syntax @ModuleName:FunctionName. The example template used a call to example_driver:default_attr to populate the attributes for the main div element.
Both anonymous funs and functions called via qualified function calls must have an arity of one since herml will pass in the current template environment. herml provides read-only access to the environment, for now, but this might change later.
Here’s what the example_driver:default_attr/1 module looks like:
-module(example_driver).
-export([default_attr/1]).
default_attr(_Env) ->
[{class, "content_area"}].
Element attribute lists are Erlang proplists containing name/value tuples. Attribute names must be Erlang atoms while attribute values are strings (or lists for the Erlang purists :)
I’ve created a public Github repo for herml here. It’s a bit rough but feel free to check out the code and play around with it.