开发者

Best way to handle dynamic css in a rails app

开发者 https://www.devze.com 2023-02-05 22:50 出处:网络
I\'m researching a problem for handling dynamic css in a rails app.Within the app, individual users and/or groups of users can have customized look and feel that is accomplished via CSS.There will not

I'm researching a problem for handling dynamic css in a rails app. Within the app, individual users and/or groups of users can have customized look and feel that is accomplished via CSS. There will not be any fixed number of "look and feels" or css files, the number will grow as the number of users and groups grows and the look and feel is defined by the users via the application's admin interface. Throughout the course of a typical day thousands (it not tens of thousands) of different variations of the css will be served up. The app will store the pre-built css in mongodb, so there it will not have to pay the price of constructing the css for e开发者_JAVA百科very request, the question is more about how is the best way to serve up this dynamic css content. I've seen other questions like [this one][1] that speak to using erb or sass, but some of these answers are dated by several years so I wanted to make sure there wasn't a better answer with Rails 3.


You can treat your CSS files as resources, store them on the database, and serve them with page caching, so that you only need to hit the db once when the CSS is modified. All later requests will be served directly by the web server from the cache, without ever touching your app or db.

# stylesheet.rb
class Stylesheet < ActiveRecord::Base
  validates_presence_of :contents
end

# stylesheets_controller.rb
class StylesheetsController < ApplicationController
  caches_page :show # magic happens here

  def show
    @stylesheet = Stylesheet.find(params[:id])
    respond_to do |format|
      format.html # regular ERB template
      format.css { render :text => @stylesheet.contents, :content_type => "text/css" }
    end
  end
  # the rest is your typical RESTful controller, 
  # just remember to expire the cache when the stylesheet changes
end

# routes.rb
resources :stylesheets

# layouts/application.html.erb
…
<link href="<%= stylesheet_path(@current_user.stylesheet) %>" rel="stylesheet" type="text/css" />


Well, I have worked with this a couple of times but they were definitely fixed no of CSS files to choose from. Its should be the same more or less.

One of things I used alot was the content_for blocks. Basically

<% content_for :css do %>
 // some css file or css content
<% end %>

And in the layout

<%=  yield :css %>
 

very simple way for managing the layouts.


This might give you a few ideas: Multiple robots.txt for subdomains in rails


I had a similar problem - but needed to serve the modified CSS only once. I store a couple of constants in a module 'Site' - which I can then use as constants in CSS or as constants throughout the Rails application. I auto-generate the CSS files whenever the Rails application restarts and the CSS input files were modified.

You could do something similar, but reference symbolic names in site_settings.rb and then fetch those on a per-user basis from MongoDB

http://unixgods.org/~tilo/Ruby/Using_Variables_in_CSS_Files_with_Ruby_on_Rails.html


Now let's say you have some dynamic styling called dynamic.css.scss.erb (the .erb at the end is important!) in app/assets/stylesheets. It will be processed by erb (and then by Sass), and as such can contain stuff like

.some_container {
<% favorite_tags do |tag, color| %>
.tag.<%= tag %=> {
    background-color: #<%= color %>;
}
<% end %>

}

0

精彩评论

暂无评论...
验证码 换一张
取 消