View Helpers in Mixed Erb/Haml environment

326 views
Skip to first unread message

railsjedi

unread,
Nov 20, 2008, 1:40:11 AM11/20/08
to Haml
Hi. I'm trying to write some view helpers that take blocks. Running
into the issue that some views are HAML and some are ERB. So erb seems
to break haml_tag, and haml seems to break content_tag. Also concat vs
haml_concat, and capture vs capture_haml. All very confusing. Seems
like helpers should be halpers, and work in all contexts no matter the
view layer.

Is there an easy way to convert from one to the other? Maybe by just
wrapping it in a capture block or something?

Thanks

railsjedi

unread,
Nov 20, 2008, 2:18:04 AM11/20/08
to Haml
Or does anyone know of a similar implementation of haml_tag for erb?

Nathan Weizenbaum

unread,
Nov 20, 2008, 2:19:28 AM11/20/08
to ha...@googlegroups.com
All the ERB helpers should work with Haml - if they don't, that's a bug.
The Haml helpers do require Haml, so you should avoid using them in a
mixed environment. The reason things like haml_concat and capture_haml
exist is to provide something for non-Rails environments.

railsjedi

unread,
Nov 23, 2008, 12:00:33 AM11/23/08
to Haml
Ok, I tracked all my frustrations with haml helpers down to an
incompatibility in content_tag between Rails 2.2 and Haml 2.0.5

When I try rendering this call in my haml view, nothing shows up:

- content_tag :div do
SHOW ME SOME CONTENT

I tried this both on Haml 2.0.4 and the latest Haml Edge (0a14c3) both
install via plugins. In all cases the content doesnt display.

Here is a test project that demonstrates the problem. Use it with Haml
2.0.4 and Rails 2.2 and you'll see the content doesnt display in Haml.
On Rails 2.1 however, it will work fine.

http://s3.amazonaws.com/railsjedi/hamlbug_rails22.zip

Traced the problem down to the content_tag alias method chaining in
lib/haml/helpers/action_view_mods.rb

I've added a unit test that isolates the bug as well as a fix for the
issue in Rails 2.2. If you are running into this error when using Haml
with Rails 2.2, give it a try. It should resolve your problems.
http://github.com/jcnetdev/haml/commit/bc27e25fcafedb85ce37d818d2872118ad19ba9a

Hope this helps. Be great to get a patch for this into 2.0.5 soon
before more Rails 2.2 users run into this issue.

ashchan

unread,
Nov 28, 2008, 8:54:14 AM11/28/08
to Haml
div_for seems to be not working either.

On Nov 23, 1:00 pm, railsjedi <jcnet...@gmail.com> wrote:
> Ok, I tracked all my frustrations with haml helpers down to an
> incompatibility in content_tag between Rails 2.2 and Haml 2.0.5
>
> When I try rendering this call in my haml view, nothing shows up:
>
> - content_tag :div do
>   SHOW ME SOME CONTENT
>
> I tried this both on Haml 2.0.4 and the latest Haml Edge (0a14c3) both
> install via plugins. In all cases the content doesnt display.
>
> Here is a test project that demonstrates the problem. Use it with Haml
> 2.0.4 and Rails 2.2 and you'll see the content doesnt display in Haml.
> On Rails 2.1 however, it will work fine.
>
> http://s3.amazonaws.com/railsjedi/hamlbug_rails22.zip
>
> Traced the problem down to the content_tag alias method chaining in
> lib/haml/helpers/action_view_mods.rb
>
> I've added a unit test that isolates the bug as well as a fix for the
> issue in Rails 2.2. If you are running into this error when using Haml
> with Rails 2.2, give it a try. It should resolve your problems.http://github.com/jcnetdev/haml/commit/bc27e25fcafedb85ce37d818d28721...

sdsykes

unread,
Nov 28, 2008, 2:50:44 PM11/28/08
to Haml
Yes, this is a very serious bug - it nixes your content tags in rails
2.2.

Before I saw this post I came up with my own fix, but in
capture_with_haml and not in content_tag_with_haml.

This fix is shorter - I just changed line 96 of action_view_mods.rb
from this:

if is_haml?

to this:

block_is_haml =
begin
eval('_hamlout', block)
true
rescue
false
end

if block_is_haml && is_haml?


- Stephen

On Nov 23, 7:00 am, railsjedi <jcnet...@gmail.com> wrote:
> Ok, I tracked all my frustrations with haml helpers down to an
> incompatibility in content_tag between Rails 2.2 and Haml 2.0.5
>
> When I try rendering this call in my haml view, nothing shows up:
>
> - content_tag :div do
>   SHOW ME SOME CONTENT
>
> I tried this both on Haml 2.0.4 and the latest Haml Edge (0a14c3) both
> install via plugins. In all cases the content doesnt display.
>
> Here is a test project that demonstrates the problem. Use it with Haml
> 2.0.4 and Rails 2.2 and you'll see the content doesnt display in Haml.
> On Rails 2.1 however, it will work fine.
>
> http://s3.amazonaws.com/railsjedi/hamlbug_rails22.zip
>
> Traced the problem down to the content_tag alias method chaining in
> lib/haml/helpers/action_view_mods.rb
>
> I've added a unit test that isolates the bug as well as a fix for the
> issue in Rails 2.2. If you are running into this error when using Haml
> with Rails 2.2, give it a try. It should resolve your problems.http://github.com/jcnetdev/haml/commit/bc27e25fcafedb85ce37d818d28721...

Nathan Weizenbaum

unread,
Nov 28, 2008, 6:44:44 PM11/28/08
to ha...@googlegroups.com
This has been fixed in the latest stable. It'll be released as part of
2.0.5 soon.

sdsykes

unread,
Nov 29, 2008, 11:06:57 AM11/29/08
to Haml
The problem remains in 2.0.5

This version I can fix just by changing line 89 of action_view_mods.rb
from

if is_haml?

to

if is_haml && block_is_haml?(block)


-Stephen

Nathan Weizenbaum

unread,
Nov 29, 2008, 2:21:15 PM11/29/08
to ha...@googlegroups.com
Can you supply a failing test?

sdsykes

unread,
Nov 30, 2008, 5:16:40 AM11/30/08
to Haml
Yes.

This is the kind of thing we are doing in our app - try adding this to
helper_test.rb

class ActionView::Base
module Ahelper
def nested_tag
content_tag(:span) {content_tag(:div) {"something"}}
end
end
include Ahelper
end

def test_content_tag_nested
assert_equal "<span><div>something</div></span>", render("=
nested_tag", :action_view).strip
end


And this test is fixed by at least by adding '&& block_is_haml?
(block)' to line 89 in action_view_mods.rb, but perhaps you can look
at it in more depth than I have.

-Stephen

sdsykes

unread,
Dec 5, 2008, 6:00:38 AM12/5/08
to Haml
Nathan, did you get a chance to look at this failing test?

-Stephen

Nathan Weizenbaum

unread,
Dec 6, 2008, 11:15:20 PM12/6/08
to ha...@googlegroups.com
Okay, I've integrated the test and the fix. This'll go out as Haml 2.0.6
pretty soon.
Reply all
Reply to author
Forward
0 new messages