Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access Rails objects in Sass?

In a Rails 3.1.0 project, I have Companies with a few customizable attributes like background_color and link_color. I want to be able to set some Sass variables like so:

$background_color: <%= @company.background_color %>
$link_color: <%= @company.link_color

...

This doesn't work because @company is nil when Sass does its thing. I'm not sure how to go about solving this in a way that's dynamic (companies can be created and colors can be changed and the views update immediately). Any suggestions?

like image 416
complex Avatar asked Sep 16 '11 05:09

complex


People also ask

Can I use CSS in SCSS?

The process for importing the regular CSS file into the SCSS file: You can create any number of CSS and SCSS files and you can use them using the keyword 'import'. For example, create one CSS file and import that file into the SCSS file then you can able to use that property in your project.

Is Sass the same as SCSS?

SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.

What is the use of @import function in Sass?

The SASS @import function helps us to import multiple SASS or CSS stylesheets together such that they can be used together. Importing a SASS file using the @import rule allows access to mixins, variables, and functions to the other file in which the other file is imported.

How do I import variables in Sass?

To use variables across multiple files in SASS is carried out by @import rule of SASS. @import rule which import Sass and CSS stylesheets for providing variables, @mixins and functions. Such that combine all stylesheets together for compiled css. It also imports URL such as frameworks like Bootstrap etc..


1 Answers

I can think of a couple approaches off the top of my head:

  1. Serve your stylesheet through a controller.
  2. Use CSS classes to configure the colors and serve just that CSS through a controller, inlined partial, or a CSS @import.

Serving your stylesheet through a controller is pretty straightforward so there's not much to say. This might be a bit ugly and cumbersome.

For the second one, you'd add a couple extra CSS classes:

.custom-bg {
    background-color: some-default-bg;
}
.link-fg {
    color: some-default-fg;
}
/*...*/

Then any element that need to use the custom background color would need their usual CSS classes and custom-bg; similar shenanigans would be needed for the other configurable values. To supply the customized CSS, you could inline a <style> element into your HTML using a standard ERB partial or you could serve the CSS through a controller (either through <style src="..."> or @import). So you'd fake the SASSy goodness with old school multiple CSS classes in your HTML.

There's also JavaScript. You'd need some way to identify the elements that need their colors adjusted and then adjust them directly with things like this:

$('.need-custom-background').css('background-color', '...');
like image 196
mu is too short Avatar answered Nov 15 '22 12:11

mu is too short