theme_support: 为Rails应用添加theme支持

theme_support: 为Rails应用添加theme支持

theme_support是Matt McCray 写的一个为Rails应用增加类似Typo的theme管理的插件,功能类似于Typo,支持liquid 和erb模板。

安装

$ script/plugin install http://mattmccray.com/svn/rails/plugins/theme_support

使用

1. 生成主题

$ script/generate theme [theme_name]

这将在你的Rails应用的跟目录下创建一个themes文件夹:

app_root
  themes/
    [theme_name]
      layouts/ <- layout .rhtml or .liquid templates
      images/
      stylesheets/
      javascripts/
      views/ <- you can override application views
      about.markdown
      preview.png

2. 然后你需要将原本在app/views目录下的模板文件移到themes/[theme_name]/views下,同时将layouts下的文件也移到模板中去,当然还有相关的CSS,JS以及图片等等。

3. 同时你需要将主题中的JS,CSS以及Images引用改为如下路径:

Image: /theme/:theme_name/images/:image_file
JS: /theme/:theme_name/javascripts/:image_file
CSS: /theme/:theme_name/stylesheets/:image_file

如果使用erb,你可以使用下面的helper:

  • theme_image_tag(filename)
  • theme_image_path(filename)
  • theme_javascript_include_tag(filename)
  • theme_javascript_path(filename)
  • theme_stylesheet_link_tag(filename)
  • theme_stylesheet_path(filename)

liquid也有一个helper:themeitem

<link rel=”StyleSheet” href=”{% themeitem %} default.css {% endthemeitem %}” />

它被转换为:

<link rel=”StyleSheet” href=”/themes/[current_theme]/stylesheets/default.css” />

4. 指定要使用的主题:


class ApplicationController
  layout 'fire'
  theme 'demo'
end

你也可以在filter中根据当前用户的设置来动态决定使用那个theme:


class ApplicationController
  before_filter :set_theme
  def set_theme
    theme current_user.theme
  end
end

Rails 2.1

如果你使用Rails 2.1,那么你还得多做一点工作:

1. 首先修改routeset_ex.rb,将draw和create_theme_routes改为如下定义:


def draw
  old_routes = @routes
  @routes = []
  begin
    create_theme_routes
    yield Mapper.new(self)
    install_helpers
  rescue
    @routes = old_routes
    raise
  end
end
def create_theme_routes
  # Added patch from D.J. Vogel that changes :filename to *filename … allowing sub-folders
  add_named_route ‘theme_images’, “/themes/:theme/images/*filename”, :controller=>’theme’, :action=>’images’
  add_named_route ‘theme_stylesheets’, “/themes/:theme/stylesheets/*filename”, :controller=>’theme’, :action=>’stylesheets’
  add_named_route ‘theme_javascript’, “/themes/:theme/javascript/*filename”, :controller=>’theme’, :action=>’javascript’
  add_route “/themes/*whatever”, :controller=>’theme’, :action=>’error’
end

2. 然后修改actionview_ex.rb,将render_file改为如下定义:


def render_file(template_path, use_full_path = true, local_assigns = {})
  ["#{RAILS_ROOT}/themes/#{controller.current_theme}/views", # for normal views
    "#{RAILS_ROOT}/themes/#{controller.current_theme}" # for layouts
   ].each do |prefix|
    @finder.prepend_view_path(prefix)
  end
  __render_file(template_path, use_full_path, local_assigns)
end

3. 如果还有问题,欢迎留言,可能我漏掉了什么。

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