Calebotomy

Annoucing Template::Plugin::Haml

A few months ago we had an assignment in Web Server Admin to create a CGI page, of course the perl was just to print text not actually do anything more, but I decided to use CGI.pm just because I never had. The whole thing was nothing more than hello world.

#!/usr/bin/env perl
use 5.012;
use CGI qw(:standard);

say header,
start_html(‘hello world’),
h1(‘hello world’),
end_html;
and I thought, wow I can write all that html with just that? why can’t I use that syntax as a templating language. Of course when I asked I got imbecilic responses of how bad CGI.pm was or how it used function calls is stupid. And I was put on to Template::Declare and another which I don’t recall. Template Declare would be nice if it didn’t require the templates to be in a Perl file.

At some point I discovered Haml, simple and shorter than writing out the full HTML, unfortunately it was still longer than the CGI syntax and whitespace sensitive. But I kept looking through it. it has variables, but no loops, conditionals, or includes. So for a templating language it’s simplistic or immature. I got around to googling “Haml Perl” and found this on StackOverflow (of course without my post). I thought well, shouldn’t be too hard to make Text::Haml into a Catalyst View or … better yet a Plugin to Template Toolkit so we can get all the goodies Haml is missing.

After some bumps in the road, I managed to make a finished product.

Here’s some code and its output

Since Haml is whitespace sensitive you have to be careful about how you strip (or don’t strip) whitespace from TT code. Obviously this is a simple example, I have no idea how well this works in a larger codebase, and since there’s no XS module for Text::Haml I don’t know how well it performs. I hope it’s useful for someone though.


Share

comments powered by Disqus