SlideShare uma empresa Scribd logo
1 de 18
HAML in 5 minutes

     Cameron Hill
markup should be beautiful
HTML:
<div id="article">
  <h2>OxiClean tackles household stains</h2>
  <span class="author">Billy Mays</span>

  <p>It boosts your detergent power</p>
  <a href="oxiclean.com">Read more</a>
</div>




HAML:
#article
  %h2 OxiClean tackles household stains
  %span.author Billy Mays

 %p It boots your detergent power
 %a{:href => "oxiclean.com"} Read more
Four Simple Rules
•   %tag creates a <tag></tag> element
•   HTML attributes as Ruby hashes
•   CSS selector shortcuts & implicit divs
•   Nested elements: Indentation matters
%tag creates a <tag></tag> element

%div Some content         <div>Some content</div>

%h2 Breaking News         <h2>Breaking News</h2>

%br                       <br/>



%widget                   <widget></widget>
HTML attributes as hashes
%a{ :href => “/article/4”, :target=> “_blank” } Read more

<a href=“/article/4” target=“_blank”>Read more</a>




%ul{ :id => "projects", :class => "secondary" }

<ul id=“projects” class=“secondary”></ul>
CSS id and class shortcuts
%h2#title Rails in Action

<h2 id=“title”>Rails in Action</h2>




%span.author Steve Klabnik

<span class="author">Steve Klabnik</span>
Implicit DIV tags
#content This is my content

<div id="content">This is my content</div>




.article This is my content

<div class=”article">This is my content</div>
Nested elements: Indentation matters

HAML:
%ul
  %li Mayonnaise
  %li Mustard
  %li
    %a{ :href => “ketchup.com” } Ketchup



HTML:
<ul>
  <li>Mayonnaise</li>
  <li>Mustard</li>
  <li><a href=“ketchup.com”>Ketchup</a></li>
Evaluating Ruby with HAML
= evaluates a Ruby expression (like <%= %>)
ERB:
<h2><%= @ticket.title %></h2>

HAML:
%h2= @ticket.title




ERB:
<span>Created by <%= @ticket.user.email %></span>

HAML:
%span= “Created by #{@ticket.user.email}”

%span Created by #{@ticket.user.email}
Blocks in HAML
        -or-
Death to <% end %>
- Indicates a conditional or block
ERB:
<% if @ticket.comments.exists? %>
  <%= render @ticket.comments.select(&:persisted?) %>
<% else %>
  <p>There are no comments for this ticket.</p>
<% end %>




HAML:
- if @ticket.comments.exists?
  = render @ticket.comments.select(&:persisted?)
- else
  %p There are no comments for this ticket.
ERB:
<% @ticket.assets.each do |asset| %>
  <p>
     <%= link_to File.basename(asset.file_name), asset.url %>
  </p>
<% end %>




HAML:
- @ticket.assets.each do |asset|
  %p= link_to File.basename(asset.file_name), asset.url
I’m sold!
How do I use it in Rails?
gem “haml”
http://haml.info
Thanks!

    cameronhill.net
github.com/cameronbot
     @cameronbot

Mais conteúdo relacionado

Mais procurados

Copycopter Presentation by Joe Ferris at BostonRB
Copycopter Presentation by Joe Ferris at BostonRBCopycopter Presentation by Joe Ferris at BostonRB
Copycopter Presentation by Joe Ferris at BostonRB
bostonrb
 
Intro to java script
Intro to java scriptIntro to java script
Intro to java script
londiem
 

Mais procurados (19)

Why Django for Web Development
Why Django for Web DevelopmentWhy Django for Web Development
Why Django for Web Development
 
programming
programmingprogramming
programming
 
Shell
ShellShell
Shell
 
Function calling in js
Function calling in jsFunction calling in js
Function calling in js
 
7. copy2 in Symfony 4
7. copy2 in Symfony 47. copy2 in Symfony 4
7. copy2 in Symfony 4
 
Mechanize at the Ruby Drink-up of Sophia, November 2011
Mechanize at the Ruby Drink-up of Sophia, November 2011Mechanize at the Ruby Drink-up of Sophia, November 2011
Mechanize at the Ruby Drink-up of Sophia, November 2011
 
jQuery Presentation to Rails Developers
jQuery Presentation to Rails DevelopersjQuery Presentation to Rails Developers
jQuery Presentation to Rails Developers
 
Built in filters
Built in filtersBuilt in filters
Built in filters
 
Copycopter Presentation by Joe Ferris at BostonRB
Copycopter Presentation by Joe Ferris at BostonRBCopycopter Presentation by Joe Ferris at BostonRB
Copycopter Presentation by Joe Ferris at BostonRB
 
Intro to java script
Intro to java scriptIntro to java script
Intro to java script
 
HTML 5 Basics Part One
HTML 5 Basics Part OneHTML 5 Basics Part One
HTML 5 Basics Part One
 
Telling Stories With RSpec
Telling Stories With RSpecTelling Stories With RSpec
Telling Stories With RSpec
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrong
 
Entry-level PHP for WordPress
Entry-level PHP for WordPressEntry-level PHP for WordPress
Entry-level PHP for WordPress
 
Optimizing AngularJS Application
Optimizing AngularJS ApplicationOptimizing AngularJS Application
Optimizing AngularJS Application
 
URUG Ruby on Rails Workshop - Sesssion 5
URUG Ruby on Rails Workshop - Sesssion 5URUG Ruby on Rails Workshop - Sesssion 5
URUG Ruby on Rails Workshop - Sesssion 5
 
Experience Manager 6 Developer Features - Highlights
Experience Manager 6 Developer Features - HighlightsExperience Manager 6 Developer Features - Highlights
Experience Manager 6 Developer Features - Highlights
 
Rails for Beginners - Le Wagon
Rails for Beginners - Le WagonRails for Beginners - Le Wagon
Rails for Beginners - Le Wagon
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
 

Destaque

Submissão à autoridad1
Submissão à autoridad1Submissão à autoridad1
Submissão à autoridad1
mgno42
 

Destaque (20)

Ensenyament 2012
Ensenyament 2012Ensenyament 2012
Ensenyament 2012
 
Virus y vacunas informã¡ticas laura
Virus y vacunas informã¡ticas lauraVirus y vacunas informã¡ticas laura
Virus y vacunas informã¡ticas laura
 
Jahir Hussain
Jahir HussainJahir Hussain
Jahir Hussain
 
Workcube CRM Fonksiyon Föyü
Workcube CRM Fonksiyon FöyüWorkcube CRM Fonksiyon Föyü
Workcube CRM Fonksiyon Föyü
 
Rsvp
RsvpRsvp
Rsvp
 
G te c sesion4b-complejidad y tpa
G te c sesion4b-complejidad y tpaG te c sesion4b-complejidad y tpa
G te c sesion4b-complejidad y tpa
 
Trastornos del aprendizaje
Trastornos del aprendizajeTrastornos del aprendizaje
Trastornos del aprendizaje
 
Miniquest.Informática, 2013.
Miniquest.Informática, 2013.Miniquest.Informática, 2013.
Miniquest.Informática, 2013.
 
Submissão à autoridad1
Submissão à autoridad1Submissão à autoridad1
Submissão à autoridad1
 
Financial densities in emerging markets
Financial densities in emerging marketsFinancial densities in emerging markets
Financial densities in emerging markets
 
Cross-promotion of iPad apps
Cross-promotion of iPad appsCross-promotion of iPad apps
Cross-promotion of iPad apps
 
Flormar 7-8
Flormar 7-8Flormar 7-8
Flormar 7-8
 
The Four Stage Link Building Cycle: Page One Power Webinar
The Four Stage Link Building Cycle: Page One Power WebinarThe Four Stage Link Building Cycle: Page One Power Webinar
The Four Stage Link Building Cycle: Page One Power Webinar
 
Los cinco sentidos
Los cinco sentidosLos cinco sentidos
Los cinco sentidos
 
proyecto de integración de las tics en la educación inicial.
proyecto de integración de las tics en la educación inicial.proyecto de integración de las tics en la educación inicial.
proyecto de integración de las tics en la educación inicial.
 
Agenda Escolar de Diabetes
Agenda Escolar de DiabetesAgenda Escolar de Diabetes
Agenda Escolar de Diabetes
 
Mateo Valero - Big data: de la investigación científica a la gestión empresarial
Mateo Valero - Big data: de la investigación científica a la gestión empresarialMateo Valero - Big data: de la investigación científica a la gestión empresarial
Mateo Valero - Big data: de la investigación científica a la gestión empresarial
 
El Agua
El AguaEl Agua
El Agua
 
8683476 assimil-njemacki-bez-muke-1974
8683476 assimil-njemacki-bez-muke-19748683476 assimil-njemacki-bez-muke-1974
8683476 assimil-njemacki-bez-muke-1974
 
Plan Nacional de Riego en Argentina
Plan Nacional de Riego en ArgentinaPlan Nacional de Riego en Argentina
Plan Nacional de Riego en Argentina
 

Semelhante a Haml in 5 minutes

Introduction to HTML5
Introduction to HTML5Introduction to HTML5
Introduction to HTML5
Terry Ryan
 

Semelhante a Haml in 5 minutes (20)

HTML5 & CSS3 Flag
HTML5 & CSS3 FlagHTML5 & CSS3 Flag
HTML5 & CSS3 Flag
 
HTML and CSS crash course!
HTML and CSS crash course!HTML and CSS crash course!
HTML and CSS crash course!
 
Advanced CSS Troubleshooting & Efficiency
Advanced CSS Troubleshooting & EfficiencyAdvanced CSS Troubleshooting & Efficiency
Advanced CSS Troubleshooting & Efficiency
 
HTML CSS and Web Development
HTML CSS and Web DevelopmentHTML CSS and Web Development
HTML CSS and Web Development
 
Html intro
Html introHtml intro
Html intro
 
Frontend for developers
Frontend for developersFrontend for developers
Frontend for developers
 
Introduction to HTML5
Introduction to HTML5Introduction to HTML5
Introduction to HTML5
 
Advanced CSS Troubleshooting
Advanced CSS TroubleshootingAdvanced CSS Troubleshooting
Advanced CSS Troubleshooting
 
SharePointfest Denver - A jQuery Primer for SharePoint
SharePointfest Denver -  A jQuery Primer for SharePointSharePointfest Denver -  A jQuery Primer for SharePoint
SharePointfest Denver - A jQuery Primer for SharePoint
 
Theming websites effortlessly with Deliverance (CMSExpo 2010)
Theming websites effortlessly with Deliverance (CMSExpo 2010)Theming websites effortlessly with Deliverance (CMSExpo 2010)
Theming websites effortlessly with Deliverance (CMSExpo 2010)
 
HTML Coding #01 : Don't Fear the Code
HTML Coding #01 : Don't Fear the CodeHTML Coding #01 : Don't Fear the Code
HTML Coding #01 : Don't Fear the Code
 
TOSSUG HTML5 讀書會 新標籤與表單
TOSSUG HTML5 讀書會 新標籤與表單TOSSUG HTML5 讀書會 新標籤與表單
TOSSUG HTML5 讀書會 新標籤與表單
 
What is HTML - An Introduction to HTML (Hypertext Markup Language)
What is HTML - An Introduction to HTML (Hypertext Markup Language)What is HTML - An Introduction to HTML (Hypertext Markup Language)
What is HTML - An Introduction to HTML (Hypertext Markup Language)
 
Seo Cheat Sheet
Seo Cheat SheetSeo Cheat Sheet
Seo Cheat Sheet
 
Seo cheat-sheet
Seo cheat-sheetSeo cheat-sheet
Seo cheat-sheet
 
Html5
Html5Html5
Html5
 
Pamela - Brining back the pleasure of hand-written HTML - Montréal Python 8
Pamela - Brining back the pleasure of hand-written HTML - Montréal Python 8Pamela - Brining back the pleasure of hand-written HTML - Montréal Python 8
Pamela - Brining back the pleasure of hand-written HTML - Montréal Python 8
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
3 1-html-fundamentals-110302100520-phpapp02
3 1-html-fundamentals-110302100520-phpapp023 1-html-fundamentals-110302100520-phpapp02
3 1-html-fundamentals-110302100520-phpapp02
 
Intro to HTML and CSS
Intro to HTML and CSSIntro to HTML and CSS
Intro to HTML and CSS
 

Último

Último (20)

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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL 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...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

Haml in 5 minutes

  • 1. HAML in 5 minutes Cameron Hill
  • 2. markup should be beautiful
  • 3. HTML: <div id="article"> <h2>OxiClean tackles household stains</h2> <span class="author">Billy Mays</span> <p>It boosts your detergent power</p> <a href="oxiclean.com">Read more</a> </div> HAML: #article %h2 OxiClean tackles household stains %span.author Billy Mays %p It boots your detergent power %a{:href => "oxiclean.com"} Read more
  • 4. Four Simple Rules • %tag creates a <tag></tag> element • HTML attributes as Ruby hashes • CSS selector shortcuts & implicit divs • Nested elements: Indentation matters
  • 5. %tag creates a <tag></tag> element %div Some content <div>Some content</div> %h2 Breaking News <h2>Breaking News</h2> %br <br/> %widget <widget></widget>
  • 6. HTML attributes as hashes %a{ :href => “/article/4”, :target=> “_blank” } Read more <a href=“/article/4” target=“_blank”>Read more</a> %ul{ :id => "projects", :class => "secondary" } <ul id=“projects” class=“secondary”></ul>
  • 7. CSS id and class shortcuts %h2#title Rails in Action <h2 id=“title”>Rails in Action</h2> %span.author Steve Klabnik <span class="author">Steve Klabnik</span>
  • 8. Implicit DIV tags #content This is my content <div id="content">This is my content</div> .article This is my content <div class=”article">This is my content</div>
  • 9. Nested elements: Indentation matters HAML: %ul %li Mayonnaise %li Mustard %li %a{ :href => “ketchup.com” } Ketchup HTML: <ul> <li>Mayonnaise</li> <li>Mustard</li> <li><a href=“ketchup.com”>Ketchup</a></li>
  • 11. = evaluates a Ruby expression (like <%= %>) ERB: <h2><%= @ticket.title %></h2> HAML: %h2= @ticket.title ERB: <span>Created by <%= @ticket.user.email %></span> HAML: %span= “Created by #{@ticket.user.email}” %span Created by #{@ticket.user.email}
  • 12. Blocks in HAML -or- Death to <% end %>
  • 13. - Indicates a conditional or block ERB: <% if @ticket.comments.exists? %> <%= render @ticket.comments.select(&:persisted?) %> <% else %> <p>There are no comments for this ticket.</p> <% end %> HAML: - if @ticket.comments.exists? = render @ticket.comments.select(&:persisted?) - else %p There are no comments for this ticket.
  • 14. ERB: <% @ticket.assets.each do |asset| %> <p> <%= link_to File.basename(asset.file_name), asset.url %> </p> <% end %> HAML: - @ticket.assets.each do |asset| %p= link_to File.basename(asset.file_name), asset.url
  • 15. I’m sold! How do I use it in Rails?
  • 18. Thanks! cameronhill.net github.com/cameronbot @cameronbot

Notas do Editor

  1. IntroductionMy talk today is HAML in 5 minutesHow many of you know what haml is?Haml is a templating markup language similar to Ruby ERB that you can use to code the views in your Rails Apps.Unlike ERB which basically just evaluates Ruby in between whatever text is around it – whether it’s HTML or CSS or JSON or whatever, HAML is abstracted out a little more actually replaces how you even write your html tags.Just like you have SASS or LESS for CSS, HAML has a syntax that ultimately compiles into HTML. It’s designed to make your life as a Rails developer easier.
  2. The makers of HAML decided that markup should be beautiful.They wanted something that was concise, elegant, and clear. What they came up with was something that borrowed a lot of style from Ruby code.For those of you that don’t like the context switch between Ruby code in your models and controllers and HTML and ERB in your views, you might really welcome HAMLSo what does HAML look like?
  3. Here is an HTML snippet at the top and how this would be written in HAML.Designed to be very easy to read. Almost intuitive to understand.Just looking at it you can probably pick out some patterns that make HAML work.#article something, looks like an h2, span, maybe that’s a css class name, paragraph, anchor tag with an href attribute
  4. No closing tags.Whatever is to the right of the %tagname becomes the content of that tagCan even create custom tags if you were marking up an XML document or were using a custom namespace
  5. Just like Ruby, pass in the attributes for your HTML tags as hashes
  6. Since css id and class are used so frequently there’s even a shortcut for them.You can even combine them and apply both at the same time or put a hash on the end and pass in even more attributes.
  7. Also since DIV tags are used so frequently in HTML, they are the default tag.Rather than writing %div#content, simply write #content
  8. Another really important aspect of HAML is that indentation matters!This is how you get around having to write closing tags.To add a child element, indent two spaces, just like Ruby. Here’s an example of how that looks --- notice there’s an anchor tag on the last list-item so it is indented another line
  9. Okay so this looks great, but those are the basics, lets get to some more complicated stuff....Evaluating Ruby with HAML
  10. For the next few slides I’m going to compare ERB to HAML so you can see the similarities and how you might go about converting your views to HAML.TIP for converting to HAML: keep hitting the delete key.So here is how we can evaluate ruby in HAML – use the equals sign after a tag to tell HAML to evaluate what is to the right.Here are some examples from our Ticketee project.In the second example, you can do string interpolation without the equals sign.
  11. And to get even more complicated, lets go over how to do blocks in HAML – and how to get rid of all those ERB end tags cluttering your code.
  12. Another example from Ticketee.Use a hypen at the beginning of the line to indicate it’s a block.Notice the equals sign all by itself for the render statement. You can do that without preceding it with a tag name.And of course the best thing is no &lt;% end %&gt; statements. I’m sure all of you have questioned why anyone would ever think a 9 character closing tag with no less than 4 special characters that require hitting the shift key was a good idea. Well suffer no more.
  13. Here’s an example with a loop.Simple!
  14. Well it wouldn’t be Rails worthy if it wasn’t extremely easy to add to your project.
  15. Add gem haml to your gemfile.And boom, you’re up and running.Anything in your views that have a .html.haml file extension will be compiled into HTML.
  16. That’s HAML.For more info, check out haml.info.