# ERB in Javascript

**URL:** <https://discuss.rubyonrails.org/t/erb-in-javascript/18181>\
**Category:** rubyonrails-talk\
**Created:** [September 15, 2007, 3:45am UTC](https://discuss.rubyonrails.org/t/erb-in-javascript/18181 "2007-09-15T03:45:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@eggie5](https://discuss.rubyonrails.org/u/eggie5)\
**Post date:** [September 15, 2007, 3:45am UTC](https://discuss.rubyonrails.org/t/erb-in-javascript/18181/1 "2007-09-15T03:45:14Z")

</div>

Hi,

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

How can I do this?

---

<div class="post-metadata">

**Author:** ![Peter\_Gumeson](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@Peter\_Gumeson](https://discuss.rubyonrails.org/u/Peter_Gumeson)\
**Post date:** [September 15, 2007, 4:05am UTC](https://discuss.rubyonrails.org/t/erb-in-javascript/18181/2 "2007-09-15T04:05:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [September 15, 2007, 4:17am UTC](https://discuss.rubyonrails.org/t/erb-in-javascript/18181/3 "2007-09-15T04:17:49Z")

</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 \<script\> tag in it, and filling this with JavaScript and \<%%\> tags...

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [September 15, 2007, 4:41am UTC](https://discuss.rubyonrails.org/t/erb-in-javascript/18181/4 "2007-09-15T04:41:15Z")

</div>

msnapper wrote:

> Check out Dan Webb'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 ?

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman1](https://avatars.discourse-cdn.com/v4/letter/g/a88e57/32.png) [@Gregory\_Seidman1](https://discuss.rubyonrails.org/u/Gregory_Seidman1)\
**Post date:** [September 15, 2007, 2:54pm UTC](https://discuss.rubyonrails.org/t/erb-in-javascript/18181/5 "2007-09-15T14:54:20Z")

</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 \<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 &nbsp;&nbsp;def foo\_js &nbsp;&nbsp;&nbsp;&nbsp;@necessary\_data = Foo.get\_that\_necessary\_data &nbsp;&nbsp;&nbsp;&nbsp;render :layout =\> false, :type =\> 'text/javascript' &nbsp;&nbsp;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.

> &nbsp;&nbsp;Phlip

--Greg
