Scott Wilton
Scott Wilton

Reputation: 253

Load particular stylesheet in rails per variable

I wish to have a subdirectory (call it custom) within my assets/stylesheets that contains several different custom stylesheets. My app is for management of different events and each event has a different instance of the app running. Those custom stylesheets would contain instance-specific css (i.e. a different background color to match the event's logo). In the app there is an EventsConfiguration model and I would like to be able to choose one of the stylesheets in assets/stylesheets/custom to save as a variable in that model. I then would not want any of the other custom css files to be used.

The custom css file should be loaded after all of the other css files (compiled into application.scss) so that it can override.

Right now application.scss has *= require_tree . in it. It would be nice to leave that so that sprockets automatically includes all css files but I want it to exclude the custom subdirectory. Is there a way to do that?

Upvotes: 0

Views: 122

Answers (2)

Adrian CB
Adrian CB

Reputation: 671

I would start by re-organizing the assets directory. One suggestion is to:

  1. Move common stylesheets under assets/stylesheets/common
  2. application.scss contains the following as well as any other stylesheets that are not custom:

    *= require_tree ./common

  3. Create event specific stylesheets (custom_event.scss):

    *= require_tree ./common

    *= require_tree ./custom_event

You can then require the correct stylesheet in your view based on the configuration parameter. Keeping stylesheets modular will very much help in the long run.

Upvotes: 1

Bigxiang
Bigxiang

Reputation: 6712

The easiest way to exclude the custom directory is that moves the custom directory to your lib/assets/stylesheets, then application.scss will not include it.

Upvotes: 0

Related Questions