Chapter 12 - Haml Flashcards
Create an Element
To create an HTML element in Haml, one simply needs to prefix the percent character (%) to an element name. The element name can be any string, allowing you to use newly added HTML5 elements, such as header.
%header content
content header >
Attributes
Attributes in Haml are defined using two styles. The first style involves defining attributes between curly braces ({}).
%a{ title: @article.title, href: article_path(@ article) } Title
%a( title=@article.title href = article_path(@article)) Title
Haml Comment
-#
Evaluating Ruby code
Somewhat similar to ERb, using the equals character (=) results in Haml evaluating Ruby code following the character and outputting the result into the document.
%p = %w( foo bar). join(‘ ‘)
HTML foo bar p >
Alternatively, using the hyphen character (-) evaluates Ruby code but doesn’t insert its output into the resulting document. This is commonly used in combination with if/ else statements and loops.
- if flash.notice.
.notice= flash.notice
Interpolation
Ruby code can be interpolated in two ways in Haml: inline with plain text using #{} or using string interpolation in combination with =.
%p By: #{ post.author_name}
%p = “By: #{ post.author_name}”