views:

276

answers:

1

To make application.js more dynamic, I create javascript_controller and rename public/application.js to app/views/javascripts/application.js.erb

But <%= … %> does not look very good in javascript, are there any better templating engines for this task?

+2  A: 

If you're just looking for a better templating engine, why not try haml?

In haml, the following erb javascript

function test(blah) {
  alert("<%= @application_name %> says " + blah);
}

could be re-written as:

function test(blah) {
  alert("#{@application_name} says " + blah);
}

Since haml using the ruby string interpolation that we've all grown to know and love.

PS: One of the nice things about haml is that it can live right along side erb. Just install the gem, change the name of your application.erb.js to application.haml.js, and you should be all set.

PPS: Going this route will also open up the door to using haml in any of your views. Granted, some people do love ERb, but in my experience, using haml is orders of magnitude more readable and more fun.

jerhinesmith
On current project I began using haml, but I was not sure that haml will not apply to some lines of javascript. Also as it is indent driven, what will it do with indented javascript?
tig
Indented javascript will be just fine. The indentation is a feature used for generating html (i.e. Haml uses indentation to figure out when to close an html tag). In this case, you'd just be using the string interpolation feature of Haml, which can be used independently of the html generation features.
jerhinesmith