Showing posts with label Template Toolkit. Show all posts
Showing posts with label Template Toolkit. Show all posts

Jul 21, 2010

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.
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.

Jun 4, 2010

Using ref to fix 5 year old bug

So I haven't been hacking perl for 5 (or more) years but I forked Template::ShowStartStop from Template::Timer which is that old. since I forked it this test has bugged me since I didn't really understand the test, the section of code it referred to or the actual problem.

This is an approximation of the error you'd get.
Couldn't render template "undef error - Can't call method "name" on
unblessed reference at /usr/lib/perl5/site_perl/5.8.0/Template/Timer.pm line
66. "
This error is actually a copy from bug #15457 but I happen to know it's basically the same error you'd get with an eval.

So what's the problem? can you spot it? ( original timer code )

Ugh! That's ugly, let's take a cue from Perl Best Practices and format it as a tabular ternary instead. Now we can read it...

Bug #13225 suggests a problem with eval and the following fix (which I translated into something more similar to the current code).

well that should fix our eval problem... since an eval will be a reference of type SCALAR. but what happens if it's a reference not of type ARRAY or SCALAR? then we'll still get that error.

According to the ref function documentation
If the referenced object has been blessed into a package, then that package name is returned instead. You can think of ref as a typeof operator.
what this means is I could check what type of object it was that I was normally getting. In order to do this I put $what into the output I get. I found out that most of the time $what is a Template::Document. So now I optimized my code for that situation.

See the thing is that we really don't want $what->name method to be called unless it's a Template::Document which actually has that method. I'm not sure that I'm not missing any tests at this point, but I'm pretty confident that, at least, this kind of bug won't crash my module anymore.

Jun 3, 2010

Testing TT Template's

So the poorly made patch the other day converted my Test::More test to use Template::Test which removed quite a bit of code from the test itself. I hadn't seen Template::Test beforehand.

Here's a very simple example of a test you could write to make sure a template is being output ok.

If you want more examples I now have quite a few for Template::ShowStartStop which you can see in the Test directory of the master branch.