Dynamic javascript

4 views
Skip to first unread message

CU

unread,
Aug 26, 2010, 3:49:55 AM8/26/10
to Ruby on Rails: Talk
I'm coming from a long period of CakePHP development. One of the more
useful features of that framework was the ability to add any inline
javascript in a view, with dynamic php codeblock-generated output
(variables, framework generated URLs, etc), to a javascript buffer,
and then have CakePHP render it inside an inline script tag just
before the </body>, or, my preferred solution, have CakePHP
automatically condense the buffer and output it to a uniquely-
named .js file on the filesystem. This file could then obviously be
cached by the browser, and was recreated whenever the structure of the
view/javascript changed.

Is there a similar feature in Rails, or a Gem that offers this
functionality? Otherwise, what would be the best practice in Rails for
unavoidable inline javascript output?

Many thanks

Marnen Laibow-Koser

unread,
Aug 26, 2010, 7:53:30 AM8/26/10
to rubyonra...@googlegroups.com
CU wrote:
> I'm coming from a long period of CakePHP development. One of the more
> useful features of that framework was the ability to add any inline
> javascript in a view, with dynamic php codeblock-generated output
> (variables, framework generated URLs, etc), to a javascript buffer,
> and then have CakePHP render it inside an inline script tag just
> before the </body>, or, my preferred solution, have CakePHP
> automatically condense the buffer and output it to a uniquely-
> named .js file on the filesystem. This file could then obviously be
> cached by the browser, and was recreated whenever the structure of the
> view/javascript changed.
>
> Is there a similar feature in Rails, or a Gem that offers this
> functionality?

Well, there's content_for, for starters. I think Rails provides some
other methods for combining JS, but I'm not certain.

> Otherwise, what would be the best practice in Rails for
> unavoidable inline javascript output?

Just don't write inline JS to start with!

Rails 3 is more helpful in this respect than Rails 2, or so I'm told.

>
> Many thanks

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

CU

unread,
Aug 26, 2010, 8:52:09 AM8/26/10
to Ruby on Rails: Talk
I thought of content_for literally 15 seconds after clicking send on
this post :)

I'm using Rails3, and you're right, there is a heavy focus on moving
away from inline JS. Perhaps I should move away from Cake's bad
practices, rather than try to find equivalents for them in Rails :)

Thanks for the help!
Reply all
Reply to author
Forward
0 new messages