alternatives for erb to make application.js dynamic
-
20-09-2019 - |
Question
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?
Solution
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.