2012-10-11 21 views
0

我正在使用rails的網站構建器,我想使用Sprockets SCSS處理器呈現網站css。由於用戶可以更改顏色和徽標,因此我無法使用Sprockets預編譯,因此我開始使用Rails SCSS模板處理程序來處理動態視圖。使用鏈輪的動態CSS

目標是在任何時間/sites/43543.css被請求時編譯'app/views/sites/show.css.scss'。這是迄今爲止我所擁有的。您會注意到我首先通過ERB處理器運行模板,然後嘗試通過Sprockets運行它。

https://gist.github.com/3870095

曼努埃爾Meurer想出了該ERB輸出寫入的路徑,然後觸發資產管道編譯它的替代解決方案。我能夠讓他的解決方案在本地工作,但不會在heroku上工作,因爲資產路徑不可寫。文件只能寫入tmp目錄,這些文件只能保證一個請求。

http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/

回答

2

一整天后,我能我的問題,感謝解決約翰Feminella和his post on google。對我來說,挑戰性的部分是弄清楚如何創建一個新的Sprockets :: Context。幸運的是,約翰的解決方案不需要上下文。

更新要點here

嘗試#1

此代碼不允許從資產管道導入CSS文件。

@import "foundation";作品,因爲基礎加載爲羅盤模塊

@import "custom_css";導致一個錯誤消息說文件找不到

def call(template) 
    erb = ActionView::Template.registered_template_handler(:erb).call(template) 

    %{ 
    options = Compass.configuration.to_sass_engine_options.merge(
     :syntax => :scss, 
     :custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)}, 
    ) 
    Sass::Engine.new((begin;#{erb};end), options).render 
    } 
end 

嘗試#2

此代碼失敗使用資產數據網址嵌入base64網址

def call(template) 
    erb = ActionView::Template.registered_template_handler(:erb).call(template) 

    %{ 
     compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments 
     options = compiler.options.merge({ 
     :syntax => :scss, 
     :custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)}, 
     }) 
     Sass::Engine.new((begin;#{erb};end), options).render 
    } 
    end 

嘗試3

原來在創建上下文可以使用空值。下面的代碼在開發中工作,但會在生產中引發錯誤。

ActionView::Template::Error (can't modify immutable index)

它出現錯誤發生在鏈輪::索引被用來代替鏈輪::環境中的生產。切換到Sprockets :: Environment也不能解決問題。

def call(template) 
    erb = ActionView::Template.registered_template_handler(:erb).call(template) 

    %{ 
     context = CompassRails.context.new(::Rails.application.assets, '', Pathname.new('')) 
     resolver = ::Sass::Rails::Resolver.new(context) 

     compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments 
     options = compiler.options.merge({ 
     :syntax => :scss, 
     :custom => {:resolver => resolver} 
     }) 

     Sass::Engine.new((begin;#{erb};end), options).render 
    } 
    end