Conditional style sheet loading

I have a website with clients. Each client can have its own theme, and when a user of a particular client logs in, the company theme must be downloaded. In application.css.scss, I have a line like this for each company:

@import "_theme_x.css.scss"; @import "_theme_y.css.scss"; @import "_theme_z.css.scss"; 

How can I download only, for example. theme_x, when user of company x logs in and does not download theme_y and theme_z? Or is there a better way to do this? Thanks!

+6
source share
2 answers

If the themes are large, you can separate them from application.css and load them conditionally in your layout. For example, if you have a theme_stylesheet in application_helper that returns the name of the theme that the client uses:

 # application.html.erb <%= stylesheet_link_tag 'application', theme_stylesheet %> 

If they are small, I like the namespace. Leave your application.css as it is, but change the themes to use the top-level rule on the body. Place the tag on the body to select a topic. The beauty of this is that you can dynamically change the theme.

 <body class="theme-<%= theme_stylesheet %>"> ... </body> 

_theme_x.css.scss

 body.theme-x { ... } 
+7
source

you can do it this way, as you can first check whose client is the username and apply some

for it, and include various css files for different layouts.

How am i doing

first I create a method in the application helper, but I can implement the layout by user roles that you

can do it according to customers.

  def choose_layout if is_admin?(current_user) or is_super_admin?(current_user) 'admin' else 'application' end 

And in the controller, call it before the filter, than the layout will be implemented according to the user

the roles

 class AdministratorController < ApplicationController include ApplicationHelper layout :choose_layout def index @user = User.new @current_user = current_user end 

end

hope you could get this idea .....

+3
source

Source: https://habr.com/ru/post/911762/


All Articles