Benjamin Carlsson
Benjamin Carlsson

Reputation: 610

How can I tell HAML to keep whitespace at the end of my view's lines?

I have some HAML views in my Rails project that are used to send files to the user. They're not rendered as HTML, just plain text files that get downloaded. These files have to match a very specific format, and the format has an idiosyncrasy that ends up requiring every second line to end with a tab character.

Line 0a\t01234
Line 0b\t
Line 1a\t12345
Line 1b\t
Line 2a\t23456
Line 2b\t

The a lines have have their tab characters printed fine, but the b lines do not. If I add any non-whitespace characters after the tab character, the tab gets printed. But when it's the last character on the line, it does not.

My view looks like

- @line_pairs.each do |line_pair|
  = line_pair.a.words + "\t" + line_pair.a.numbers
  = line_pair.b.words + "\t"

I'm sure that the tab character is not there (my editor shows them visually). There also is no space or anything of the like. I just get

Line 0a\t01234
Line 0b
Line 1a\t12345
Line 1b
Line 2a\t23456
Line 2b

Is there any way to fix this? Thanks for any help.

Upvotes: 0

Views: 618

Answers (3)

Benjamin Carlsson
Benjamin Carlsson

Reputation: 610

The proper solution as pointed out by matt is actually to just use ERB, as HAML is not meant to fill needs around controlling whitespace at this level of detail.

Upvotes: 0

sam
sam

Reputation: 40668

Try :preserve

:preserve
    - @line_pairs.each do |line_pair|

Upvotes: 0

carols10cents
carols10cents

Reputation: 7014

The haml documentation says that tilde (~) acts just like = but preserves whitespace.

Does this work?

- @line_pairs.each do |line_pair|
  ~ line_pair.a.words + "\t" + line_pair.a.numbers
  ~ line_pair.b.words + "\t"

Upvotes: 3

Related Questions