# ERB in Javascript

**URL:** <https://www.ruby-forum.com/t/erb-in-javascript/114766>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/erb-in-javascript/114766 "2007-09-17T13:25:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/erb-in-javascript/114766/1 "2007-09-17T13:25:23Z")

</div>

Hi,

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

How can I do this?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/erb-in-javascript/114766/2 "2007-09-25T23:02:29Z")

</div>

msnapper wrote:

> Check out Dan W.'s MinusMOR plugin…
> 
> [danwebb.net - MinusMOR Released!](http://www.danwebb.net/2006/11/24/minusmor-released)

Kewt!

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

–  
Phlip

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/erb-in-javascript/114766/3 "2007-09-25T23:04:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/erb-in-javascript/114766/4 "2007-09-25T23:04:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/erb-in-javascript/114766/5 "2007-09-25T23:10:00Z")

</div>

Check out Dan W.'s MinusMOR plugin…

[http://www.danwebb.net/2006/11/24/minusmor-released](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](http://www.danwebb.net/2006/11/17/rjs-minus-r)

Peter
