title: Layout and View, what's the difference? As an example of Zend_Layout and Zend_View
body: <p>I cannot understand what is the difference between <code>Zend_Layout</code> and <code>Zend_View</code>.</p>
  
  <p>Here is a picture from <code>Zend_Layout</code> Tutorial.
  <img src="https://i.stack.imgur.com/f27K4.png" alt="enter image description here"></p>
  
  <p>Everything seems easy to understand. We have elements in <code>&lt;head&gt;</code>, we have <code>Header</code>, <code>Navigation</code> segment, <code>Content</code> segment, <code>Sidebar</code> and <code>Footer</code>. And it's easy to understand how they are called. But I can't see the difference between <code>View</code> and <code>Layout</code>. Why <code>Navigation</code> segment is called as <code>Layout</code>'s property and <code>Footer</code> and <code>Header</code> are called as <code>View</code> property?</p>
  
  <p>I tested <code>Zend_Layout</code> and interchanged them. I called the <code>Navigation</code> segment not as the <code>Layout</code>'s property but as the <code>View</code>'s property:</p>
  
  <pre><code>echo $this-&gt;layout()-&gt;nav;  // as in tutorial
  echo $this-&gt;nav;    // used to call like this
  </code></pre>
  
  <p>And everything works fine. Not only for <code>$nav</code>, but for any variable. So what's the difference?</p>
  
  <p>I attach my experiment code here.
  My experiment layout page consists of three main blocks:</p>
  
  <ul>
  <li>header (html-code of header),</li>
  <li>content (html-code of content block)</li>
  <li>footer (html-код footer)</li>
  </ul>
  
  <p>Here's a script of template:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
    &lt;html&gt;
     &lt;head&gt;
     &lt;/head&gt;
        &lt;body&gt;
           &lt;div header&gt;
             &lt;?php echo $this-&gt;header ?&gt;     // it works
             &lt;?php echo $this-&gt;layout()-&gt;header ?&gt;  // and this variant also works
           &lt;/div&gt;
  
           &lt;div content&gt;
              &lt;?php echo $this-&gt;content ?&gt;      // same thing, it is a View's property
              &lt;?php echo $this-&gt;layout()-&gt;content ?&gt;    // And it is a Layout's property
           &lt;/div&gt;
  
           &lt;div footer&gt;
               &lt;?php echo $this-&gt;footer ?&gt;        // same thing
               &lt;?php echo $this-&gt;layout-&gt;footer() ?&gt;  // both work (one or another I mean)
           &lt;/div&gt;
       &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>
  
  <p>My code now:</p>
  
  <pre><code>$layout = Zend_Layout::startMvc();         // instantiate Layout
  $layout-&gt;setLayoutPath('\TestPage\views');  // set the path where my layouts live
  
  // And here's the most interesting
  // Set Header layout first
  $layout-&gt;setLayout('header');    // 'header.php' - is my file with html-code of the Header 
                                   // I pass only name 'header', and it makes 'header.php' from it. 
                                   // Predefined suffix is 'phtml' but I changed it to 'php'
  $layout-&gt;getView()-&gt;button = "Button";   // assign some variable in the Header. Please pay attention, it is View's property
  $layout-&gt;button_2 = "Button_2";   // and also I can assign this way. It's Layout's property now. And they both work
  $headerBlock = $layout-&gt;render();   // render my Header and store it in variable
  
  // the same procedures for the Content block
  $layout-&gt;setLayout('content');
  $layout-&gt;getView()-&gt;one = "One";   
  $layout-&gt;two = "Two";
  $contentBlock = $layout-&gt;render();   // render and store in the variable
  
  //  and the same for the Footer
  $layout-&gt;setLayout('footer');
  $layout-&gt;getView()-&gt;foot = "Foot";   
  $layout-&gt;foot_2 = "Foot_2";
  $footerBlock = $layout-&gt;render();   // render and store in the variable
  
  // and finally last stage - render whole layout and echo it
  $lout-&gt;setLayout('main_template');
  $layout-&gt;getView()-&gt;header = $headerBlock;   // again, I can do also $layout-&gt;header
  $lout-&gt;content = $contentBlock;
  $lout-&gt;getView()-&gt;footer = $footerBlock;
  echo $lout-&gt;render();     // render and echo now.
  </code></pre>
  
  <p>And everything works fine, the page is displayed without errors. But I don't know whether I use <code>Zend_Layout</code> and <code>Zend_View</code> right way or wrong way. Is it the right way to construct the page using <code>Zend_Layout</code> as I do it? What's the difference between</p>
  
  <pre><code>echo $this-&gt;layout()-&gt;header // this
  echo $this-&gt;header  // and this
  </code></pre>
  
  <p>Which variant is right one?</p>
  
  <p>Also it seems I have double rendering: at first I render each segment. And then I render them again when I render my final template. Is this the right way?</p>