Best way to customize my site when viewed through

2019-08-18 22:58发布

I have a site where users need to access our material in two ways:

  1. from the site itself
  2. 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?

2条回答
smile是对你的礼貌
2楼-- · 2019-08-18 23:07

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
查看更多
唯我独甜
3楼-- · 2019-08-18 23:09

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.

查看更多
登录 后发表回答