Layouts and pages
Props
The outermost +layout.haml is the whole document. Every layout and page
receives these props, which Haml reads with a $:
| Prop | Value |
|---|---|
$request | path, params and url of the page being rendered |
$stylesheets | URLs of the CSS the page and its layouts import |
$scripts | URLs of the JavaScript they import |
$children | the nested layout or page, in layouts only |
%html{ lang: "en" }
%head
%title My site
= $stylesheets.map do |href|
%link{ rel: "stylesheet", href: href }
= $scripts.map do |src|
%script{ type: "module", src: src }
%body
= $children
A page links only what it and its layouts import. The front page of this site does not load the stylesheet of a blog post.
Nested layouts
A +layout.haml in a subdirectory wraps the pages below it, inside the outer
layout. The list of pages beside this text comes from
src/routes/docs/+layout.haml:
:ruby
Docs = import("/docs")
%nav
= Docs.all.map do |page|
%a{ href: Docs.path_of(page) }= page::Frontmatter.fetch(:title)
%div
= $children
Titles
A page or layout can emit %title anywhere. The innermost one replaces the
layout’s, so each document has one <title>:
%title About
%h1 About
Metadata
%meta works like %title. A page, a component or a nested layout can write
a %meta anywhere, and it is moved into <head>. It takes the place of a
%meta there with the same name or property. So the layout holds the
defaults for the site:
%head
%title My site
%meta{ name: "description", content: "A site about things." }
%meta{ property: "og:type", content: "website" }
%link{ rel: "canonical", href: $request.url }
And a page replaces the ones it knows better:
%title= post::Frontmatter.fetch(:title)
%meta{ name: "description", content: post::Frontmatter.fetch(:summary) }
%meta{ property: "og:type", content: "article" }
A %link{ rel: "canonical" } moves the same way. A %meta with itemprop
stays where it is.
$request.url is the absolute URL of the page, built from :url in
src/site.rb. Open Graph needs absolute URLs for images too. Import the
image and join its src with the site’s URL:
:ruby
Site = import("/site")
Card = import("/card.png")
%meta{ property: "og:image", content: URI.join(Site.fetch(:url), Card.src).to_s }
This site wraps those tags in one component, src/components/Meta.haml.
Components
A Haml file outside routes/ is a component. Import it, and use it as a tag.
Its attributes arrive as props:
-# src/components/PostMeta.haml
%p
%time{ datetime: $date }= $date
:ruby
PostMeta = import("/components/PostMeta.haml")
%PostMeta{ date: "2026-12-01" }
Attribute names
Attribute names are written with dashes in the output. data_state and
"data-state" both become data-state. An attribute whose value is nil or
false is left out, and true writes the name alone.
Scripts
A page gets JavaScript by importing it. This site’s layout has
import("./site.ts"), and the compiled file is in $scripts. There is no
client runtime, and nothing is hydrated.