Javascript in Haml
One thing that is not obvious with HAML is how to add javascript. In the past, haml didn’t really like javascript that much but the latest version of haml on github makes it very simple. It’s done using the :javascript filter. Here’s an example:
:javascript
jQuery(function(){
tog("#forgot_password_clicker", "#login_form");
tog("#forgot_password_clicker", "#forgot_form", forgot_text);
});You can also mix in ruby variables by using the standard way of interpolating ruby with strings through: #{ }. Here’s an example of how it all works in a full page with a ruby variable mixed in:
!!! XML
!!!
%html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"}
%head
%meta{'http-equiv' => "content-type", :content => "text/html;charset=UTF-8"}
%body
- @some_ruby_variable = "something goes here."
:javascript
#{@some_ruby_variable}
jQuery(function(){
tog("#forgot_password_clicker", "#login_form");
tog("#forgot_password_clicker", "#forgot_form", forgot_text);
});This makes this HTML:
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- Programming Language:
- Tags:
- Technology:
- Add new comment
- 111 reads
- Feed: railsnotes
- Original article


Recent comments
1 year 23 weeks ago
1 year 23 weeks ago
1 year 25 weeks ago
1 year 27 weeks ago
1 year 42 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 46 weeks ago
1 year 48 weeks ago