On this page
Helpers
Doctype
Valid HTML and XHTML documents should include a DOCTYPE declaration.
Besides being difficult to remember, these can also affect how certain elements in your document should be rendered (for instance, CDATA escaping in <script> and <style> elements.
The Doctype helper allows you to specify one of the following types:
XHTML11XHTML1_STRICTXHTML1_TRANSITIONALXHTML1_FRAMESETXHTML1_RDFAXHTML1_RDFA11XHTML_BASIC1XHTML5HTML4_STRICTHTML4_LOOSEHTML4_FRAMESETHTML5
Basic Usage
The Doctype helper requires a constant indicating the desired doctype to its constructor.
Given no arguments, the default doctype of HTML 5 is used.
It can then be cast to a string in order to emit the doctype declaration:
use Laminas\View\Helper\Doctype;
$helper = new Doctype();
echo (string) $helper; // <!DOCTYPE html>
An example of configuring the helper with a specific doctype:
use Laminas\View\Helper\Doctype;
$helper = new Doctype(Doctype::HTML4_FRAMESET);
echo (string) $helper; // <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset…
In templates, you will typically use the helper in a layout template, for example:
<?php echo $this->doctype() ?>
In normal usage, the desired doctype is configured, and other helpers query the configured doctype to customise their output.
For example, in a XHTML document, meta tags have a self-closing tag <meta /> whereas in HTML 5, tags omit the closing slash, i.e. <meta>
Usage in a Mezzio Application
The factory Laminas\View\Helper\Service\DoctypeFactory checks the application configuration, making it possible to define the doctype through your configuration, e.g. config/autoload/mezzio.global.php or a ConfigProvider.php in a module.
For example, add the following lines to your config/autoload/mezzio.global.php file to set the Doctype to HTML5:
return [
/* ... */
'view_helper_config' => [
'doctype' => \Laminas\View\Helper\Doctype::HTML5,
],
];
Usage in a laminas-mvc Application
If you're running a laminas-mvc application, you should specify doctype via the ViewManager service.
Add the following lines to your config/autoload/global.php file to set the Doctype to HTML5:
return [
/* ... */
'view_manager' => [
'doctype' => \Laminas\View\Helper\Doctype::HTML5,
/* ... */
],
];
Note
The default doctype is HTML 5 when no configuration is specified.
Retrieving the Doctype
Inside templates, you can retrieve the doctype declaration by either casting the doctype helper to a string, or calling doctypeDeclaration()
$doctype = $this->doctype()->doctypeDeclaration();
Typically, you'll want to know if the doctype is XHTML or not; for this, the
isXhtml() method will suffice:
if ($this->doctype()->isXhtml()) {
// do something differently
}
You can also check if the doctype represents an HTML5 document.
if ($this->doctype()->isHtml5()) {
// do something differently
}
Choosing a Doctype to Use with the Open Graph Protocol
To implement the Open Graph Protocol, you may specify a doctype compatible with RDFa. These doctypes allows a developer to use the Resource Description Framework within an HTML document.
The constants to use to indicate RDFa support are:
use Laminas\View\Helper\Doctype;
$supportsRdfa = [
Doctype::HTML5,
Doctype::XHTML1_RDFA,
Doctype::XHTML1_RDFA11,
Doctype::XHTML5,
];
The RDFa doctype allows XHTML to validate when the 'property' meta tag attribute is used per the Open Graph Protocol spec. Example within a view script:
<?= $this->doctype('XHTML1_RDFA'); ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://opengraphprotocol.org/schema/">
<head>
<meta property="og:type" content="musician" />
In the previous example, we set the property to og:type. The og references
the Open Graph namespace we specified in the html tag. The content identifies
the page as being about a musician. See the Open Graph Protocol
documentation for supported properties. The
HeadMeta helper may be used to programmatically set these Open
Graph Protocol meta tags.
Here is how you check if the doctype is set to XHTML1_RDFA:
<?= $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml"
<?php if ($this->doctype()->isRdfa()): ?>
xmlns:og="http://opengraphprotocol.org/schema/"
xmlns:fb="http://www.facebook.com/2008/fbml"
<?php endif; ?>
>