haml - don't use end tags

so I’m surprised I didn’t figure this out earlier… it was this message that finally drove the point home.

When using haml in your rails view templates, you do not need to close ruby blocks and conditional statements.

for example:

Here's how I was doing it before:

- content_for :head do
=   stylesheet_link_tag    'jquery.autocomplete'
=   javascript_include_tag 'jquery.autocomplete'
- end
- javascript_behaviour '$("input#user_full_name").autocomplete("project_roles/auto_complete_for_user_full_name")'

And here's how it really should be:

- content_for :head do
  = stylesheet_link_tag    'jquery.autocomplete'
  = javascript_include_tag 'jquery.autocomplete'
- javascript_behaviour '$("input#user_full_name").autocomplete("project_roles/auto_complete_for_user_full_name")'

So much nicer and readable, just as the haml gods intended :)

+1 for making ”- end” an error.

UPDATE:

On Oct 10, 2008, at 5:18 PM, Nathan Weizenbaum wrote: ”- end” actually is an error in the most recent Haml release, 2.0.3.

Leave a Reply