Different layout for iframe

I have a rails application with content that other websites should access through iframes. The content should have a different layout if it is displayed on websites (there is no menu bar, etc.), I created a new layout file called iframe.html.erb How can I check if the page is called an external iframe format, so it’s used correct layout file?

+4
source share
3 answers

As far as i know when you do

<iframe src="www.google.pl"></iframe> 

you have no control over the layout or display styles of the page in the iframe, unless you own the page and cannot make it look the way you like.

edited

If you are showing your own site, do the following:

 <iframe src="/some_site_that_i_can_change_code_in?from=iframe"></iframe> 

and then in the controller some_site_that_i_can_change_code_in:

 if params[:from] == "iframe" render :layout => "for_iframe" else render :layout => "normal" end 
+8
source

A good way to control a particular layout and content when servicing an iframe is to register with an "iframe" type.

 ## config/initializers/mime_types.rb Mime::Type.register 'text/html', "iframe" 

Create a view that matches the action of the controller, i.e.: show.iframe.haml . Then, when the request comes with format: iframe , it displays the version of the iframe.

This way you can precisely control what is in the iframe on other sites. No need to go crazy in the controller.

+5
source

I think the only way to do this is with Javascript and then redirect, but this seems to be messy and not a good idea. See this topic for more information: Detecting if it is a download or direct iframe

0
source

Source: https://habr.com/ru/post/1338222/


All Articles