Basic Placeholder Usage

Standard Placeholders

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:

  1. $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:

  1. 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:

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     protected function _initDocType()
  4.     {
  5.         $this->bootstrap('View');
  6.         $view = $this->getResource('View');
  7.         $view->doctype('XHTML1_STRICT');
  8.     }
  9. }

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

  1. <?php echo $this->doctype() ?>
  2. <html>
  3.     <!-- ... -->

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.

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     // ...
  4.  
  5.     protected function _initPlaceholders()
  6.     {
  7.         $this->bootstrap('View');
  8.         $view = $this->getResource('View');
  9.         $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:

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

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

  1. <?php echo $this->doctype() ?>
  2. <html>
  3.     <?php echo $this->headTitle() ?>
  4.     <!-- ... -->

This will generate the following output:

  1. <!-- If append() was used: -->
  2. <title>My Site :: Some Page</title>
  3.  
  4. <!-- If prepend() was used: -->
  5. <title>Some Page :: My Site</title>

Aggregating Scripts Using HeadScript

Another common tactic to prevent long page load times is to only load JavaScript when necessary. That said, you may need several layers of scripts: perhaps one for progressively enhancing menus on the site, and another for page-specific content. In these situations, the headScript() helper presents a solution.

Similar to the headLink() helper, headScript() provides the ability to append or prepend scripts to the collection, and then echo the entire set. It provides the flexibility to specify either script files themselves to load, or explicit JavaScript. You also have the option of capturing JavaScript via captureStart()/ captureEnd(), which allows you to simply inline the JavaScript instead of requiring an additional call to your server.

Also like headLink(), headScript() provides "virtual" methods via overloading as a convenience when specifying items to aggregate; common methods include prependFile(), appendFile(), prependScript(), and appendScript(). The first two allow you to specify files that will be referenced in a <script> tag's $src attribute; the latter two will take the content provided and render it as literal JavaScript within a <script> tag.

In this example, we'll specify that a script, "/js/site.js" needs to be loaded on every page; we'll update our _initPlaceholders() bootstrap method to do this.

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     // ...
  4.  
  5.     protected function _initPlaceholders()
  6.     {
  7.         $this->bootstrap('View');
  8.         $view = $this->getResource('View');
  9.         $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.         // Set the initial JS to load:
  19.         $view->headScript()->prependFile('/js/site.js');
  20.     }
  21.  
  22.     // ...
  23. }

Within a view script, we might then add an extra script file to source, or capture some JavaScript to include in our document.

  1. <?php $this->headScript()->appendFile('/js/user-list.js') ?>
  2. <?php $this->headScript()->captureStart() ?>
  3. site = {
  4.     baseUrl: "<?php echo $this->baseUrl() ?>"
  5. };
  6. <?php $this->headScript()->captureEnd() ?>

Within our layout script, we then simply echo the placeholder, just as we have all the others:

  1. <?php echo $this->doctype() ?>
  2. <html>
  3.     <?php echo $this->headTitle() ?>
  4.     <?php echo $this->headLink() ?>
  5.     <?php echo $this->headScript() ?>
  6.     <!-- ... -->

This will generate the following output:

  1. <script type="text/javascript" src="/js/site.js"></script>
  2. <script type="text/javascript" src="/js/user-list.js"></script>
  3. <script type="text/javascript">
  4. site = {
  5.     baseUrl: "<?php echo $this->baseUrl() ?>"
  6. };
  7. </script>

Note: InlineScript Variant
Many browsers will often block display of a page until all scripts and stylesheets referenced in the <head> section have loaded. If you have a number of such directives, this can impact how soon somebody can start actually viewing the page.
One way around this is to emit your <script> tags just prior to closing the <body> of your document. (This is a practice specifically recommend by the » Y! Slow project.)
Zend Framework supports this in two different ways:

  • You can render your headScript() tag whereever you like in your layout script; just because the title references "head" does not mean it needs to be rendered in that location.

  • Alternately, you may use the inlineScript() helper, which is simply a variant on headScript(), and retains the same behavior, but uses a separate registry.


Basic Placeholder Usage