Begin main content

Template::Plugin::Haml caveats

Including other templates is tricky. Either you have to indent the included template as if it was pasted inline (eg. with a bunch of spaces in front of every line instead of the first level being column zero) or you have to format the include appropriately.

For example, let's say you have a content variable which was made by rendering some templates, you'd like to do something like:

%html
  %body
    #content
      [% content %]

You want the embedded content to be location neutral, ie. start at column zero. Something like:

#myDiv
  %ul
    %li foo

Except this will be rendered AFTER the closing html tag since the combined haml will look like:

%html
  %body
    #content
#myDiv
  %ul
    %li foo

I'm going to try to think of some better way to resolve this, but for now I'm using the Template::Toolkit built in indent filter like so:

%html
  %body
    #content
      [% content | indent('      ') %]

The only downside (apart from the pain of keeping it in sync) is that you end up with the rendered html having some funky indentation after the newlines are stripped, but I can live with that. You can use a number instead of a string as an argument to indent, and it will pad by that many spaces which may turn out to be easier to keep in sync.

I'll let you know if I have any flashes of genius and will also contact the Template::Plugin::Haml and Text::Haml authors.

08:00 AM, 29 Jul 2010 by Mark Aufflick Permalink | Comments (0)

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:

packageHello::View::Haml;

usestrict;
usewarnings;

usebase'Catalyst::View::TT';

useTemplate::Plugin::Haml;

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

subrender {
    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} }
        ifref$argsne'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:

subfoo: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>

04:35 AM, 29 Jul 2010 by Mark Aufflick Permalink | Comments (0)

XML

Blog Categories

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

Notifications

Icon of envelope Request notifications

Syndication Feed

XML

Recent Comments

  1. Mark Aufflick: No UML for You!
  2. Unregistered Visitor: Umbrello on Mac OS X
  3. Mark Aufflick: Comments are now fixed
  4. Unregistered Visitor: test4
  5. Unregistered Visitor: using Amazon S3
  6. Unregistered Visitor: Thank you ! Thank you ! Thank you !
  7. Unregistered Visitor: Umbrello on leopard
  8. Unregistered Visitor: Script gor generate for library
  9. Unregistered Visitor: Similar but different
  10. Unregistered Visitor: Thanks for fixing my problem!