Rendering layouts that live in /public?
At work we have the following concept.
- a “site” has a subset of our content
- a “site” has a url - eg. “oursite.com/stickers” or “stickersNOTvicars.com”
- a “site” can have its own customised layout, or fall back to the default one
How does this work in code?
- site.articles (pretty simple)
- a whole load of “route filters” that match the incoming host
- the code I’m writing about
So, historically we did
class ApplicationController
layout :determine_layout
def determine_layout
if File.exists?("#{Rails.root}/public/sites/#{@site.id}/application.rhtml")
"../../public/sites/#{@site.id}/application"
else
"site_default"
end
end
end
This was a hack, and it breaks with Rails 2.1.1, so after a day or so playing with the internals of ActionView I discovered this;
append_view_path("#{Rails.root}/public/sites")
And it works!
We just need to set;
"#{@site.id}/application"
as our layout, and we’re away.
Site-specific layouts that live in /public!