Topic: Ruby inside stylesheet

Is there a way to insert ruby code into the css files? I would like to have an array with colors so I can easily change them without having to go through the whole file and replace all color values.

Re: Ruby inside stylesheet

If you're talking about dynamically changing colors in your running application, then I would suggest separating those parts into theme CSS files that you selectively include in your layout based on a user's preference or something.

I'm a Phoenix Ruby on Rails developer and startup junkie kicking ass at Flatterline, a Ruby on Rails web application development company.

Re: Ruby inside stylesheet

Not really. I just have the same handful of colors spread out the sheet. I would like to replace them with variables that are initialized at the top of the file. That way I don't need to go through the whole file just because I wanted to darken two colors a little.

Re: Ruby inside stylesheet

ephracis wrote:

Is there a way to insert ruby code into the css files? I would like to have an array with colors so I can easily change them without having to go through the whole file and replace all color values.

Hello,

I'm not sure if this will work with CSS's (I have not tryied it yet) but I did this with a JavaScript file:

In the "app/views/layouts/application.rhtml" file:

<% @myVar = 20 %> <!-- This variable could come from a controller. //-->
<%= javascript_include_tag 'test.js.rhtml?myVar='.@myVar.to_s %>

In the "public/javascripts/test.js.rhtml" file:

<%
  begin
    myVar = params[:myVar].to_s # In PHP I use this $_GET['myVar'];
  rescue Exception => exc
    myVar = exc.to_s
  end
%>
alert("Hello World <%= myVar %>");

Ok, here everything so far so good, all the Ruby sentences are working (you will notice it because of the "begin...rescue") and most of the Rails issues too, even I could get information from a model without any problem.

However there is a little problem with the "parms" variable because it's not working, I'm trying to find out what it's this problem.

For the CSS solution should be something like:

In the "app/views/layouts/application.rhtml" file:

<% @myVar = 20 %> <!-- This variable could come from a controller. //-->
<%= stylesheet_link_tag 'test.css.rhtml?myVar='.@myVar.to_s, :media=>'all' %>

Or something like that!

I hope it helps you.

Thanks.

Re: Ruby inside stylesheet

SASS works perfect for this. http://sass-lang.com/