haml_tag and autoclosing elements

76 views
Skip to first unread message

treybean

unread,
Apr 11, 2008, 12:18:20 PM4/11/08
to Haml
I'm running the near latest haml 1.9 and just came across an oddity
with how the new haml_tag works.

If I do:

haml_tag :div, :class=>'clearfloat'

it generates:

<div class="clearfloat"/>

a closed div tag instead of:
<div class="clearfloat"></div>

Is this just to get people to stop putting crappy markup in their
html? It's something I'm trying to get away from, but the app I'm
working on has a bunch of that still going on.

What's the thinking on this? I've figured out the temporary fix of
haml_tag :div, :class=>'clearfloat' do end

but I'd be happier dropping the do end.

Thanks,
Trey

Mislav Marohnić

unread,
Apr 11, 2008, 12:41:21 PM4/11/08
to ha...@googlegroups.com

On Fri, Apr 11, 2008 at 6:18 PM, treybean <trey...@gmail.com> wrote:


If I do:

haml_tag :div, :class=>'clearfloat'

it generates:

<div class="clearfloat"/>

In XHTML, this is perfectly valid, safe and legal. But, if you're serving your pages as text/html (which you are because IE doesn't support XHTML), this would be parsed like an opening tag for DIV and the tag soup parser will continue to search for what it thinks could be the closing tag. In most cases, the document would render all messed-up (depending on your CSS, mostly).

You can use this:

  haml_tag :div, nil, :class=>'clearfloat'

- Mislav

treybean

unread,
Apr 11, 2008, 1:00:31 PM4/11/08
to Haml
Thanks Mislav. That makes sense. Yeah, if we could use true XHMTL
the world would be a nicer place. One day!

On Apr 11, 10:41 am, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:

treybean

unread,
Apr 11, 2008, 1:11:52 PM4/11/08
to Haml
Actually, I just tried your line: haml_tag :div,
nil, :class=>'clearfloat'
but I got a haml error

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.collect

vendor/gems/haml-1.9.0/lib/haml/precompiler.rb:470:in
`build_attributes'
vendor/gems/haml-1.9.0/lib/haml/helpers.rb:295:in `haml_tag'

It's no big deal, I can go back to the other way, but if people are
trying to do this with 1.9 maybe there should be an option to use an
HTML self-closing tags list to check if the element should be self-
closed.

Nathan Weizenbaum

unread,
Apr 11, 2008, 1:12:39 PM4/11/08
to ha...@googlegroups.com
I just updated master so that haml_tag will only close elements that are
listed in the :autoclose option.

Mislav Marohnić

unread,
Apr 11, 2008, 1:23:38 PM4/11/08
to ha...@googlegroups.com

On Fri, Apr 11, 2008 at 7:12 PM, Nathan Weizenbaum <nex...@gmail.com> wrote:

I just updated master so that haml_tag will only close elements that are
listed in the :autoclose option.

Cool!

Treybean: yeah I was wrong with the nil I suggested you. But if you replace nil with empty string, that will work. Or you could update to edge Haml (thanks, Nathan!)

treybean

unread,
Apr 11, 2008, 1:33:56 PM4/11/08
to Haml
Totally updating. Thanks for the update Nathan. I'm sure this will
make for an easier transition for people once the doors really open to
1.9.

Cheers all,
Trey

On Apr 11, 11:23 am, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages