Lance Pollard
Lance Pollard

Reputation: 79478

Whitespace Between Tags in HAML

Is there a way to specify "pretty-print"-like formatting around HTML tags? I want to be able to put whitespace between blocks of HTML, so this:

<!-- container -->
<div id='container'>
</div>
<!-- footer -->
<div id="footer">
</div>
<!-- analytics -->
...

...is converted to this:

<!-- container -->
<div id='container'>
</div>


<!-- footer -->
<div id="footer">
</div>


<!-- analytics -->
...

I know you can do comments with /, is there something like that for whitespace between tags? Maybe something like this

/ container
#container
\
\
/ footer
#footer
:s
:s
/ analytics

Where the \ or :s could be custom filters?

Or even something like = space(10) for 10 line breaks? Or maybe even ~ by itself but that doesn't work.

Upvotes: 3

Views: 1223

Answers (4)

Dmitry Polushkin
Dmitry Polushkin

Reputation: 3403

I wrote special filter for that kind of thing: https://gist.github.com/dmitry/6050231

To use is, you just need to add :s, like in your example:

#footer
:s
:s
/ analytics

Upvotes: 0

Pratik Ganvir
Pratik Ganvir

Reputation: 311

\#container  
= ('< br />'*5).html_safe 

\#footer

Upvotes: 0

Natalie Weizenbaum
Natalie Weizenbaum

Reputation: 5974

#container
- haml_concat("\n" * 5)
#footer

The haml_concat helper directly concatenates text onto the output buffer, without any sort of pre-processing.

Upvotes: 5

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24118

You can evaluate Ruby block to insert additional newlines:

.main
  .container
    %p Something

  ~ "\n" * 5

  .footer
    %p Footer

This also uses ~ - whitespace preservation.

Upvotes: 1

Related Questions