-
从您上面发布的代码来看,您似乎正在尝试
在 Rails 应用程序中实现 css 主题。如果你,这就是我的方式
在我的应用程序中实现了主题功能。
每当管理员对主题文件进行更改并更新它时,
编译的 css 文件在
带有主题名称的 public/assets/themes/ 文件夹。那个文件是
应用程序根据应用的当前主题拾取。 (我可以
如果这是您要查找的内容,请提供代码。)
要仅将资源提供给某些特定页面,您需要实现某种逻辑来根据它加载资源。例如。控制器特定资产:check here.
选项 1 的更新
我有主题资源,我在其中保存主题名称和两种主题颜色(您可以使用更多)。
这是我的视图表单的样子:
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :color1 %><br>
<%= f.text_field :color1 %>
</div>
<div class="field">
<%= f.label :color2 %><br>
<%= f.text_field :color2 %>
</div>
这是我的 ThemesController 文件的样子:
class ThemesController < ApplicationController
after_action :compile_theme, only: [:create, :update]
THEME_PATH = 'app/assets/'
PATH = THEME_PATH + '/themes'
def new
@theme = Theme.new
end
def edit
@theme = Theme.find(params[:id])
end
def create
@theme = Theme.new(theme_params)
if @theme.save
# Create a scss theme file
write_theme_file
redirect_to @theme, notice: 'Theme was successfully created.'
else
render :new
end
end
def update
@theme = Theme.find(params[:id])
if @theme.update(theme_params)
# Create/Update a scss theme file, you can check for file exists
write_theme_file
redirect_to @theme, notice: 'Theme was successfully updated.'
else
render :edit
end
end
private
def write_theme_file
file = PATH + name + '.scss'
body = "$color1: #{@theme.color1};\n$color2: #{@theme.color2};"
File.write(file, body)
end
def compile_theme
file = PATH + name + '.scss'
theme_body = ''
if File.exists?(file) && File.exists?(THEME_PATH + 'theme.scss')
file = File.open(file)
theme_body = file.read
file.close
file = File.open(THEME_PATH + 'theme.scss')
theme_body = theme_body + file.read
file.close
else
colors = ''
end
env = if Rails.application.assets.is_a?(Sprockets::Index)
Rails.application.assets.instance_variable_get('@environment')
else
Rails.application.assets
end
Dir.mkdir(Rails.root + 'public/assets/themes') unless Dir.exists?(Rails.root + 'public/assets/themes')
asset_file = File.join(Rails.root, 'public', asset_path(name))
File.delete(asset_file) if File.exists?(asset_file)
body = ::Sass::Engine.new(theme_body, {syntax: :scss, cache: false, read_cache: false, style: :compressed}).render
File.write(File.join(Rails.root, 'public', asset_path(name)), body)
end
def asset_path(name)
digest = Digest::MD5.hexdigest(name)
"assets/themes/#{name}-#{digest}.css"
end
def name
return @theme.name.downcase
end
def theme_params
params.require(:theme).permit(:name, :color1, :color2)
end
end
控制器方法说明:
创建或更新新主题时,它会存储主题并在 app/assets/themes 中创建一个新的 .scss 文件,其中包含主题名称和定义的颜色值。
css 资产文件的编译和创建发生在创建/更新操作完成之后。 compile_theme 方法查找 theme.scss(底部示例)文件(我已经在 app/assets/stylesheets/ 文件夹中创建了基本主题颜色变量)并用当前颜色值替换 $color1 和 $color2 变量主题文件。生成的 css 数据保存在 theme_body 变量中。
body = ::Sass::Engine.new(theme_body, {syntax: :scss, cache: false, read_cache: false, style: :compressed}).render
File.write(File.join(Rails.root, 'public', asset_path(name)), body)
最后两行将在 public/assets/themes 中创建一个新文件,其中包含生成的 theme_body css 内容和文件名以及 theme_name 和摘要。
现在您需要在每个页面中提取文件。为此,在 application_controller.rb 文件中,定义这个
before_filter :set_theme
private
def set_theme
@theme = Theme.first # or change theme according to params
end
最后,您需要在布局文件中提取主题文件。因此,将其添加到 layouts/application.html.erb 文件中:
<% if @theme.present? %>
<% digest = Digest::MD5.hexdigest(@theme) %>
<%= stylesheet_link_tag asset_url("assets/themes/#{@theme}-#{digest}.css") %>
<% end %>
仅供参考,我的 theme.scss 文件如下所示:
body {
background-color: $color1;
}
#header-wrapper, footer {
background-color: $color2;
}
就是这样。希望这可以帮助。如果您有任何问题,请告诉我。