specific theme per project

Added by Vianney Lecroart about 11 years ago

Hello,

I wanted to have different themes for different project.

In themes.rb, I changed this:

    @current_theme ||= Redmine::Themes.theme(th)

with:

    th = Setting.ui_theme
    if not @project.nil?
      th << "_#{@project.identifier}" 
    end
    @current_theme ||= Redmine::Themes.theme(th)

Then, I created different themes like:

classic_proj1/
classic_proj2/

The strange thing is that it almost works but when I do a force refresh with the browser, it displays classic theme instead of the proj1 one.
Is it a cache issue or something?
Have you a better idea to do that?

Replies (3)

RE: specific theme per project - Added by François Tertre about 11 years ago

Hello,

I have found where was your issue.

In your code, just change this :

th = Setting.ui_theme

by this

th = String.new(Setting.ui_theme)

The problem was that the _=_ just copy the pointer and not the value.

RE: specific theme per project - Added by Vianney Lecroart about 11 years ago

Thank you François,

It works fine now, I see that I lack some knowledge in ruby :/

RE: specific theme per project - Added by David Bronke about 11 years ago

My company has adapted this patch slightly to be more robust. We've added checking for the existence of the per-project theme so that it will default to the global theme if no per-project one exists. Our version also removes the prefix on the per-project theme, since for our site most of the per-project themes have little to do with the overall theme, and the overall theme for our site seldom changes. As a side note, we find that the easiest way to manage per-project themes is to simply create symlinks to other themes, unless there's actual per-project customization for a given project.

Here is a patch containing our changes:

--- redmine-0.8.3-original/lib/redmine/themes.rb    2009-04-05 12:44:19.000000000 +0000
+++ redmine-0.8.3/lib/redmine/themes.rb    2009-05-12 19:15:03.000000000 +0000
@@ -65,7 +65,20 @@

 module ApplicationHelper
   def stylesheet_path(source)
-    @current_theme ||= Redmine::Themes.theme(Setting.ui_theme)
+    
+    # Horrible hack to support multiple themes!
+    th = String.new(Setting.ui_theme)
+    if not @project.nil?
+      projecttheme = "#{RAILS_ROOT}/public/themes/#{@project.identifier}" 
+      if File.exists?(projecttheme)
+        th = @project.identifier
+      end
+    end
+    @current_theme ||= Redmine::Themes.theme(th)
+    
+    # Comment out everything above this line and uncomment this to go back to the default theme behavior.
+    #@current_theme ||= Redmine::Themes.theme(Setting.ui_theme)
+    
     super((@current_theme && @current_theme.stylesheets.include?(source)) ?
       "/themes/#{@current_theme.dir}/stylesheets/#{source}" : source)
   end

(1-3/3)