[Rails 常用插件简介]Theme Support

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

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

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

[code]
require 'patches/routeset_ex'
[/code]

然后在你的/config/routes.rb适当的地方添加如下的代码:
[code]
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'
[/code]


2:生成新的theme
[code]ruby script/generate theme default[/code]
生成如下目录
[attachimg]238[/attachimg]

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

[img]http://mmm.iteye.com/upload/picture/pic/4901/32d07e41-acc7-4626-8f54-78af46c78a3f.png[/img]

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

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

def initialize_user_theme
"default" unless login?
current_user.theme
end
end
[/code]

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

[code]
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
[/code]

对于不同的css,images,js就是我们刚刚在routes中添加的三行代码来实现的
[code]
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
[/code]

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


好了,好像没什么好说的,any question?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值