Import SASS file from database instead of filesystem

后端 未结 2 890
攒了一身酷
攒了一身酷 2020-12-20 23:48

I don\'t have much experience with Ruby. I want to @import sass from database instead of filesystem. I did not find any examples online. How should I go about implementing t

相关标签:
2条回答
  • 2020-12-21 00:18

    Since you're using Compass to compile, you can add a custom Sass importers in the Compass config file. For example, compiling using compass compile -c config.rb, you would include something like this in your config.rb file:

    require File.join(File.dirname(__FILE__), 'importer.rb')
    Sass.load_paths << Sass::Importers::Custom.new()
    

    Then in importer.rb in the same directory, you would include your importer definition:

    module Sass
        module Importers
            class Custom < Base
                def find(name, options)
                    if name == '[globals]'
                        options[:syntax] = :scss
                        options[:filename] = 'globals'
                        options[:importer] = self
                        return Sass::Engine.new("$imported-variable: blue;", options)
                    else
                        return nil
                    end
                end
    
                def find_relative(uri, base, options)
                    nil
                end
    
                def key(uri, options)
                    [self.class.name + ":" + uri, uri]
                end
    
                def mtime(uri, options)
                    nil
                end
    
                def to_s
                    '[custom]'
                end
            end
        end
    end
    

    Then in your Sass file you can use the importer:

    @import '[globals]';
    p {
        color: $imported-variable;
    }
    

    Of course, this is just a dummy implementation that only accepts a URI matching "[globals]". You'll need to supply your own implementation that accesses your MySQL database, as I don't have any experience with database access in Ruby. Hopefully this should get you a little closer, though, in addition to the links that @Sean has provided.

    0 讨论(0)
  • 2020-12-21 00:23

    Out of the box, sass will only import local files (anything else is compiled into a CSS @import statement) but as the sass docs explain you can write your own custom importers by extending the Sass::Importers::Base class.

    This answer to another question gives an example of a custom HTTP importer. You could approach this in (at least) two ways: write a custom importer for your database or serve the sass file over HTTP from the database (via a simple PHP page or something)

    0 讨论(0)
提交回复
热议问题