# Best way to use ERb in a JavaScript file?

**URL:** <https://www.ruby-forum.com/t/best-way-to-use-erb-in-a-javascript-file/65636>\
**Category:** Rails\
**Created:** [August 20, 2006, 3:04am UTC](https://www.ruby-forum.com/t/best-way-to-use-erb-in-a-javascript-file/65636 "2006-08-20T03:04:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/8e8cbc/32.png) [@xavi](https://www.ruby-forum.com/u/xavi)\
**Post date:** [August 20, 2006, 3:04am UTC](https://www.ruby-forum.com/t/best-way-to-use-erb-in-a-javascript-file/65636/1 "2006-08-20T03:04:45Z")

</div>

I am currently naming my JavaScript files with the extension .rhtml just  
to be able of using ERb in them (“If the template file has a +.rhtml+  
extension then it uses a mixture of ERb (included in Ruby) and HTML”,  
[Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Base.html) ). Does anybody  
know of a better way?  
I looked at RJS, but it doesn’t seem the right solution here.

xavi

---

<div class="post-metadata">

**Author:** ![xavi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/8e8cbc/32.png) [@xavi](https://www.ruby-forum.com/u/xavi)\
**Post date:** [August 22, 2006, 9:53am UTC](https://www.ruby-forum.com/t/best-way-to-use-erb-in-a-javascript-file/65636/2 "2006-08-22T09:53:26Z")

</div>

Xavi C. wrote:

> I am currently naming my JavaScript files with the extension .rhtml just  
> to be able of using ERb in them (“If the template file has a +.rhtml+  
> extension then it uses a mixture of ERb (included in Ruby) and HTML”,  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Base.html) ). Does anybody  
> know of a better way?  
> I looked at RJS, but it doesn’t seem the right solution here.

Not sure why you wouldn’t use RJS, but I saw a blog posting about  
setting up a route and controller to to use ERb on .rcss files. The  
same technique should work for you:

[http://blog.hasmanythrough.com/articles/2006/03/23/dirt-simple-rcss-templates](http://blog.hasmanythrough.com/articles/2006/03/23/dirt-simple-rcss-templates)

–  
Devin Ben-Hur 503/860-4114 [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

You may not have noticed, but we live in one of the safest,  
most peaceful, times in human history. In the US, the three  
leading causes of death are: killing yourself, killing yourself,  
and killing yourself [http://tinyurl.com/msxhq](http://tinyurl.com/msxhq).

–  
No virus found in this outgoing message.  
Checked by AVG Free Edition.  
Version: 7.1.405 / Virus Database: 268.11.4/424 - Release Date:  
8/21/2006

---

<div class="post-metadata">

**Author:** ![xavi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/8e8cbc/32.png) [@xavi](https://www.ruby-forum.com/u/xavi)\
**Post date:** [August 22, 2006, 11:09am UTC](https://www.ruby-forum.com/t/best-way-to-use-erb-in-a-javascript-file/65636/3 "2006-08-22T11:09:26Z")

</div>

Devin Ben-Hur wrote:

> Not sure why you wouldn’t use RJS, but I saw a blog posting about  
> setting up a route and controller to to use ERb on .rcss files. The  
> same technique should work for you:
> 
> [http://blog.hasmanythrough.com/articles/2006/03/23/dirt-simple-rcss-templates](http://blog.hasmanythrough.com/articles/2006/03/23/dirt-simple-rcss-templates)

My JavasScript file is for the most part static, and I think that if use  
RJS I would have to write lots of lines like the one below, wouldn’t I?

page \<\< ‘rawJavaScript();’

On the other hand, thanks for the link! Yes, that should work, although  
after seeing what has to be done, I think I will continue using the  
.rhtml extension.

xavi
