Haml in 5 minutes


Published on

Quick overview of HAML for templating views in Ruby on Rails.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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?
  • 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
  • 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
  • Just like Ruby, pass in the attributes for your HTML tags as hashes
  • 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.
  • Also since DIV tags are used so frequently in HTML, they are the default tag.Rather than writing %div#content, simply write #content
  • 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
  • Okay so this looks great, but those are the basics, lets get to some more complicated stuff....Evaluating Ruby with HAML
  • 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.
  • 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.
  • 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 <% end %> 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.
  • Here’s an example with a loop.Simple!
  • Well it wouldn’t be Rails worthy if it wasn’t extremely easy to add to your project.
  • 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.
  • That’s HAML.For more info, check out haml.info.
  • Haml in 5 minutes

    1. 1. HAML in 5 minutes Cameron Hill
    2. 2. markup should be beautiful
    3. 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. 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. 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. 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. 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. 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. 9. Nested elements: Indentation mattersHAML:%ul %li Mayonnaise %li Mustard %li %a{ :href => “ketchup.com” } KetchupHTML:<ul> <li>Mayonnaise</li> <li>Mustard</li> <li><a href=“ketchup.com”>Ketchup</a></li>
    10. 10. Evaluating Ruby with HAML
    11. 11. = evaluates a Ruby expression (like <%= %>)ERB:<h2><%= @ticket.title %></h2>HAML:%h2= @ticket.titleERB:<span>Created by <%= @ticket.user.email %></span>HAML:%span= “Created by #{@ticket.user.email}”%span Created by #{@ticket.user.email}
    12. 12. Blocks in HAML -or-Death to <% end %>
    13. 13. - Indicates a conditional or blockERB:<% 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. 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. 15. I’m sold!How do I use it in Rails?
    16. 16. gem “haml”
    17. 17. http://haml.info
    18. 18. Thanks! cameronhill.netgithub.com/cameronbot @cameronbot