Alex Ghiculescu
Alex Ghiculescu

Reputation: 7540

Removing whitespace in a block in Haml

I have this:

= link_to user_path(f.object.user) do
  %span.hourly-rate>= f.object.user.hourly_rate.to_currency
  \/hour

Which renders as:

<a href="/users/44"><span class='hourly-rate'>$16</span>/hour
</a>

The close tag is on a separate line. This results in the link looking funny when there is underlining (eg. on hover)

The underlining goes for one character too long

The solution is to have the markup look like:

<a href="/users/44"><span class='hourly-rate'>$16</span>/hour</a>

But I haven't found a way of doing that in Haml. I'd prefer to keep using the block form of link_to if possible (I imagine not using the block would result in even messier code, but at this point I'm open to anything).

Tangent: I imagine this would be fixed if I was using the :ugly Haml option. I have Haml::Template.options[:ugly] = true in my config/environments/development.rb, and I have haml-rails in my Gemfile (I know it doesn't include :ugly, but it's worth mentioning), and I have an initializer with;

# config/initializers/haml.rb
Haml::Template.options[:format] = :html5
Haml::Template.options[:ugly] = true

... but the code still isn't ugly :( Any ideas why not?

Upvotes: 0

Views: 637

Answers (2)

Alex Ghiculescu
Alex Ghiculescu

Reputation: 7540

The span was being used by javascript, to get its value (in this case, the user's hourly rate) - it wasn't being used for CSS at all.

Thus, I ended up removing the span and adding the user's hourly rate as a data- attribute of a separate field. So the final markup for the link was

= link_to f.object.user.hourly_rate.to_currency + "/hour", user_path(f.object.user)

Upvotes: 1

Jiř&#237; Posp&#237;šil
Jiř&#237; Posp&#237;šil

Reputation: 14401

If you can live with the content wrapped inside another div, you can use this:

= link_to user_path(f.object.user) do
  %div<>
    %span.hourly-rate>= f.object.user.hourly_rate.to_currency
    \/hour

Upvotes: 1

Related Questions