Begin main content

Using Haml with Catalyst

Thanks to Viacheslav Tykhanovskyi's Text::Haml, we can now enjoy the good clean fun of Haml markup in Perl. Much Perl web templating is done with Template::Toolkit, and using Haml within TT is also now possible thanks to Template::Plugin::Haml by Caleb Cushing. The trouble is, you need to wrap every template in some boilerplate:

[%- USE Haml ; FILTER haml -%]
...
[%- END -%]

So I hacked up a quick solution to using .haml templates directly in Catalyst. First, create a MyApp::View::Haml class:

package Hello::View::Haml;

use strict;
use warnings;

use base 'Catalyst::View::TT';

use Template::Plugin::Haml;

__PACKAGE__->config(
    TEMPLATE_EXTENSION => '.haml',
    render_die => 1,
);

sub render {
    my ($self, $c, $template, $args) = @_;

    $c->log->debug(qq{Rendering Haml/TT template "$template"}) if $c && $c->debug;

    # args may be passed in or be in the stash
    $args = { %{ $c->stash} }
        if ref $args ne 'HASH';

    # let the master haml template know which template to render
    $args->{haml_template} = $template;

    # do the normal TT render
    return $self->SUPER::render($c, 'haml_master.tt', $args);
}

1;

Then you need a haml_master.tt template:

[%- USE Haml ; PROCESS $haml_template | haml -%]

And finally set your config to use the Haml view by default:

default_view => 'Haml'

Et voila - .haml template rendering albeit with only TT style variable interpolation. Getting haml interpolation working might need some change to how the stash is passed to the Text::Haml module.

A controller method might look like this:

sub foo :Global {
    my ($self, $c) = @_;

    $c->stash(foo => 'bar');
    $c->stash(template => 'foo.haml');
}

And foo.haml:

!!! HTML
%body
  %p This is haml inside TT rendering your variables: [% foo %]

Becomes rendered as:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<body>
<p>This is haml inside TT rendering your variables: bar</p>
</body>

06:35 PM, 29 Jul 2010 by Mark Aufflick Permalink | Short Link | Comments (0)

Higher Order Perl now available as a *free* PDF

I've reviewed "Higher Order Perl" by Mark Jason Dominus before - it's a great book for people who know Perl and want to master topics such as parsing or memoisation or who want to incorporate more functional approaches such as currying into their code.

It's worth buying, but now you can also get a free PDF thanks to the generosity of the author, Mark Jason Dominus:

http://hop.perl.plover.com/book/

02:37 PM, 17 Sep 2009 by Mark Aufflick Permalink | Short Link | Comments (0)

MapReduce in Perl

Someone recently asked to get access to the OpenACS paypal-support package that I wrote and lives in my cvs repository. Unbenknownst to me, my recent firewall change blocked access to the cvs web interface, which I have now fixed.

While I was looking around my public cvs interface, I remembered that I had never quite finished my Perl implementation of Google's MapReduce infrastructure.

Since I'm too busy to get back onto it now I figure I may as well unleash it on you guys. Be warned that there's no documentation (that I remember) and that the current implementation only works on a single machine, although the structure is easily extendable to multiple machines by replacing the forking with some form of rpc.

My sample Perl implementation of MapReduce (for sufficiently weak meaning of the word 'implementation' ;)

Given that limitation it may also not quite work properly - I really don't remember what state I left it in. Nevertheless if you first read the MapReduce paper by Jeffrey Dean and Sanjay Ghemawat then the api and code should make sense.

If anyone finds this interesting please feel free to email me any questions. If enough people are interested I may be able to make the time to polish it off and write documentation!

03:36 PM, 18 Nov 2006 by Mark Aufflick Permalink | Short Link | Comments (0)

XML

Blog Categories

software (29)
..cocoa (14)
  ..heads up 'tunes (5)
..ruby (4)
..lisp (1)
..perl (3)
..openacs (1)
mac (19)
embedded (2)
..microprocessor (2)
  ..avr (1)
electronics (3)
design (1)
photography (24)
..black and white (6)
..A day in Sydney (18)
..The Daily Shoot (5)
food (2)

Notifications

Icon of Envelope Request notifications

Syndication Feed

XML

Recent Comments

  1. John Amoratis: Thanks for making a wonderful program
  2. Unregistered Visitor: To replace current categories or remove them...
  3. Unregistered Visitor: Base 256
  4. Unregistered Visitor: ...
  5. Unregistered Visitor: www.pipl.com
  6. Unregistered Visitor: This reminds me of the infinite monkeys
  7. Unregistered Visitor: Feedback
  8. Unregistered Visitor: Diito... Snow Leopard no worky
  9. Unregistered Visitor: WFM
  10. Unregistered Visitor: Pie