Gian Saß

Inline Links in HAML

· Gian Sass

Ruby on Rails becomes butthurt confusing on some things that ought to be simple. I use HAML a lot and get uncertain at times on how to do easy things, like putting a link in the middle of a paragraph.

1. HAML-way
%p
  You can visit us at
  %a{:href => "http://example.com"} this link
  anytime!

 

%p
  You can visit us at
  = link_to "this link", "http://example.com"
  anytime!

This both compiles to:

<p>
    You can visit us at
    <a href="http://example.com">this link</a>
    anytime!
</p>

The comma problem

Suppose you want to directly append a comma after a link while it not being part of it. This is where it get’s really confusing because HAML (or something else) automatically appends a whitespace before the comma, leaving you smashing your keyboard.

You need to do it like this:

%p
  You can visit us at
  = succeed "," do
    = link_to "this link", "http://example.com"
  anytime!

Want to throw up already? It seems increasingly clear that HAML is not good for content anyways, it’s only good for layouts and design. Read here for more information.