ERB in Javascript

Hi,

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

How can I do this?

msnapper wrote:

Check out Dan W.'s MinusMOR plugin…

danwebb.net - MinusMOR Released!

Kewt!

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


Phlip

On Fri, Sep 14, 2007 at 09:17:49PM -0700, Phlip wrote:

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 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

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

tag in it, and filling this with JavaScript and <%%> tags... -- Phlip

Check out Dan W.'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