# Using register\_template\_handler to serve CSS files (Making A

**URL:** <https://www.ruby-forum.com/t/using-register-template-handler-to-serve-css-files-making-a/52974>\
**Category:** Rails\
**Created:** [January 22, 2006, 3:18am UTC](https://www.ruby-forum.com/t/using-register-template-handler-to-serve-css-files-making-a/52974 "2006-01-22T03:18:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 22, 2006, 3:18am UTC](https://www.ruby-forum.com/t/using-register-template-handler-to-serve-css-files-making-a/52974/1 "2006-01-22T03:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 22, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/using-register-template-handler-to-serve-css-files-making-a/52974/2 "2006-01-22T13:46:47Z")

</div>

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

Do you know RCSS?  
[http://rcss.rubyforge.org/](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

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 22, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/using-register-template-handler-to-serve-css-files-making-a/52974/3 "2006-01-22T16:45:36Z")

</div>

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

>
