How to setup multiple Layouts in Zend Framework. Eg. Public/Logged in/Various combinations of modules

StackOverflow https://stackoverflow.com/questions/4732778

Question

I know & used the very basic Zend Framework's Layouts where I used 1 layout throughout the whole site. But now I need a more intermediate/organized setup.

  • The public site layout will have the div#mainContent taking up the whole 12 columns (using 960gs)
  • The logged in site will have div#mainContent taking up 9 columns + side bar with 3 columns
  • In the sidebar of the logged in site, various pages may contain various modules (not Zend Framework's modules, more like "boxes/widgets")
  • They will have different nav menus too

I am thinking of using 1 base layout where the 2 sub layouts will "extend". The base layout will just contain the <html> declarations headScripts etc till the <body> then the sublayouts will contain definations for the wrapping divs div.grid_12, grid_9, grid_3. How can I implement this "extending", basically, I just want to reuse code

Also whats a good way to render sidebar boxes/widgets

Was it helpful?

Solution

I'm switching between layouts depending on the subdomain of my website.

Here's the layout plugin I'm using...

class App_Layout_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $layout = $this->getLayout();
        $filename = $layout->getLayoutPath() . '/' . $request->getModuleName() . '.' . $layout->getViewSuffix();

        //check if the layout template exists, if not use the default layout set in application.ini
        if (file_exists($filename))
        {
            $this->getLayout()->setLayout($request->getModuleName());
        }
    }

}

Of course you can modify this for your own needs.

Make sure you set up you application.ini correctly too including elements like the following...

resources.layout.layout = "default"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.pluginClass = "App_Layout_Controller_Plugin_Layout"

In my case I have:

default.phtml, admin.phtml, clients.phtml

I hope this helps...
Angel

OTHER TIPS

The way we are doing it, which I'm not sure if it's the best approach, is to set the current layout within the init() method available in each controllers.

So for example in case we have this URL: www.mysite.com/social/

class SocialController extends BaseController
{
    public function init(){
        $layout = $this->_helper->layout();
        $layout->setLayout('social');
    }
}

Then within the config.ini:

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

And there is a socia.phtml defined within the resources.layout.layoutPath

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top