In Django and many other frameworks or templating systems, we see template inheritance. Where you can put the static parts of the template in a single template and then extend that template to work out the dynamic parts. At my first look at the Zend Framework, it has “views” and “layout”. The “layout” is the master view. If you enable it, the corresponding views will be mashed up with the “layout” before the output is sent out to the browser.
In the zend layout, there is a part:
1 |
<?php echo $this->layout()->content; ?> |
Whatever is in the view file, it will be inserted at the position of this code in the layout file. Then the question rises, how do we modify other parts of the layout? Can we define some dynamic blocks in the layout?
The answer is YES. We can. It took me a while to find out. But it’s very simple.
# Any variable you define in the view file is available in the layout.
# You can use Placeholders for capturing large block of HTML contents or small texts.
While the first one is obviously simple, we shall have a look at the second one.
In your layout file, you can define a block or placeholder by using this piece of code:
1 |
<?php echo $this->placeholder('foo') ?> |
Later, from the view files, we can define the content of the place holder:
1 2 3 |
<?php $this->placeholder('foo')->captureStart(); ?> <b>This is the content</b> <?php $this->placeholder('foo')->captureEnd() ?> |
The capturing technique is specially good for capturing large block of content. While you can also set small text:
1 |
<?php $this->placeholder('foo')->set("Some text for later") ?> |
For a thorough overview, I would suggest you to have a look at the (messy) docs from the Zend Framework Manual: http://framework.zend.com/manual/en/zend.view.helpers.html 😉