SameShirtEveryDay.com

Personal blog of the one called Alex Gorbatchev, from Toronto, Canada.

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

Posted on April 19th, 2009 by Alex Gorbatchev. Tagged with , . In Ruby. 1 comment so far...

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
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One comments so far...

  1. madsheep

    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.each
    syntax. 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

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> , rel="nofollow" in use - no link dropping, no keywords or domains as names; do not spam, and do not advertise!

home
Subscribe to this blog Follow me on Twitter My bookmarks on Delicious My photography on Flickr