I have a site where users need to access our material in two ways:
- from the site itself
- from iframes embedded into another site
My site handles #1 very nicely, and now I'm trying to extend its functionality to iframes that can be easily embedded. Essentially, I need to get rid of the site header, the site footer, and change some CSS for every page that is shown through an iframe (users need to be able to navigate quite a few pages using this iframe).
When trying the <iframe>
tag, I get my site appearing but exactly as it normally does, showing its header bar, footer, etc. inside the iframe.
What's the best way to allow users to navigate my site while changing the layout and CSS?
You can detect if the page is being viewed from an iframe via Javascript by adding this to the "domready" event (framework dependent):
var isInIFrame = (window.location != window.parent.location) ? true : false;
Wrap the affected areas in containers and display=none;
the stuff you would like hidden when accessed via iframe.
I'd make another layout for the iframe pages, something like app/views/layouts/iframe.html.erb
, that doesn't have your usual headers and footers, may have different CSS, etc.
Then you just need to make your controller actions use that layout when rendering, as appropriate. You can do that when if you call render explicitly, like this:
render "show", :layout => "iframe"
But it's probably simpler to set it at the controller level:
class SomeController < ApplicationController
layout "iframe", :only => "show"
end
You could also put your logic for choosing the correct layout (whatever it is -- I'm assuming that isn't really the thrust of the question) into a method:
class SomeController < ApplicationController
layout :current_layout
private
def current_layout
# Return the correct layout for the current request.
params[:iframe] ? "iframe" : "application"
end
end