[Solar-talk] Best way to use more than one layout?

Rodrigo Moraes rodrigo.moraes at gmail.com
Fri Nov 3 05:18:59 PST 2006


On 11/3/06, Matt M. wrote:
> I'd like to find an easy but flexible way to have a nested layout. For
> example, I have an Order page controller with action views: create, update,
> delete etc. Each of those views are then wrapped around a layout:
> Order/Layout/default.php. That layout has navigation for the Order
> controller. This Order controller, happens to be in the "admin" section of
> my site... so I'd like an admin layout too. And for the heck of it... why
> not use a generic site wide "document" layout that has nothing but the basic
> html document tags. Is this possible to do, and if so... how?

Matt, this is pretty possible, and it is my preferred approach too. It
is indeed a bit confusing to insert views inside layouts when you are
not used to Solar_Controller_Page. I've talked about this confusion
yesterday and I think it would be good for newcomers if Solar provided
a "document" layout in its examples; it would be easier to understand
and start to play with views and custom layouts.

Here's a recipe. :-)

1. Create a layout and include a variable template on it:

<html>
<body>
<?php echo $this->template($this->layout_tpl); ?>
</body>
</html>

2. Set a default $layout_tpl in your app.

3. Override _setViewLayout() to include your views dir to the layout path:

    protected function _setViewLayout($view)
    {
        // change the view object over to layouts.
        parent::_setViewLayout($view);

        // get the current class
        $class = get_class($this);

        // now add our own stuff
        $template = array();
        $template[] = str_replace('_', DIRECTORY_SEPARATOR, "{$class}_View");
        $view->addTemplatePath($template);
    }

Done. Now you can put your layout templates in the /Layout or /View
directory. You can also include views directly in a layout.

rodrigo


More information about the solar-talk mailing list