Getting Started Zend_View Placeholders. Standard Placeholders

Translations of this material:

into Russian: 5.3 Начало работы с Zend_View Placeholders. Стандартные Placeholder`ы. 54% translated in draft.
Submitted for translation by antdmi 19.02.2011

Text

In the previous section, we learned about the placeholder() view helper, and how it can be used to aggregate custom content. In this section, we'll look at some of the concrete placeholders shipped with Zend Framework, and how you can use them to your advantage when creating complex composite layouts.

Most of the shipped placeholders are for generating content for the <head> section of your layout content -- an area you typically cannot manipulate directly via your application view scripts, but one you may want to influence. As examples: you may want your title to contain certain content on every page, but specific content based on the controller and/or action; you may want to specify CSS files to load based on what section of the application you're in; you may need specific JavaScript scripts loaded at different times; or you may want to set the DocType declaration.

Zend Framework ships with placeholder implementations for each of these situations, and several more.

Setting the DocType

DocType declarations are troublesome to memorize, and often essential to include in your document to ensure the browser properly renders your content. The doctype() view helper allows you to use simple string mnemonics to specify the desired DocType; additionally, other helpers will query the doctype() helper to ensure the output generated conforms with the requested DocType.

As an example, if you want to use the XHTML1 Strict DTD, you can simply specify:

  01. $this->doctype('XHTML1_STRICT');

Among the other available mnemonics, you'll find these common types:

XHTML1_STRICT — XHTML 1.0 Strict

XHTML1_TRANSITIONAL — XHTML 1.0 Transitional

HTML4_STRICT — HTML 4.01 Strict

HTML4_Loose — HTML 4.01 Loose

HTML5 — HTML 5

You can assign the type and render the declaration in a single call:

  01. echo $this->doctype('XHTML1_STRICT');

However, the better approach is to assign the type in your bootstrap, and then render it in your layout. Try adding the following to your bootstrap class:

  01. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

  02. {

  03.     protected function _initDocType()

  04.     {

  05.         $this->bootstrap('View');

  06.         $view = $this->getResource('View');

  07.         $view->doctype('XHTML1_STRICT');

  08.     }

  09. }

Then, in your layout script, simply echo() the helper at the top of the file:

  01. <?php echo $this->doctype() ?>

  02. <html>

  03.     <!-- ... -->

This will ensure that your DocType-aware view helpers render the appropriate markup, ensure that the type is set well before the layout is rendered, and provide a single location to change the DocType.

Specifying the Page Title

Often, a site will include the site or business name as part of the page title, and then add additional information based on the location within the site. As an example, the zend.com website includes the string "Zend.com" on all pages, and the prepends information based on the page: "Zend Server - Zend.com". Within Zend Framework, the headTitle() view helper can help simplify this task.

At its simplest, the headTitle() helper allows you to aggregate content for the <title> tag; when you echo it, it then assembles it based on the order in which segments are added. You can control the order using prepend() and append(), and provide a separator to use between segments using the setSeparator() method.

Typically, you should specify any segments common to all pages in your bootstrap, similar to how we define the doctype. In this case, we'll define a _initPlaceholders() method for operating on all the various placeholders, and specify an initial title as well as a separator.

  01. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

  02. {

  03.     // ...

  04. 

  05.     protected function _initPlaceholders()

  06.     {

  07.         $this->bootstrap('View');

  08.         $view = $this->getResource('View');

  09.         $view->doctype('XHTML1_STRICT');

  10. 

  11.         // Set the initial title and separator:

  12.         $view->headTitle('My Site')

  13.              ->setSeparator(' :: ');

  14.     }

  15. 

  16.     // ...

  17. }

Within a view script, we might want to add another segment:

  01. <?php $this->headTitle()->append('Some Page'); // place after other segments ?>

  02. <?php $this->headTitle()->prepend('Some Page'); // place before ?>

In our layout, we will simply echo the headTitle() helper:

  01. <?php echo $this->doctype() ?>

  02. <html>

  03.     <?php echo $this->headTitle() ?>

  04.     <!-- ... -->

This will generate the following output:

  01. <!-- If append() was used: -->

  02. <title>My Site :: Some Page</title>

  03. 

  04. <!-- If prepend() was used: -->

  05. <title>Some Page :: My Site</title>

Specifying Stylesheets with HeadLink

Good CSS developers will often create a general stylesheet for sitewide styles, and individual stylesheets for specific sections or pages of the website, and load these latter conditionally so as to decrease the amount of data needing to be transferred on each request. The headLink() placeholder makes such conditional aggregation of stylesheets trivial within your application.

To accomplish this, headLink() defines a number of "virtual" methods (via overloading) to make the process trivial. The ones we will be concerned with are appendStylesheet() and prependStylesheet(). Each takes up to four arguments, $href (the relative path to the stylesheet), $media (the MIME type, which defaults to "text/css"), $conditionalStylesheet (which can be used to specify a "condition" under which the stylesheet will be evaluated), and $extras (an associative array of key and value pairs, commonly used to specify a key for "media"). In most cases, you will only need to specify the first argument, the relative path to the stylesheet.

In our example, we'll assume that all pages need to load the stylesheet located in "/styles/site.css" (relative to the document root); we'll specify this in our _initPlaceholders() bootstrap method.

  01. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

  02. {

  03.     // ...

  04. 

  05.     protected function _initPlaceholders()

  06.     {

  07.         $this->bootstrap('View');

  08.         $view = $this->getResource('View');

  09.         $view->doctype('XHTML1_STRICT');

  10. 

  11.         // Set the initial title and separator:

  12.         $view->headTitle('My Site')

  13.              ->setSeparator(' :: ');

  14. 

  15.         // Set the initial stylesheet:

  16.         $view->headLink()->prependStylesheet('/styles/site.css');

  17.     }

  18. 

  19.     // ...

  20. }

Later, in a controller or action-specific view script, we can add more stylesheets:

  01. <?php $this->headLink()->appendStylesheet('/styles/user-list.css') ?>

Within our layout view script, once again, we simply echo the placeholder:

  01. <?php echo $this->doctype() ?>

  02. <html>

  03.     <?php echo $this->headTitle() ?>

  04.     <?php echo $this->headLink() ?>

  05.     <!-- ... -->

This will generate the following output:

  01. <link rel="stylesheet" type="text/css" href="/styles/site.css" />

  02. <link rel="stylesheet" type="text/css" href="/styles/user-list.css" />

Pages: ← previous Ctrl next
1 2