开发者

Dynamic (S)CSS in rails 3.1

开发者 https://www.devze.com 2023-04-12 04:23 出处:网络
I\'m trying to allow the user to customize my application using YML files. When the user updates certain things the CSS needs to be updated as well.

I'm trying to allow the user to customize my application using YML files.

When the user updates certain things the CSS needs to be updated as well.

I'd like to solve this problem using dynamic CSS instead. The way I was planning on 开发者_运维百科doing this is to have a settings SCSS file which the other css files import and use.

Here is what I have so far:

settings.scss.erb:

$width: <%= Rails.application.config.width %>px;

main.css.scss:

//= require settings

@import "settings";

#main {
  width: $width;
}

But I get this error:

Invalid CSS after "$width: ": expected expression (e.g. 1px, bold), was "<%= Rails.appli..."`

So It seems that the settings are not being passed through the erb parser before being handed off to the SCSS parser, is there any way to solve this.

I'd rather not put everything in .erb files since my text editor doesn't support (syntax highlighting and commands) when having scss in erb files


Side stepping the problem where ERB is not being parsed, you are not going to be able to customize the CSS dynamically (I think the main file may require an erb extension). The asset pipeline is designed to serve assets in a way that tells browsers that the are static and are not going to change.

Assuming the erb parsing was working, the width would be rendered during the precompile phase, or on the first request. If you are using Sprockets far-future headers are set to tell the remote clients to cache the content for 1 year. And Sprockets only picks up changes if the timestamp of the file changes, so would never get any new values.

You could force Sprockets to dynamically serve every request, and to not send any headers, but it is not really designed for this and there are significant performance risks in doing so.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号