Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic flow of pages in Zend Framework PHP

How does Zend link $this->layout()->content with scripts/index/index.phtml?

I think I'm failing to understand the basics of how the pages are supposed to stick together. I've looked at the quick start on the zend site but it's far too simplistic.

like image 766
alan Avatar asked Feb 18 '11 21:02

alan


1 Answers

So as Tomáš Fejfar explained that's how the $this->layout()->content works. Yet what is interesting is that 'content' is not just a variable in the layout. In fact, 'content' is a key in a View placeholder called 'Zend_Layout'. For this reason, the following snippets are equivalent to echo $this->layout()->content in your layout.phtml:

 $placeHolder = Zend_View_Helper_Placeholder_Registry::getRegistry()->getContainer('Zend_Layout');
 echo $placeHolder['content'];

 // or

 echo $this->placeholder('Zend_Layout');

 // or 

 echo $this->placeholder('Zend_Layout')->content;

This can be very useful. What I mean is that you can define some places in your layout.phtml that will be display values of your custom keys from 'Zend_Layout' placeholder. For instance, imagine that you would like to have a layout.phtml and you want to be able to modify text in your footer. You could do this by defining layout.phtml which will contain the following in the footer:

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

You could setup a default value for this footer in e.g. your Bootstrap.php. However, if you wanted you could modify this text in your actions as follows;

$this->view->placeholder('Zend_Layout')->myFooterText = 'Some text only for this action';

That's it what I wanted to add. Off course one could think of other scenerios, because $this->view->placeholder('Zend_Layout') is an instance of Zend_View_Helper_Placeholder_Container so you could do other things with Zend_Layout placeholder.

EDIT: Key 'content' is a default name. You can change it to something else using setContentKey method of Zend_Layout, e.g.:

protected function _initSetNewLayoutContentKey() {

    $layout = $this->bootstrap('layout')->getResource('layout');

    // instead of 'content' use 'viewoutput'
    $layout->setContentKey('viewoutput');
}

With this change, in your layout.phtml you would use echo $this->layout()->viewoutput; instead of echo $this->layout()->content;.

like image 133
Marcin Avatar answered Sep 28 '22 16:09

Marcin