ERB in Javascript

Hi,

I would like to use some ERB in my javascript files.

How can I do this?

Check out Dan Webb's MinusMOR plugin...

http://www.danwebb.net/2006/11/24/minusmor-released

be sure to read the background as well... http://www.danwebb.net/2006/11/17/rjs-minus-r

Peter

eggie5 wrote:

I would like to use some ERB in my javascript files.

How can I do this?

Someone might know a better way than writing a partial rhtml file, putting a <script> tag in it, and filling this with JavaScript and <%%> tags...

msnapper wrote:

Check out Dan Webb's MinusMOR plugin...

danwebb.net - MinusMOR Released!

Kewt!

Can I use it inside a :update do |page| block ?

eggie5 wrote: > I would like to use some ERB in my javascript files. > > How can I do this?

Someone might know a better way than writing a partial rhtml file, putting a <script> tag in it, and filling this with JavaScript and <%%> tags...

Well, yes, there is a better way. It mainly lets you avoid the script tag and refer to the JS with a normal javascript_include_tag call (which means easy caching). Here's a toy example:

### script_controller

class ScriptController < ApplicationController   def foo_js     @necessary_data = Foo.get_that_necessary_data     render :layout => false, :type => 'text/javascript'   end end

### foo_js.rhtml <%# eruby_subtype=js -%> MyCacheOfNecessaryData = <%= @necessary_data.to_json %>;

(Note that the eruby_subtype line is a hint to vim 7.1 and later to get syntax of the file right.)

Naturally, the majority of your JS will be static code and should be served as such, but when you have some data that needs to be available to the client-side JS, this is a useful idiom.

  Phlip

--Greg