lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, list!

Everyone is familiar with text templates. One of the most popular
examples is {{mustache}} [1] [2].

Now and then I find myself writing data transformation code that would
benefit from a similar template technique.

For example, I may write something like this:

local data =
{
  { "foo.png", 42, 24 };
  { "bar.png", 0, 10 };
  { "baz.png", 314, 1 };
}

local result = { }
for i = 1, #data do
  result[#result + 1] =
  {
    sprite = "/img/" .. data[1];
    x = data[2], y = data[3];
  }
end

(Actual code and data structures are, of course, usually somewhat more
complicated than this.)

I'd like to write this declaratively. Maybe like this:

local result = T:map (data)
{
  sprite = "/img/" .. T(1);
  x = T(2), y = T(3);
}

Before I go ahead and reinvent the wheel, I wonder, is there something
along these lines out there?

Alexander.

[1] http://mustache.github.com/
[2] http://olivinelabs.com/lustache/