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. span style="color: #ff0000;">'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. span style="color: #ff0000;">'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. span style="color: #ff0000;">'View');
  2.         $view = $this->getResource('View''XHTML1_STRICT');
  3.     }
  4. }

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

  1.  

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. span style="color: #808080; font-style: italic;">// ...
  2. 'View');
  3.         $view = $this->getResource('View''XHTML1_STRICT');
  4.  
  5.         // Set the initial title and separator:
  6.         $view->headTitle('My Site')
  7.              ->setSeparator(' :: ');
  8.     }
  9.  
  10.     // ...
  11. }

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

  1. span style="color: #ff0000;">'Some Page'); // place after other segments ?>
  2. 'Some Page'); // place before ?>

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

  1.  

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. span style="color: #808080; font-style: italic;">// ...
  2. 'View');
  3.         $view = $this->getResource('View''XHTML1_STRICT');
  4.  
  5.         // Set the initial title and separator:
  6.         $view->headTitle('My Site')
  7.              ->setSeparator(' :: ');
  8.  
  9.         // Set the initial stylesheet:
  10. '/styles/site.css');
  11.  
  12.         // Set the initial JS to load:
  13. '/js/site.js');
  14.     }
  15.  
  16.     // ...
  17. }

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

  1. span style="color: #ff0000;">'/js/user-list.js'"<?php echo $this->baseUrl() ?>"

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

  1.  

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