This question already has an answer here:
-
Injecting variable values into javascript and HAML in RoR
2 answers
Hey is there a way I can do this in haml?
:javascript
var Tab = <%= @tab %>
I could just add a helper I guess like:
<script>
var Tab = '<%= @tab %>'
</script>
But it'd be nice to use HAML!
You can use the string interpolation syntax (#{...}
) :
:javascript
var tab = #{@tab}
Take care of correctly escaping, however.
This is a terrible way to structure code.
Inline JavaScript is bad enough. Inline JavaScript with ERb inside puts you straight into hell.
JavaScript should be in external static files. That way the browser can cache them, and you can use JavaScript as a real programming language.
In your case, I'd recommend something like:
(Haml file)
#tab= @tab
(CSS file)
#tab { display: none }
(Javascript file)
var Tab = $('#tab').innerHTML();
That way everything stays static and maintainable, and you're not mixing JS and HTML.