My Science Is Better

13Oct/110

Switch layouts in Zend Framework

If you have a larger Zend Framework application, you probably want to you use a layout for more of your pages. For instance you'd need at least two layouts: one for a backend and another one for frontend. In order to tell ZF to use layouts, you'll need to set the layout paths in your application.ini

In order to store the layouts in /application/layouts/scripts, add the following to /application/configs/application.ini

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

You can now create several layouts, something around this model:

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <?php echo $this->headTitle() ?>
    <?php echo $this->headScript() ?>
    <?php echo $this->headStyle() ?>
</head>
<body>
    <?php echo $this->render('header.phtml') ?>

    <div id="content"><?php echo $this->layout()->content ?></div>

    <?php echo $this->render('footer.phtml') ?>
</body>
</html>

By default, your application will use the layout in /application/layouts/scripts/layout.phtml, but if you want some pages to be rendered using another layout you can tell ZF to do so like this:

// Within controller
// note that the file that's going to be rendered is /application/layouts/scripts/backend.phtml
$this->_helper->_layout->setLayout('backend')

//Within view script
$this->layout()->setLayout('other-layout'); ?>

More info on using Zend_Layout: http://framework.zend.com/manual/en/zend.layout.quickstart.html

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

Tag Cloud

Categories

Archives

Akismet

Blogroll

Ads