Zend Layout

From EKiniWiki

Jump to: navigation, search

Contents

[edit] Zend_Layout

[edit] Setup in the bootstrap

// setup layouts
require_once 'Zend/Layout.php';
Zend_Layout::startMvc(array('layoutPath' => '../application/views/layouts'));

[edit] Directory structure

Then my directory structure would be like this:

../application
      /views
           /helpers
           /layouts
                layout.phtml
           /scripts
                auth/ (rest of the auth view files)
                dummy/ (rest of the dummy view files)
                error/ (rest of the error view files)
                index/ (rest of the index view files)
                sidebar.phtml

[edit] The Layout file

My Layout file would look something like this:

//application/views/layouts/layout.phtml
< html >
< head >
    < title ><?php echo $this->escape(Zend_Registry::get('appName')); ?>< / title >
    < link rel="stylesheet" href="<?php echo $this->baseUrl(); ?>/styles.css" type="text/css">
< / head >
< body >
    < div id="content">
        <?php echo $this->layout()->content ?>
    < / div>
    < div id="sidebar">
        <?php echo $this->layout()->sidebar; ?>
    < / div>
< / body>
< / html>

The sidebar.phtml is will just be like this:

//application/views/scripts/sidebar.phtml
<pre>
< h2 >Sidebar< / h2>
< ul>
< li>Item 1</ li>
< li>Item 2</ li>
< li>Item 3</ li>
< li>Item 4</ li>
< li>Item 5</ li>
< / ul>

[edit] What it would look like in the controller

My controller would look something like this:

<?php
//application/controllers/indexController.php
class IndexController extends Zend_Controller_Action
{
    protected $_flashMessenger = null;

    public function init() {

        // Render sidebar for every action
        $response = $this->getResponse();
        $response->insert('sidebar', $this->view->render('sidebar.phtml'));

        //To change the layout:
        //$this->_helper->layout()->setLayout('layout2');

        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->messages = $this->_flashMessenger->getMessages();
    }

    public function indexAction()
    {
        $this->view->pageTitle = "My First Web Page";
    }
}

[edit] What it would look like inside the view file

My view file for the controller above would look something like this:

//application/views/scripts/index/index.phtml


<h1><?php echo $this->pageTitle ;?></h1>
<p>Look no HTML Headers and Footers!</p>
Personal tools
Bookmarks