Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:javascript filter breaks #{local} variables #2

Closed
alexgorbatchev opened this issue Apr 19, 2009 · 3 comments
Closed

:javascript filter breaks #{local} variables #2

alexgorbatchev opened this issue Apr 19, 2009 · 3 comments

Comments

@alexgorbatchev
Copy link

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 # (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
```

however, this works just fine. notice that I replaced :javascript filter with regular %script tag.

```
require ‘haml/engine’

template_broken = ’
%script
– [1, 2, 3, 4].each do |item|
= “Hey #{item}”

puts Haml::Engine.new(template_broken).to_html
```

@nex3
Copy link
Collaborator

nex3 commented Apr 19, 2009

You’re misunderstanding the Javascript filter. Like other Haml filters, it doesn’t take Haml code; it takes Javascript code. For example:

```
:javascript
function foo() {
return “bar”;
}
```

@alexgorbatchev
Copy link
Author

Have I missed this somewhere in the doc? It seems odd that I can’t generate output inline for a filter. Is this a technical limitation or by design? Think I saw something about scope issues in HAML code comments…

@nex3
Copy link
Collaborator

nex3 commented Apr 19, 2009

This is by design. You can put Ruby code in a filter using `#{}`, like so:

```
:javascript
var rails_version = #{Rails::VERSION::STRING.to_json}
```

but you can’t use Haml constructs like “-” and “=”. The purpose of filters is to allow non-Haml languages, like Javascript, to be embedded within Haml.

k0kubun added a commit to k0kubun/haml that referenced this issue Jun 8, 2021
k0kubun added a commit to k0kubun/haml that referenced this issue Jun 8, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants