Switching layout using a url parameter

I needed to open a static page in a pop-up window. For static pages we have a handy controller Pages but I wanted to use a different layout for each page and do it in a flexible way.

The magic of my solution is in three lines of code in AppController (app/app_controller.php). I simplify the code for the sake of clarity:

  1. span class=”st0″>’layout’‘layout’];
  2.     }
  3.   }
  4. }

I created a file /app/views/layouts/popup.ctp that contains only:

  1.  

And I call the static page as:

  1. http://test.com/pages/termsOfMembership/layout:popup

Which is created using this call:

  1. span class=”st0″>’Terms of Membership’‘controller’ => ‘pages’, ‘action’ => ‘termsOfMembership’, ‘layout’ => ‘popup’‘default’‘onclick’ => "window.open(this.href);")
  2.    ),

With this setting in my /app/config/routes.php:

  1. Router::connect(‘/pages/*’‘controller’ => ‘pages’, ‘action’ => ‘display’));

That Router setting is redundant if you use urls that look like this:

  1. http://test.com/pages/display/termsOfMembership/layout:popup

Then you also have to use a different call to the Router::url():

  1. span class=”st0″>’controller’ => ‘pages’, ‘action’ => ‘display’, ‘termsOfMembership’, ‘layout’ => ‘popup’)

Leave a Reply