I cannot understand what is the difference between Zend_Layout and Zend_View.
Here is a picture from Zend_Layout Tutorial.

Everything seems easy to understand. We have elements in <head>, we have Header, Navigation segment, Content segment, Sidebar and Footer. And it’s easy to understand how they are called. But I can’t see the difference between View and Layout. Why Navigation segment is called as Layout‘s property and Footer and Header are called as View property?
I tested Zend_Layout and interchanged them. I called the Navigation segment not as the Layout‘s property but as the View‘s property:
echo $this->layout()->nav; // as in tutorial
echo $this->nav; // used to call like this
And everything works fine. Not only for $nav, but for any variable. So what’s the difference?
I attach my experiment code here.
My experiment layout page consists of three main blocks:
- header (html-code of header),
- content (html-code of content block)
- footer (html-код footer)
Here’s a script of template:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div header>
<?php echo $this->header ?> // it works
<?php echo $this->layout()->header ?> // and this variant also works
</div>
<div content>
<?php echo $this->content ?> // same thing, it is a View's property
<?php echo $this->layout()->content ?> // And it is a Layout's property
</div>
<div footer>
<?php echo $this->footer ?> // same thing
<?php echo $this->layout->footer() ?> // both work (one or another I mean)
</div>
</body>
</html>
My code now:
$layout = Zend_Layout::startMvc(); // instantiate Layout
$layout->setLayoutPath('\TestPage\views'); // set the path where my layouts live
// And here's the most interesting
// Set Header layout first
$layout->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->getView()->button = "Button"; // assign some variable in the Header. Please pay attention, it is View's property
$layout->button_2 = "Button_2"; // and also I can assign this way. It's Layout's property now. And they both work
$headerBlock = $layout->render(); // render my Header and store it in variable
// the same procedures for the Content block
$layout->setLayout('content');
$layout->getView()->one = "One";
$layout->two = "Two";
$contentBlock = $layout->render(); // render and store in the variable
// and the same for the Footer
$layout->setLayout('footer');
$layout->getView()->foot = "Foot";
$layout->foot_2 = "Foot_2";
$footerBlock = $layout->render(); // render and store in the variable
// and finally last stage - render whole layout and echo it
$lout->setLayout('main_template');
$layout->getView()->header = $headerBlock; // again, I can do also $layout->header
$lout->content = $contentBlock;
$lout->getView()->footer = $footerBlock;
echo $lout->render(); // render and echo now.
And everything works fine, the page is displayed without errors. But I don’t know whether I use Zend_Layout and Zend_View right way or wrong way. Is it the right way to construct the page using Zend_Layout as I do it? What’s the difference between
echo $this->layout()->header // this
echo $this->header // and this
Which variant is right one?
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?
See the answer I posted to your other (similar) question for some background.
Zend_Viewis for rendering templates.Zend_Layoutis a special type of template that contains one or more other templates. You should only ever have one layout on a page. The idea is that the layout contains the portions of the HTML that don’t really change between pages.Your code can be simplified to:
The main layout file:
Usage:
Don’t call startMvc() if you are not using the Zend Framework controller classes (which you don’t appear to be).