SameShirtEveryDay.com

Personal blog of the one called Alex Gorbatchev, from Toronto, Canada.
follow me on Twitter

HAML bug with :javascript filter and undefined local variable or method

Posted on April 19th, 2009 by Alex Gorbatchev. Tagged with , . In Ruby. No comments yet...

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
home
Subscribe to this blog Follow me on Twitter My bookmarks on Delicious My photography on Flickr