4

我正在为 rails 3 应用程序创建一个简单的主题系统。主题由放置在Rails.root/themes 中的文件夹组成,其中包含

  • .yml 清单文件
  • 一些液体模板文件
  • 静态资产子文件夹

现在对于某些控制器/动作,我想从当前主题渲染视图,因此相应地使用静态资产。

因此我需要一种方法来告诉 Rails 重写

  • http://example.com/theme1/* ----> #{Rails.root}/themes/theme1/assets/*
  • http://example.com/theme2/* ----> #{Rails.root}/themes/theme2/assets/*
  • ...

目前我无法弄清楚如何做到这一点,因为我想避免为每个主题使用不同的引擎或将资产文件复制到public.

我怎么解决这个问题?

编辑:其他要求

我一直在寻找不会破坏 rails 默认值的东西,这样以后我可以利用新的资产管道功能(计划用于 rails 3.1)。

目前我发现只有这个:

config.asset_path = proc { |asset_path| "assets/#{asset_path}" } 

那将完全满足我的要求,不幸的是,启用资产管道时它将不适用。

4

1 回答 1

0

看看themes_for_rails插件。

自述文件的以下摘录显示了如何根据需要的任何逻辑更改使用的主题。

在控制器动作中:

class MyController < ApplicationController
  def show
    theme "purple"
  end
end

在控制器的类级别:

class MyController < ApplicationController
  theme "purple" # all actions will use this theme
  def show
    ...
  end
end 

或使用“解析器” lambda/函数:

class MyController < ApplicationController
  theme :theme_resolver
  # ...
private
  def theme_resolver
    current_user.theme # or anything else that return a string. 
  end
end

您的视图和邮件程序也有帮助函数。

于 2011-07-05T06:59:41.090 回答