I can't understand when to use Layout
's variables and when to use View
's variables to get page segments on the page. Here is the picture form their Layout
package tutorial ($this
means the View
instance everywhere):
Why Navigation
, Content
and Sidebar
segments are got as Layout
variables?
$this->layout()->nav;
But HeadTitle
, HeadScript
, HeadStylesheet
are got straightly from View?
$this->headTitle(); // I know that this is a placeholder view helper.
// But this segment of the page logically belongs to Layout.
// and it has to be called smth like view->layout->placeholder
And why Header
and Footer
are from some partial
method of the View
but not Layout
's properties?
$this->partial('header.phtml');
I've tried to change them and both ways work fine:
echo $this->nav; // I assigned navigation segment script to the View and it works;
I tried to assign Footer
segment script to the Layout
and it also works:
$layout->footer = $footer;
echo $this->layout()->footer; // it also works, it's displayed on the page
Any of the ways may be applied to any variable on the page. For example in Navigation
segment I have a lot of variables to display and I can output them using both ways - one variable as Layout
's property, another one sa View
's property.
So what is the rule to use them right way? When should I use View
's variables and when Layout
's ones?
I agree that this isn't very clear from the documentation, and I don't think $this->layout()->nav
is explained at all. A few points that might help:
$this->layout()
is actually a call to the layout view helper, which returns the current instance of Zend_Layout
.
Zend_Layout
registers its own placeholder helper (with the key 'Zend_Layout'), and by default creates a 'content' variable in this.
- the
Zend_Layout
class has a magic __get()
method which proxies any member variable calls over to its registered placeholder container. So calling $this->layout()->content
is another way of writing $this->placeholder('Zend_Layout')->content
- the
Zend_Layout
class also has a magic __set()
method that proxies stored data to the placeholder class. So $layout->footer = 'foo'
is the same as calling $this->placeholder('Zend_Layout')->footer = 'foo' in the view
With that in mind:
Why Navigation, Content and Sidebar segments are got as Layout variables?
As these are accessing data stored in Zend_Layout
's placeholder. You could also use $this->placeholder('Zend_Layout')->content
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
These are view helpers.
And why Header and Footer are from some partial method of the View but not Layout's properties?
This is the standard way of accessing content from other templates.
In general, assume that using the view object is the correct way to access the data. Use the layout object/helper only if you know the data is in the layout placeholder.
The advantage of using placeholders over partials is that you can access and modify them in several different places, including in the view itself. For example say you had a sidebar which is stored in a partial. If you were to store this in the Zend_Layout placeholder instead (for example in a controller plugin), you can then override this for certain actions in the controller:
public function someAction()
{
$this->view->layout()->sidebar = 'Some other sidebar content';
}
or in the view script itself:
<?php $this->layout()->sidebar = 'Content for this page only'; ?>