Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

HAML: A new view template language for Rails

By Peter Cooper / September 14, 2006

HAML is a new template language for Ruby on Rails developed by Hampton Catlin, a Canadian Rails developer. It's a high-level, heavily semantic language that breaks the mold of RHTML and makes it very hard to make markup errors. In a way similar to Python, HAML relies on indentation, which it uses to enforce DOM hierarchy.

Here's an example of some HAML code in action:

!!!
%html
  %head
    %title Client Admin Site
    %meta{"http-equiv"=>"Content-Type",
       :content=>"text/html; charset=utf-8"}/
    = stylesheet_link_tag 'tabbed'
    = javascript_include_tag 'tabbed'
  %body
    #application
      #header
        .container
          .statusbar
            .logo
              %strong Admin Interface
            .menu= link_to 'logout',
               :controller => 'account', :action => 'logout'
          %br{:style=>"clear:both;"}/
          .tabs
            %ul.navigation
              %li= link_to 'Member Approval', member_admin_url
              %li= link_to 'User Management', user_admin_url,
                 :class => 'selected'
              %li= link_to 'Pages', page_admin_url
              %li= link_to 'Reports', reports_url
              %li= link_to 'Help', '/'
      #page
        #content
          //These will only render if there is
             a non-false value returned from the helper
          #errors= print_flash(:error)
          #notice= print_flash(:notice)
          = @content_for_layout
        #sidebar= @content_for_sidebar || false
        %hr/
      #footer
        %p= "Copyright Hampton Catlin 2006"

Learn more in this article.

Comments

  1. ncalpunker says:

    Yuk! Yet another templating engine...when will people learn, lol.

  2. Chadwick says:

    This hurts my head. I have a hard time seeing what advantages this has of any other template engine.

    I'm still waiting for someone (myself?) to implement a TAL (http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL%20Specification%201.4) engine. If you're going to use a template engine, it might as well be nice valid XML.

  3. Ezra says:

    Actually, I think nice in its own way. The only real showstopper is that it does not handle multi line ruby statements. So you can't write the normal @foo.each multi line iteration. Not sure why they decided to do that but its fairly limiting unless I am missing something here.

  4. Jason says:

    I quite like this from the standpoint that I hate HTML and XHTML, but I think this will be hard for people to adopt only because it is yet another thing for someone to learn and there will be fewer examples. I'm not saying this is good, just the way it probably will be.

  5. Aaron Blohowiak says:

    I don't like this.

    Markaby http://markaby.rubyforge.org/ does this better, if you must.

  6. rubylicio.us says:

    Quite nice actually, I like how he got it simular to actuall CSS with "#" for uniqe ids, "." for classes and well, I guess % is ok for normal tags :). As ezra is pointing out it wouldn't be bad still having the possibilities.. or maybe it's there, I see some link_to's in there after ='s..

  7. Rimantas says:

    Oh, no... Looks like HARM to me...

  8. Jeroen says:

    I wonder how this performs! I can't imagine that it's quicker than the current template engine. Does anybody know this?

  9. evan says:

    I don't see much benefit to this over Markaby. With Markaby, your markup must validate as valid Ruby code, blocks and all, because it is real Ruby code. This means anything in Ruby is supported, you can't misnest anything, errors are caught early, and you don't have to learn any new syntax (ok, 30 seconds of new syntax: [ tag.idname!.classname ] == [ tag :id => 'idname', :class => 'classname' ]== [ ] ).

  10. evan says:

    WordPress swallowed my html tag example; sigh...

  11. Peter Cooper says:

    One thing I do like about it (whether for good or bad reasons) is the way you don't need to remember to close off a block.. but then I hit into one reason why I hate Python.

  12. Sarah says:

    Ugh, why try to turn any aspect of Ruby into Python? Awful, disgusting whitespace requirements...

  13. David Marko says:

    Would be great if someone could implement PageTemplates that Python has. Its great way how to embed code into tags and let designer goes his way.

  14. ncalpunker says:

    I agree with Chadwick about TAL. It has its good and bad, but if you are working with a designer and want to do round-trip design with something like dreamweaver, it is the best solution I have seen to date.

  15. John Philip Green says:

    More discussion can be found on the RoR-talk list: http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/52a625a3f379f73a/e61fa29c4a48ef67?

Other Posts to Enjoy

Twitter Mentions