Markup
Introduction
Haml templates: indentation-based markup, attributes, Ruby code, and filters.
- Haml reference (haml.info)
- Haml website (haml.info)
- Haml on GitHub (github.com)
Doctype
Tags
%html
%head
%title My page
%body
%h1 Hello World
%br
Indent two spaces per level and keep it consistent; void elements like %br need no trailing slash.
See: HTML elements
Classes and IDs
%p.note
%div#hero
%h1.title.big#hero Hello
.note Defaults to a div
. adds a class and # sets an id; a line with no tag name renders a div.
Attributes
Inline attributes
Ruby hash
%meta{
name: "viewport",
content: "width=device-width, initial-scale=1" }
HTML style
%a(href="/about" title="About") About
Both forms are current: the hash evaluates Ruby, parentheses stay close to HTML.
See: Attributes
Boolean attributes
%input{ type: "checkbox", checked: true }
%input{ type: "checkbox", checked: false }
%input(type="checkbox" checked)
true renders the bare attribute and false leaves it out.
See: Boolean attributes
Data attributes
%a{ href: "/posts", data: { author_id: 123 } } Posts
Hashes under data and aria expand to hyphenated attributes, so this renders data-author-id="123".
See: Data attributes
Ruby
Comments
Code and output
- @items = Item.all
- if @items.any?
= render @items
- else
%p No items yet
- runs Ruby and outputs nothing; = also inserts the result.
See: Ruby evaluation
Interpolation and escaping
%p= @user.name
%p Hello #{@user.name}
%p&= @user.bio
%p!= @user.raw_html
&= always escapes and != never does; plain = follows the :escape_html option.
See: Ruby interpolation
Filters
:javascript
console.log("hi")
:javascript and :css wrap the block in a tag; :plain and :escaped leave it as text.
See: Filters
0 Comments for this cheatsheet. Write yours!