Skip to content

Getting Started

Project layout

A typical project that uses Template::HAML keeps its templates under an app/views/ (or any other) directory, organized by controller / feature, with the .html.haml extension:

1
2
3
4
5
6
7
8
9
my-project/
├── lib/
│   └── MyApp.rakumod
└── app/
    └── views/
        ├── home/
        │   └── index.html.haml
        └── layouts/
            └── app.html.haml

Your first template

Create hello.haml:

1
2
3
4
%html
  %body
    %h1 Hello, world
    %p.lead This page was rendered by Template::HAML.

Render it from Raku:

1
2
3
4
use Template::HAML;

my $html = HAML.render(:src(slurp 'hello.haml'));
say $html;

You should see:

1
2
3
4
5
6
<html>
  <body>
    <h1>Hello, world</h1>
    <p class='lead'>This page was rendered by Template::HAML.</p>
  </body>
</html>

Rendering from a string

HAML.render takes any string, so you can also embed templates inline:

1
2
3
4
5
6
7
my $src = q:to/HAML/;
%section.container
  %h1 Title
  %h2 Subtitle
HAML

say HAML.render(:$src);

Where to go next

  • Tags — element names, sigils, class and id shorthand
  • Attributes — hash-style attribute syntax
  • Indentation — how nesting works
  • API — the public Raku API