views:

30

answers:

2

for my CMs i want to be able to easily add new themes, my idea was to simply add a mime type for the new theme (so application.theme1.erb would work).

but for 99% of the themes im not going to want to change the views, well not all of them.

is there someway to have rails fall back on html if the themed view isnt present?

A: 

I'm pretty new to Rails, so this might not be a perfect answer:

you might want to try using querystring parameters as part of the route like described here: http://guides.rubyonrails.org/routing.html#querystring-parameters

so eventually something like this would work

map.connect ':theme/:controller/:action/:id' 

As I understand it, the theme would be available as params[:theme] in the controller. If no theme is specified you probably have to add another route like

map.connect '/:controller/:action/:id' 

to handle that case.

In the i18n guide something similar is described for locales: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params

Hope that helps.

StefanS
if i use that to set the mime type i will still have the same problem
Arcath
I guess I'm suggesting not to use the mimetype but instead a query string parameter. You could use this parameter to render the right layout.respond_to do |format| format.html render :layout => params[:theme]endsee http://guides.rubyonrails.org/layouts_and_rendering.html#using-render 2.2.11.2
StefanS
A: 

It depends on how much of the layout you want to change with the themes. If you build your HTML right, most of the things can be done through css. (changing fonts, colours, where the stuff show up)

Then it is quite easy to add just a theme parameter to style it.

If you don't want to do that, you can always create a separate layout for it, and assign that depending on the parameters passed in (or even set it as a session variable so you won't have it in the url).

Basically, for the default theme, you stick to the layouts/application.erb, then you have say layouts/theme1.erb which you then assign with a method

class ApplicationController 
   layout :decide_layout


  def decide_layout
    @session[:layout] || 'application'
  end
end

Customizing the views would be possible just by doing something like this in your actions:

def my_action
  render "my_action_#{@session[:layout]}" if @session[:layout]
end

if @session[:layout] is not set, it will render the default, otherwise it will render your themed view.

Of course, if you set the layout depending on user input, make sure to sanitize and check the layout parameter before.

Jimmy Stenke