ruby-on-rails – Rails资产管道中的动态CSS,即时编译
我正在Rails 3.2中构建一个站点.自从我触及Rails或
Ruby已经3年了,所以我两个都生锈了,加上我最后一次使用rails就是Rails 2.3.不用说,请原谅下面的任何“简单”问题.
这是规格 >多Tennant CMS /商店网站 > http://company1.mywebsite.com >每个“商店”(也称为子域)可以通过CSS自定义具有自己的外观,感觉等 >可以在应用程序内的UI中执行自定义,允许用户更改Bootstrap的基本变量(即@textColor,@ bodyBackground等) >我正在使用less-rails-bootstrap gem来获取Twitter Bootstrap的外观/感觉等. 以下是挑战 >我需要能够将CSS的变量动态输出到一个混合到Bootstrap中的文件中,以便拾取变量以创建最终的CSS 使问题进一步复杂化…… …鉴于我基本上必须找到一些方法来动态编译CSS,这意味着我必须包含我通常不会在生产环境中使用的GEMS.表现非常重要.有没有办法隔离这个?一旦CSS被无效并重新生成,我就可以获取内容并将其写入磁盘或存储在某些memcached / redis / etc中.表现的实例. 任何评论,即使只是指向我一般的方向,将不胜感激. 谢谢! 解决方法
这是我最终落实的解决方案:
>我最终切换到bootstrap-sass而不是https://github.com/thomas-mcdonald/bootstrap-sass if defined?(Bundler) # If you precompile assets before deploying to production,use this line # Bundler.require(*Rails.groups(:assets => %w(development test))) # If you want your assets lazily compiled in production,use this line Bundler.require(:default,:assets,Rails.env) end >使用了Kuraut Computing的Manuel Meure(谢谢Manuel!)提供的概念,发现于http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/. >我做了一些调整以适应我自己的需要,但Manuel所说明的核心概念是我编译过程的基础. >在我的模型中(我们称之为“网站”),我有一段代码如下: # .../app/models/site.rb ... BASE_STYLE = " @import "compass/css3"; <ADDITIONAL_STYLES> @import "bootstrap"; @import "bootstrap-responsive"; ".freeze # Provides the SASS/CSS content that would # be included into your base SASS content before compilation def sass_content " $bodyBackground: #{self.body_background}; $textColor: #{self.text_color}; " + self.css # Any additional CSS/SASS you would want to add end def compile_css(test_only = false,force_recompile = false) # SassCompiler is a modification of the information made available at the Kraut Computing link compiler = SassCompiler.new("#{self.id}/site.css",{:syntax => :scss,:output_dir => Rails.root.join('app','assets','sites')}) # Bail if we're already compiled and we're not forcing recompile return if compiler.compiled? && !force_recompile && !test_only # The block here yields the content that will be rendered compiler.compile(test_only) { # take our base styles,slap in there some vars that we make available to be customized by the user # and then finally add in our css/scss that the user updated... concat those and use it as # our raw sass to compile BASE_STYLE.gsub(/<ADDITIONAL_STYLES>/,self.sass_content) } end 我希望这有帮助.我知道它偏离了原来的帖子,但是它的偏离是因为这似乎是解决问题最容易解决的问题. 如果我没有回答您的具体问题,请随时发表评论,以便尽可能扩展. 谢谢! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |