HAML bug with :javascript filter and undefined local variable or method
http://github.com/nex3/haml/issues#issue/2
on haml 2.0.9
require 'haml/engine'
template_broken = '
:javascript
- [1, 2, 3, 4].each do |item|
= "Hey #{item}"
'
puts Haml::Engine.new(template_broken).to_html
causes
(haml):3:in `to_html': undefined local variable or method `item' for #<Object:0x2bedf64> (NameError)
from c:/ruby/lib/ruby/gems/1.8/gems/haml-2.0.9/lib/haml/engine.rb:149:in `to_html'
from c:/ruby/lib/ruby/gems/1.8/gems/haml-2.0.9/lib/haml/engine.rb:149:in `instance_eval'
from c:/ruby/lib/ruby/gems/1.8/gems/haml-2.0.9/lib/haml/engine.rb:149:in `to_html'
from haml.rb:9
Replacing :javascript filter with regular %script tag fixed the issue. It’s a workaround.
require 'haml/engine'
template_broken = '
%script
- [1, 2, 3, 4].each do |item|
= "Hey #{item}"
'
puts Haml::Engine.new(template_broken).to_html
One comments so far...
It’s not a bug, you are just not familiar with filters enough. Once you used filter you are forcing haml interpreter to render code after the filter as this particular language and nothing else. So once you started javascript filter you can’t go with rubys

-@array.eachsyntax. You can of course go with
#{@variable}to insert variable value in to the filtred code – so summing up: try using
#{- [1, 2, 3, 4].each do |item|}
#{= "Hey #{item}"}
but i’m not sure if that works. What will sure work is:
- [1, 2, 3, 4].each do |item|
:javascript
#{= "Hey #{item}"}
In other words RTFM
Cheers.
Leave a Reply