Pass a variable to sass from programming language

2019-06-12 23:45发布

问题:

I would like to pass a variable to my sass from coldfusion or php.

Above I declare a var that I need to pass to sass to set a container-size in my _base.sass.

Is there a way to do it?

回答1:

Use the !default flag in your variable to do this. here's what i mean

// SCSS file
@import "php-variables"; // have php write this file first before compiling
@import "variables"; // custom colors, font-sizes, etc.

// contents of php-variables

$myvar: #f00; // the value you're setting with php

// contents of variables

$myvar: #0f0 !default; // the value to use if php DOESN't override it.

This way you can still have a fallback for if your php code doesn't need to override a var. anything you write in the php file will be declared first. then as long as your variables file uses the !default flag on ALL variables then it won't override the php set ones.

more info here:

http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#variable_defaults_

I have tested this with the php compiler at http://www.phpsass.com/ and it works.



回答2:

I think you should be writing directly to the sass file through php/coldfusion. I doubt there is another way to do it.

An easy way to do this would just be to put your variable on your first line in the sass file or something and just overwrite the value using PHP.



标签: haml sass