[Rails 常用插件简介]Theme Support

Theme Support 是一个支持多皮肤的插件,是从typo中抽取出来的

一:安装
千篇一律:(
ruby script/plugin install [url]http://mattmccray.com/svn/rails/plugins/theme_support[/url]


二:使用
/vendor/plugins/theme_support/lib/patches/routeset_ex.rb 不能正常执行,需要稍作修改
1:修改
/vendor/plugins/theme_support/init.rb

去掉

require 'patches/routeset_ex'


然后在你的/config/routes.rb适当的地方添加如下的代码:
    map.theme_images "/themes/:theme/images/*filename", :controller=>'theme', :action=>'images'
    map.theme_stylesheets "/themes/:theme/stylesheets/*filename", :controller=>'theme', :action=>'stylesheets'
    map.theme_javascript "/themes/:theme/javascript/*filename", :controller=>'theme', :action=>'javascript'



2:生成新的theme
ruby script/generate theme default

生成如下目录
[attachimg]238[/attachimg]

预览图:
/themes/default/images/preview.png
说明
/themes/default/about.markdown

[Rails 常用插件简介]Theme Support

你的rhtml文件按在app/views中的布局在 themes/${theme_name}/views 放置即可

在controller中,我们可以写如下:
class UsersController < ApplicationController
    layout  'default'
    theme :initialize_user_theme

    def initialize_user_theme
        "default" unless login?
        current_user.theme
    end
end


三:原理
其实很简单,theme support改变了模板加载的顺序

      def render_file(template_path, use_full_path = true, local_assigns = {})
         search_path = [
            "../themes/#{controller.current_theme}/views",       # for components
            "../../themes/#{controller.current_theme}/views",    # for normal views
            "../../themes/#{controller.current_theme}",          # for layouts
            "../../../themes/#{controller.current_theme}/views", # for mailer views
            ".",                                                 # fallback
            ".."                                                 # Mailer fallback
         ]
         
      end


对于不同的css,images,js就是我们刚刚在routes中添加的三行代码来实现的
class ThemeController < ActionController::Base
  def stylesheets
    render_theme_item(:stylesheets, params[:filename].to_s, params[:theme], 'text/css')
  end

  def javascript
    render_theme_item(:javascript, params[:filename].to_s, params[:theme], 'text/javascript')
  end

  def images
    render_theme_item(:images, params[:filename].to_s, params[:theme])
  end


render_theme_item
send_file "#{Theme.path_to_theme(theme)}/#{type}/#{file}", :type => mime, :disposition => 'inline', :stream => false



好了,好像没什么好说的,any question?

你可能感兴趣的:(JavaScript,SVN,css,Ruby,Rails)