Haml breaks pre indentation

Haml has a feature called “Whitespace Preservation” which usually tries to nicely indent your markup. This is great.

However, sometimes it gets in your way – especially when it indents your <pre> paragraphs wrong.

I had a setup like

%p
  Here, some code!
  = render_some_code

where #render_some_code would return some markup.

def render_some_code
  "
include 'cells'ninclude 'apotomo' /pre>"
end

[The missing < before /pre is intentional as it breaks this writing, haha.]

I expected the render output to be ...nice.

Here, some code!
include 'cells'
include 'apotomo'

Nevertheless, it turned out to be even nicer. Haml's nice indentation striked.

Here, some code!
include 'cells'
  include 'apotomo'

I found this delicious post by Chrissy Eppstein and in turn read the haml docs on whitespace preservation.

Using Haml::Helpers#find_and_preserve, it works like a charm now. I don't know why, neither how, but it works.

include Haml::Helpers

def render_some_code
  find_and_preserve "
include 'cells'ninclude 'apotomo' /pre>"
end
Advertisement

One thought on “Haml breaks pre indentation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s