PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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:

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

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:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDocType()
    {
        $this->bootstrap('View');
        $view = $this->getResource('View');
        $view->doctype('XHTML1_STRICT');
    }
}

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


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

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.

Zend Framework