SlideShare a Scribd company logo
1 of 23
Download to read offline
How’s Haml?
Patrick Reagan
What is Haml?

• Templating language for creating XHTML
• Alternative to ERB in Rails
• Principle: “Markup should be beautiful”
What is Beauty?
What is Beauty?
Installation
$ sudo gem install --no-ri haml
Successfully installed haml-1.8.0
1 gem installed
Installing RDoc documentation for haml-1.8.0...




$ haml --rails ./my_rails_app
Haml plugin added to ./my_rails_app
Using Haml
 Rename views and layouts

app/views/posts: index.html.erb => index.html.haml
app/views/layouts: application.html.erb => application.haml




  Start deleting!
ERB / RHTML
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Yet Another Rails Blog</title>
  </head>
  <body>
    <div id='container'>
      <div id='navigation'>
        <%= link_to 'Home', root_path %>
      </div>
      <div id='content'>
        <% @posts.each do |post| -%>
         <div class='post'>
           <h3><%= h post.title %></h3>
           <strong>by: <%= post.author %></strong>
           <p><%= post.body %></p>
         </div>
        <% end %>
      </div>
    </div>
  </body>
</html>
ERB / RHTML
<?xml version='1.0' encoding='utf-8' ?>
!!! XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
!!! Strict
%html{html_attrs}      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Yet Another Rails Blog</title>
  </head>
  <body>
    <div id='container'>
      <div id='navigation'>
        <%= link_to 'Home', root_path %>
      </div>
      <div id='content'>
        <% @posts.each do |post| -%>
         <div class='post'>
           <h3><%= h post.title %></h3>
           <strong>by: <%= post.author %></strong>
           <p><%= post.body %></p>
         </div>
        <% end %>
      </div>
    </div>
  </body>
</html>
ERB / RHTML
<?xml version='1.0' encoding='utf-8' ?>
!!! XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
!!! Strict
%html{html_attrs}      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
  %head
  <head>
    %title Yet Another Rails Blog
    <title>Yet Another Rails Blog</title>
  %body
  </head>
  <body>
    <div id='container'>
      <div id='navigation'>
        <%= link_to 'Home', root_path %>
      </div>
      <div id='content'>
        <% @posts.each do |post| -%>
         <div class='post'>
           <h3><%= h post.title %></h3>
           <strong>by: <%= post.author %></strong>
           <p><%= post.body %></p>
         </div>
        <% end %>
      </div>
    </div>
  </body>
</html>
ERB / RHTML
<?xml version='1.0' encoding='utf-8' ?>
!!! XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
!!! Strict
%html{html_attrs}      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
  %head
  <head>
    %title Yet Another Rails Blog
    <title>Yet Another Rails Blog</title>
  %body
  </head>
  <body>
    #container
    <div id='container'>
      #navigation
      <div id='navigation'>
         = link_to('Home', root_path)
        <%= link_to 'Home', root_path %>
      </div>
      <div id='content'>
        <% @posts.each do |post| -%>
         <div class='post'>
           <h3><%= h post.title %></h3>
           <strong>by: <%= post.author %></strong>
           <p><%= post.body %></p>
         </div>
        <% end %>
      </div>
    </div>
  </body>
</html>
Hamlified!
<?xml version='1.0' encoding='utf-8' ?>
!!! XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
!!! Strict
%html{html_attrs}      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
  %head
  <head>
    %title Yet Another Rails Blog
    <title>Yet Another Rails Blog</title>
  %body
  </head>
  <body>
    #container
    <div id='container'>
      #navigation
      <div id='navigation'>
         = link_to('Home', root_path)
        <%= link_to 'Home', root_path %>
      </div>
      #content
      <div id='content'>
        <%@posts.each do |post| -%>
         - @posts.each do |post|
           .post
         <div class='post'>
           <h3><%=h(post.title) %></h3>
             %h3= h post.title
           <strong>by:"by: post.author %></strong>
             %strong= <%= #{h(post.author)}"
           <p><%=post.body %></p>
             %p= post.body
         </div>
        <% end %>
      </div>
    </div>
  </body>
</html>
Haml Deconstructed
    !!! XML
    !!! Strict

    %html{html_attrs}
      %head
        %title Yet Another Rails Blog
      %body
        #container
          #navigation
            = link_to('Home', root_path)
          #content
            - @posts.each do |post|
              .post
                %h3= h(post.title)
                %strong= "by: #{h(post.author)}"
                %p= post.body
Haml Deconstructed
XHTML document type
                      !!! XML
                      !!! Strict

                      %html{html_attrs}
                        %head
                          %title Yet Another Rails Blog
                        %body
                          #container
                            #navigation
                              = link_to('Home', root_path)
                            #content
                              - @posts.each do |post|
                                .post
                                  %h3= h(post.title)
                                  %strong= "by: #{h(post.author)}"
                                  %p= post.body
Haml Deconstructed
XHTML document type
                      !!! XML
  XHTML tag with      !!! Strict

   static content     %html{html_attrs}
                        %head
                          %title Yet Another Rails Blog
                        %body
                          #container
                            #navigation
                              = link_to('Home', root_path)
                            #content
                              - @posts.each do |post|
                                .post
                                  %h3= h(post.title)
                                  %strong= "by: #{h(post.author)}"
                                  %p= post.body
Haml Deconstructed
XHTML document type                    Haml helper method
                      !!! XML
  XHTML tag with      !!! Strict

   static content     %html{html_attrs}
                        %head
                          %title Yet Another Rails Blog
                        %body
                          #container
                            #navigation
                              = link_to('Home', root_path)
                            #content
                              - @posts.each do |post|
                                .post
                                  %h3= h(post.title)
                                  %strong= "by: #{h(post.author)}"
                                  %p= post.body
Haml Deconstructed
XHTML document type                    Haml helper method
                      !!! XML
  XHTML tag with      !!! Strict

   static content     %html{html_attrs}
                        %head
                          %title Yet Another Rails Blog
   DIV tag with ID      %body
                          #container
                            #navigation
                              = link_to('Home', root_path)
                            #content
                              - @posts.each do |post|
                                .post
                                  %h3= h(post.title)
                                  %strong= "by: #{h(post.author)}"
                                  %p= post.body
Haml Deconstructed
XHTML document type                    Haml helper method
                      !!! XML
  XHTML tag with      !!! Strict

   static content     %html{html_attrs}
                        %head
                          %title Yet Another Rails Blog
   DIV tag with ID      %body
                          #container
                            #navigation
                              = link_to('Home', root_path)
 Execute Ruby code          #content
                              - @posts.each do |post|
                                .post
                                  %h3= h(post.title)
                                  %strong= "by: #{h(post.author)}"
                                  %p= post.body
Haml Deconstructed
XHTML document type                    Haml helper method
                      !!! XML
  XHTML tag with      !!! Strict

   static content     %html{html_attrs}
                        %head
                          %title Yet Another Rails Blog
   DIV tag with ID      %body
                          #container
                            #navigation
                              = link_to('Home', root_path)
 Execute Ruby code          #content
                              - @posts.each do |post|
                                .post
                                  %h3= h(post.title)
  XHTML tag with                  %strong= "by: #{h(post.author)}"
                                  %p= post.body
  dynamic content
XHTML Output
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Yet Another Rails Blog</title>
  </head>
  <body>
    <div id='container'>
      <div id='navigation'>
         <a href="/">Home</a>
      </div>
      <div id='content'>
         <div class='post'>
           <h3>January Refresh Recap</h3>
           <strong>by: Patrick</strong>
           <p>It was awesome!</p>
         </div>

        ...

      </div>
    </div>
  </body>
</html>
Off the Rails?

$ irb
>> require 'rubygems'
=> true
>> require 'haml'
=> true
>> Haml::Engine.new('%p Hello, World').render
=> "<p>Hello, World</p>n"
Why Use It?

• Creates well-formatted markup
• Automatic closing of tags
• Less “noise”
Why Not?

• Buildout must happen inside Rails
• XHTML is a widespread standard
• Performance
Resources
Other templating systems
 •   Erubis
 •   Markaby
 •   pHAML

Tutorial & Documentation
 •   http://haml.hamptoncatlin.com/tutorial/
 •   http://haml.hamptoncatlin.com/docs/

TextMate Bundle (via SVN)
 •   http://macromates.com/svn/Bundles/trunk

More Related Content

What's hot

11 page-directive
11 page-directive11 page-directive
11 page-directive
snopteck
 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2
RORLAB
 

What's hot (12)

Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect ModelComprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
Comprehensive Browser Automation Solution using Groovy, WebDriver & Obect Model
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
Theme Kickstart
Theme KickstartTheme Kickstart
Theme Kickstart
 
11 page-directive
11 page-directive11 page-directive
11 page-directive
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Amp and higher computing science
Amp and higher computing scienceAmp and higher computing science
Amp and higher computing science
 
HTML 5 & CSS 3
HTML 5 & CSS 3HTML 5 & CSS 3
HTML 5 & CSS 3
 
Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracket
 
HTML5 Essentials
HTML5 EssentialsHTML5 Essentials
HTML5 Essentials
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
 
Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2Layouts and Rendering in Rails, Season 2
Layouts and Rendering in Rails, Season 2
 

Viewers also liked (6)

Coding With Confidence: Adding TDD to Your Toolset
Coding With Confidence: Adding TDD to Your ToolsetCoding With Confidence: Adding TDD to Your Toolset
Coding With Confidence: Adding TDD to Your Toolset
 
Mockfight! FlexMock vs. Mocha
Mockfight! FlexMock vs. MochaMockfight! FlexMock vs. Mocha
Mockfight! FlexMock vs. Mocha
 
Changing Your Mindset: Getting Started with Test-Driven Development
Changing Your Mindset: Getting Started with Test-Driven DevelopmentChanging Your Mindset: Getting Started with Test-Driven Development
Changing Your Mindset: Getting Started with Test-Driven Development
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance TestingMake Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance Testing
 
Better Functional Design through TDD
Better Functional Design through TDDBetter Functional Design through TDD
Better Functional Design through TDD
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Similar to Hows Haml?

How to create a basic template
How to create a basic templateHow to create a basic template
How to create a basic template
vathur
 
Zen codingcheatsheet
Zen codingcheatsheetZen codingcheatsheet
Zen codingcheatsheet
goldenveizer
 
Noticias Ducez
Noticias DucezNoticias Ducez
Noticias Ducez
matheus
 
Introduction to xhtml
Introduction to xhtmlIntroduction to xhtml
Introduction to xhtml
Dhairya Joshi
 
WDIM268 Week 6 (Summer 2010)
WDIM268 Week 6 (Summer 2010)WDIM268 Week 6 (Summer 2010)
WDIM268 Week 6 (Summer 2010)
Tyler Sticka
 

Similar to Hows Haml? (20)

Haml
HamlHaml
Haml
 
How to create a basic template
How to create a basic templateHow to create a basic template
How to create a basic template
 
Zen codingcheatsheet
Zen codingcheatsheetZen codingcheatsheet
Zen codingcheatsheet
 
Statische Websites in Rails 3.1
Statische Websites in Rails 3.1Statische Websites in Rails 3.1
Statische Websites in Rails 3.1
 
Simple Markdown with Ecto and Protocols
Simple Markdown with Ecto and ProtocolsSimple Markdown with Ecto and Protocols
Simple Markdown with Ecto and Protocols
 
Lecture1and2
Lecture1and2Lecture1and2
Lecture1and2
 
Django Templates
Django TemplatesDjango Templates
Django Templates
 
Html.docx
Html.docxHtml.docx
Html.docx
 
Noticias Ducez
Noticias DucezNoticias Ducez
Noticias Ducez
 
Introduction to xhtml
Introduction to xhtmlIntroduction to xhtml
Introduction to xhtml
 
Frfrfrf
FrfrfrfFrfrfrf
Frfrfrf
 
Html Workshop
Html WorkshopHtml Workshop
Html Workshop
 
Getting to know perch — and perch runway!
Getting to know perch — and perch runway!Getting to know perch — and perch runway!
Getting to know perch — and perch runway!
 
AttributesL3.pptx
AttributesL3.pptxAttributesL3.pptx
AttributesL3.pptx
 
WDIM268 Week 6 (Summer 2010)
WDIM268 Week 6 (Summer 2010)WDIM268 Week 6 (Summer 2010)
WDIM268 Week 6 (Summer 2010)
 
Introdução a CSS
Introdução a CSS Introdução a CSS
Introdução a CSS
 
Html full
Html fullHtml full
Html full
 
Java script and html new
Java script and html newJava script and html new
Java script and html new
 
Java script and html
Java script and htmlJava script and html
Java script and html
 
Html goodies
Html goodiesHtml goodies
Html goodies
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Hows Haml?

  • 2. What is Haml? • Templating language for creating XHTML • Alternative to ERB in Rails • Principle: “Markup should be beautiful”
  • 5. Installation $ sudo gem install --no-ri haml Successfully installed haml-1.8.0 1 gem installed Installing RDoc documentation for haml-1.8.0... $ haml --rails ./my_rails_app Haml plugin added to ./my_rails_app
  • 6. Using Haml Rename views and layouts app/views/posts: index.html.erb => index.html.haml app/views/layouts: application.html.erb => application.haml Start deleting!
  • 7. ERB / RHTML <?xml version='1.0' encoding='utf-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body> </html>
  • 8. ERB / RHTML <?xml version='1.0' encoding='utf-8' ?> !!! XML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" !!! Strict %html{html_attrs} "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body> </html>
  • 9. ERB / RHTML <?xml version='1.0' encoding='utf-8' ?> !!! XML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" !!! Strict %html{html_attrs} "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> %head <head> %title Yet Another Rails Blog <title>Yet Another Rails Blog</title> %body </head> <body> <div id='container'> <div id='navigation'> <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body> </html>
  • 10. ERB / RHTML <?xml version='1.0' encoding='utf-8' ?> !!! XML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" !!! Strict %html{html_attrs} "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> %head <head> %title Yet Another Rails Blog <title>Yet Another Rails Blog</title> %body </head> <body> #container <div id='container'> #navigation <div id='navigation'> = link_to('Home', root_path) <%= link_to 'Home', root_path %> </div> <div id='content'> <% @posts.each do |post| -%> <div class='post'> <h3><%= h post.title %></h3> <strong>by: <%= post.author %></strong> <p><%= post.body %></p> </div> <% end %> </div> </div> </body> </html>
  • 11. Hamlified! <?xml version='1.0' encoding='utf-8' ?> !!! XML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" !!! Strict %html{html_attrs} "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> %head <head> %title Yet Another Rails Blog <title>Yet Another Rails Blog</title> %body </head> <body> #container <div id='container'> #navigation <div id='navigation'> = link_to('Home', root_path) <%= link_to 'Home', root_path %> </div> #content <div id='content'> <%@posts.each do |post| -%> - @posts.each do |post| .post <div class='post'> <h3><%=h(post.title) %></h3> %h3= h post.title <strong>by:"by: post.author %></strong> %strong= <%= #{h(post.author)}" <p><%=post.body %></p> %p= post.body </div> <% end %> </div> </div> </body> </html>
  • 12. Haml Deconstructed !!! XML !!! Strict %html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body
  • 13. Haml Deconstructed XHTML document type !!! XML !!! Strict %html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body
  • 14. Haml Deconstructed XHTML document type !!! XML XHTML tag with !!! Strict static content %html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body
  • 15. Haml Deconstructed XHTML document type Haml helper method !!! XML XHTML tag with !!! Strict static content %html{html_attrs} %head %title Yet Another Rails Blog %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body
  • 16. Haml Deconstructed XHTML document type Haml helper method !!! XML XHTML tag with !!! Strict static content %html{html_attrs} %head %title Yet Another Rails Blog DIV tag with ID %body #container #navigation = link_to('Home', root_path) #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body
  • 17. Haml Deconstructed XHTML document type Haml helper method !!! XML XHTML tag with !!! Strict static content %html{html_attrs} %head %title Yet Another Rails Blog DIV tag with ID %body #container #navigation = link_to('Home', root_path) Execute Ruby code #content - @posts.each do |post| .post %h3= h(post.title) %strong= "by: #{h(post.author)}" %p= post.body
  • 18. Haml Deconstructed XHTML document type Haml helper method !!! XML XHTML tag with !!! Strict static content %html{html_attrs} %head %title Yet Another Rails Blog DIV tag with ID %body #container #navigation = link_to('Home', root_path) Execute Ruby code #content - @posts.each do |post| .post %h3= h(post.title) XHTML tag with %strong= "by: #{h(post.author)}" %p= post.body dynamic content
  • 19. XHTML Output <?xml version='1.0' encoding='utf-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang='en-US' xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Yet Another Rails Blog</title> </head> <body> <div id='container'> <div id='navigation'> <a href="/">Home</a> </div> <div id='content'> <div class='post'> <h3>January Refresh Recap</h3> <strong>by: Patrick</strong> <p>It was awesome!</p> </div> ... </div> </div> </body> </html>
  • 20. Off the Rails? $ irb >> require 'rubygems' => true >> require 'haml' => true >> Haml::Engine.new('%p Hello, World').render => "<p>Hello, World</p>n"
  • 21. Why Use It? • Creates well-formatted markup • Automatic closing of tags • Less “noise”
  • 22. Why Not? • Buildout must happen inside Rails • XHTML is a widespread standard • Performance
  • 23. Resources Other templating systems • Erubis • Markaby • pHAML Tutorial & Documentation • http://haml.hamptoncatlin.com/tutorial/ • http://haml.hamptoncatlin.com/docs/ TextMate Bundle (via SVN) • http://macromates.com/svn/Bundles/trunk