Using register_template_handler to serve CSS files (Making A


#1

Hi,

I wanted to be able to use some Rails code inside CSS files, so I set up
a controller (StylesController) to serve CSS files that reside inside
the controller’s view folder when the browser requests
/stylesheets/:action

So in the controller I just define empty actions with the names I want
my style sheets in (ie: def cooleffects end - that would respond to
/stylesheets/cooleffects).

And in views/styles I have cooleffects.css

Then, I tried the following (both inside the controller, and in a
plugin’s init.rb):

ActionView::Base.register_template_handler ‘css’, ActionView::Base

Which doesn’t seem to have any effect, because Rails throws an error on
the CSS file (no matter if I comment the line above):

|No rhtml, rxml, or delegate template found for |

So, how can I make ActionView handle CSS files as if they were RHTML
files?

Thanks!

Ivan V.


#2

Iván
> I wanted to be able to use some Rails code inside CSS files,

Do you know RCSS?
http://rcss.rubyforge.org/
It lets you use Erb inside css stylesheets.

Note: I’ve experienced some problems when using RCSS on a shared server
(Dreamhost). I wonder if it’s related to RCSS resource need:it has to
translate the RCSS file into a CSS one. What happened is that the page
would sometimes render without any styling, as when you remove all the
stylesheed. Refreshing the page would have display correctly.

Alain


#3

Thanks Alain, it looks very interesting, specially the server-side
constants, which are something that I was looking to implement using
plain old erb code.

However, since you mention some problems with it, I’ll just keep an eye
on it for future usage. Right now, I went with this:

    tpl = ''
    File.open("#{RAILS_ROOT}/app/views/styles/#{params['cssfile']}")

do |css|
while line = css.gets
tpl += line
end
end
render_template tpl

Regards,
Ivan V.

Alain R.
escribió:> Iván