Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS global variables not being passed to partials

Tags:

Basically I have a structure that used to work perfectly in 3.0.x and now it breaks with undefined variable errors.

  • partials
    • _base.css.sass
    • _header.css.sass
  • main.css.sass
  • application.css.sass

the application.css.sass:

//=require 'main' 

the main.css.sass:

@import 'partials/base' @import 'partials/header' 

Pretty simple stuff.

I get an error on _header.css.sass because it uses a variable defined on _base.css.sass

I didn't have this error before, and it makes no sense to me, since we're importing those variables on the _base.css.sass in the same context we are importing the _header.css.sass.

Am I going to have to import the _base.css.sass on every partial too? What is the point of the main.css.sass requiring global variables if they cannot be used in the own partials you are requiring?

If my structure is completely wrong, please give me an alternative, I'd love suggestions.

like image 477
Draiken Avatar asked Jun 07 '11 17:06

Draiken


People also ask

How do I declare partials in Sass?

Sass Partials scss files directly. However, when you want to import a file, you do not need the file to be transpiled directly. Sass has a mechanism for this: If you start the filename with an underscore, Sass will not transpile it. Files named this way are called partials in Sass.

Are SCSS variables scoped?

Scope of SASS variables In SCSS, there are two scopes: global scope and local scope.


2 Answers

You can't use //=require to include "dynamic" SASS stuff like mixins and variables, because that's used just for including the most "static" stuff (like pure CSS). You have to @import all your files in application.css.sass. Railscasts provides good explanation of this in episode #268.

like image 118
muhal Avatar answered Oct 06 '22 08:10

muhal


There is a simple solution for the original topic. Your application and main css files should be scss files. That means you can use the known syntax:

/*  *= require_self  *= require main  */ 

In main.scss you can import the partials like this:

@import 'partials/base' @import 'partials/header' 

These files are sass files and you can benefit from the greatness of SASS ;-)

like image 29
awenkhh Avatar answered Oct 06 '22 09:10

awenkhh