Skip to content
lucasefe edited this page Feb 17, 2012 · 1 revision

Inside method, for some explicit action:

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

Or at class level definition, in order to set a theme for more than one action. I think this is is prettier, and less invasive.

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

You could also enable a theme for some actions only

class MyController < ApplicationController
  theme "purple", :only => :show
  def show
    # with theme
  end
  def edit
    # no theme
  end
end

As a plus, you could do this to defer theme name resolution to a method:

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

As a general rule, when passing a String, that becomes the theme name, but when a Symbol is sent, it gets treated as method message.

Action Mailer integration:

As a plus, you can use it from Action Mailer too (thanks to rafaelss) like this:

class MyMailer < ActionMailer::Base

  def notify_someone
    mail :theme => "blue" , :to => "some@one.com"
  end

end

Or set the theme by default like this (thanks to maxjgon):

class MyMailer < ActionMailer::Base

  default :theme => "blue"
   
  def notify_someone
    mail :to => "some@one.com"
  end

end

Url Helpers

In your views you should be able to access your assets like this (given the theme ‘default’ is set):

current_theme_image_path('logo.png')   # => /themes/default/images/logo.png
current_theme_stylesheet_path('style') # => /themes/default/stylesheets/logo.css
current_theme_javascript_path('app')   # => /themes/default/stylesheets/app.js

Or a given theme:

current_theme_image_path('logo.png', 'purple')   # => /themes/purple/images/logo.png

In your application views, there are theme specific helper tags
available to you. For ERb templates they are:

theme_image_tag
theme_image_path
theme_javascript_include_tag
theme_javascript_path
theme_stylesheet_link_tag
theme_stylesheet_path  

Generators

For now, it only creates the theme folder and add the “themes_for_rails” route in the routes.rb.

rails generate themes_for_rails:install  

Inside the themes folder, it create a structure for my_theme.

rails generate themes_for_rails:theme my_theme  

Changing things

At least for now, you can change the ThemesForRails base dir in your app, in the corresponding environment file, or in your application.rb file. Do it like this:

KillerApp::Application.configure do
  #
  
  config.themes_for_rails.base_dir = File.join(Rails.root, "tmp")

  #...
end

Thanks to matheusca, now you can change the name of the theme’s dir.

KillerApp::Application.configure do
  #
  
  config.themes_for_rails.themes_dir = "another_themes"

  #...
end

Sass support

ThemesForRails will automatically add the themes paths to Sass, if sass is available.

For instance, everything you put inside themes/my_theme/stylesheets/sass will get compiled into themes/my_theme/stylesheets (duh, right?)

To bypass sass configuration, do this:


KillerApp::Application.configure do
# config.themes_for_rails.use_sass = false #…

end