My basic mindset while coding a HAML file is that I don't want to code
any tags at all. I want it all to be HAML tags. At the same time, I
want my markup to be rendered pretty.
Keeping these two expectations in mind, my issue arises when I want to
render a line of HTML that has, say, a few words in a formatting tag. I
would write, in HAML:
%p This is a sentence that
%b has a few words
bolded.
Which would render out with the bolded segment on its own line. Not too
ugly, doesn't really bother me.
However, if you get into any sort of a more complicated example, things
can get messy quick:
%p
North America covers an area of about 24,490,000 km
%sup 2
(
%i 9,450,000 sq mi
)
renders as:
<p>
North America covers an area of about 24,490,000 km
<sup>2</sup>
(
<i>9,450,000 sq mi</i>
)
</p>
...which, to me, is uglier than:
North America covers an area of about 24,490,000
km<sup>2</sup>(<i>9,450,000 sq mi</i>)
Now, I realize this may just be the nature of writing markup, and I'm
prepared to accept that. The answer could be as simple as giving up my
never-hard-code-tags mentality and just mixing in those simple
formatting tags with HAML. But I have to ask, does anyone else share my
feelings about this, and if so, how do you address this concern when
writing HAML yourself?
However, there are several helpers built into Haml that should help
address this issue: precede, succeed, and surround. These allow you to
put text around chunks of Haml without adding newlines. For instance,
you might write your snippet as:
%p
North America covers an area of about 24,490,000
= precede 'km' do
%sup 2
= surround '(', ')' do
%i 9,450,000 sq mi
which would output
%p
North America covers an area of about 24,490,000
km<sup>2</sup>
(<i>9,450,000</i> sq mi)
- Nathan
%p
North America covers an area of about 24,490,000 km <sup>2</sup>
(<i>9,450,000 sq mi</i>)
For me, I'm totally fine with inline formatting tags being HTML. They
don't really bother me.
The main reason is that Haml is for structure, not really formatting.
Usually in an application you'd write
#continent
#information= @continent.description
And allow the db to drive that kind of data that would require
formatting.
However, in the few cases that's not possible, then I tend to just do
inline tags by hand.
Haml is for structure!
-hampton.