User theme switching with SASS - Ruby on Rails

后端 未结 4 535
执笔经年
执笔经年 2021-02-04 13:45

So I have an rails admin system that will allow a user to choose a theme, basically a set of SASS color variables that will recompile application.css.scss with the new colors. H

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 14:09

    3 easy steps:

    1. Compile all themes into different files upon deploy. This will take care of timestamping, zipping, etc.

    2. Render page with default theme.

    3. Use javascript to load alternate theme CSS.

    No need to mess with dynamic compilation and all that.

    To load a CSS dynamically you can use something like this:

    function loadCSS(url) {
      var cssfile = document.createElement("link");
      cssfile.setAttribute("rel", "stylesheet");
      cssfile.setAttribute("type", "text/css");
      cssfile.setAttribute("href", url);
    }
    

提交回复
热议问题