Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 17 ■ THE ZEND FRAMEWORK APPLIED^269

Listing 17-13. A Layout Script with an Included Conditional Placeholder

<html>
<body>
<?php
//Check if a menu placeholder exists
if($this->placeholder('Zend_Layout')->offsetExists('menu')) {
//Output the placeholders menu key content
echo $this->placeholder('Zend_Layout')->menu;
}
?>
<?php echo $this->placeholder('Zend_Layout')->content; ?>
</body>
</html>

■Note In Listing 17-12, you used $this->layout()->content instead of $this->placeholder
('Zend_Layout')->content to access the layout content placeholder. The former form is simply a
convenience method for the latter form.

When you want to place a menu item into this placeholder, from the context of a view
script, you can simply capture some content, as shown in Listing 17-14.

Listing 17-14. Capturing Output to a Placeholder (View Context)

<?php
$this->placeholder('Zend_Layout')
->captureStart('APPEND', 'menu');
?>
<li><a href="...">New Menu Item</a></li>
<?php $this->placeholder('Zend_Layout')->captureEnd(); ?>

This content will then be placed in the proper location in the document when the layout is
rendered. This will allow you to create some advanced view effects that inject code into different
sections of the document.

■Tip The Zend Framework developers currently have plans for other helpers that build on the placeholder
concept. These helpers will be called headScript, headTitle, headLink, and so on, and will be special-
ized for a specific common addition. As always, for the latest information, check out the Zend Framework manual.

McArthur_819-9.book Page 269 Friday, February 29, 2008 8:03 AM

Free download pdf