Source for file Page2.php
Documentation is available at Page2.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997 - 2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
// | Klaus Guenther <klaus@capitalfocus.org> |
// +----------------------------------------------------------------------+
// $Id: Page2.php,v 1.11 2009/01/26 14:15:44 clockwerx Exp $
* The PEAR::HTML_Page2 package provides a simple interface for generating an XHTML compliant page
* - supports virtually all HTML doctypes, from HTML 2.0 through XHTML 1.1 and
* XHTML Basic 1.0 plus preliminary support for XHTML 2.0
* - global language declaration for the document
* - full META tag support
* - support for stylesheet declaration in the head section
* - support for script declaration in the head section
* - support for linked stylesheets and scripts
* - full support for header <link> tags
* - body can be a string, object with toHtml or toString methods or an array
* - Use to validate the output of a class for XHTML compliance
* - Quick prototyping using PEAR packages is now a breeze
* @version @package_version@
* @version $Id: Page2.php,v 1.11 2009/01/26 14:15:44 clockwerx Exp $
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Klaus Guenther <klaus@capitalfocus.org>
* Include HTML_Common class
* <p>Additional required files:</p>
* <p>HTML/Page2/Doctypes.php is required in private method
* <p>HTML/Page2/Namespaces.php is required in private method
* <p>HTML/Page2/Frameset.php is optionally required in method setDoctype() if
* the doctype variant is frameset.</p>
require_once 'HTML/Common.php';
* Determines how content is added to the body.
* Use with the @see addBodyContent method.
define('HTML_PAGE2_APPEND', 0 );
define('HTML_PAGE2_PREPEND', 1 );
define('HTML_PAGE2_REPLACE', 2 );
* (X)HTML Page generation class
* <p>This class handles the details for creating a properly constructed XHTML page.
* Page caching, stylesheets, client side script, and Meta tags can be
* managed using this class.</p>
* <p>The body may be a string, object, or array of objects or strings. Objects with
* toHtml() and toString() methods are supported.</p>
* <p><b>XHTML Examples:</b></p>
* <p>Simplest example:</p>
* // the default doctype is XHTML 1.0 Transitional
* // All doctypes and defaults are set in HTML/Page/Doctypes.php
* $p->addBodyContent("<p>some text</p>");
* <p>Complex XHTML example:</p>
* // The array takes an array of attributes that determine many important
* // aspects of the page generations.
* // Possible attributes are: charset, mime, lineend, tab, doctype, namespace,
* $p = new HTML_Page2(array (
* // Sets the charset encoding (default: utf-8)
* // Sets the line end character (default: unix (\n))
* // Sets the tab string for autoindent (default: tab (\t))
* // This is where you define the doctype
* 'doctype' => "XHTML 1.0 Strict",
* // Global page language setting
* // If cache is set to true, the browser may cache the output.
* $p->setTitle("My page");
* // Add optional meta data
* $p->setMetaData("author", "My Name");
* // Put something into the body
* $p->addBodyContent("<p>some text</p>");
* // If at some point you want to clear the page content
* // and output an error message, you can easily do that
* // See the source for {@link toHtml} and {@link _getDoctype}
* $p->setTitle("Error!");
* $p->setBody("<p>Houston, we have a problem: $error</p>");
* } // end error handling
* $p->toFile('example.html');
* Simple XHTML declaration example:
* // An XHTML compliant page (with title) is automatically generated
* // This overrides the XHTML 1.0 Transitional default
* $p->setDoctype('XHTML 1.0 Strict');
* // Put some content in here
* $p->addBodyContent("<p>some text</p>");
* <p><b>HTML examples:</b></p>
* <p>HTML 4.01 example:</p>
* $p = new HTML_Page2('doctype="HTML 4.01 Strict"');
* $p->addBodyContent = "<p>some text</p>";
* <p>nuke doctype declaration:</p>
* $p = new HTML_Page2('doctype="none"');
* $p->addBodyContent = "<p>some text</p>";
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Klaus Guenther <klaus@capitalfocus.org>
* Contains the content of the <body> tag.
* Controls caching of the page
* Contains the character encoding string
* Contains the !DOCTYPE definition
var $_doctype = array ('type'=> 'xhtml','version'=> '1.0','variant'=> 'transitional');
* Contains the page language setting
* Array of Header <link> tags
var $_metaTags = array ( 'standard' => array ( 'Generator' => 'PEAR HTML_Page' ) );
var $_mime = 'text/html';
* Array of linked scripts
* Array of scripts placed in the header
* Array of included style declarations
* Array of linked style sheets
var $_styleSheets = array ();
* Defines whether XML prolog should be prepended to XHTML documents
* Contains an instance of {@see HTML_Page2_Frameset}
* <p>Accepts an array of attributes</p>
* <p><b>General options:</b></p>
* - "lineend" => "unix|win|mac" (Sets line ending style; defaults to
* unix.) See also {@link setLineEnd}.
* - "tab" => string (Sets line ending style; defaults to \t.) See
* - "cache" => "false|true" See also {@link setCache}.
* - "charset" => charset string (Sets charset encoding; defaults
* to utf-8) See also {@link setCharset} and {@link getCharset}.
* - "mime" => mime encoding string (Sets document mime type;
* defaults to text/html) See also {@link setMimeEncoding}.
* <p><b>XHTML specific options:</b></p>
* - "doctype" => string (Sets XHTML doctype; defaults to
* XHTML 1.0 Transitional.) See also {@link setDoctype}.
* - "language" => two letter language designation. (Defines global
* document language; defaults to "en".) See also {@link setLang}.
* - "namespace" => string (Sets document namespace; defaults to the
* W3C defined namespace.) See also {@link setNamespace}.
* - "profile" => string (Sets head section profile) See also
* {@link setHeadProfile}.
* - "prolog" => bool (Enables or disables the XML prolog. This is
* usually unwanted, as it makes the page invalid XHTML.) See also
* {@link disableXmlProlog} and {@link enableXmlProlog}.
* <p>For extensive usage examples, see class-level documentation
* ({@see HTML_Page2}).</p>
* @param mixed $attributes Associative array of table tag
$attributes = $this->_parseAttributes ($attributes);
if (isset ($attributes['lineend'])) {
$this->setLineEnd ($attributes['lineend']);
if (isset ($attributes['charset'])) {
if (isset ($attributes['doctype'])){
if ($attributes['doctype'] == 'none') {
} elseif ($attributes['doctype']) {
if (isset ($attributes['language'])) {
$this->setLang($attributes['language']);
if (isset ($attributes['mime'])) {
if (isset ($attributes['namespace'])) {
if (isset ($attributes['profile'])) {
if (isset ($attributes['tab'])) {
$this->setTab ($attributes['tab']);
if (isset ($attributes['cache'])) {
if (isset ($attributes['prolog'])) {
if ($attributes['prolog'] === false ) {
} // end class constructor
* Iterates through an array, returning an HTML string
* <p>It also handles objects, calling the toHTML or toString methods
* and propagating the line endings and tabs for objects that
* extend HTML_Common.</p>
* <p>For more details read the well-documented source.</p>
* @param mixed $element The element to be processed
function _elementToHtml(&$element) // It's a reference just to save some memory.
// get the special formatting settings
$lnEnd = $this->_getLineEnd ();
$tabs = $this->_getTabs ();
$offset = $this->getTabOffset () + 1;
// initialize the variable that will collect our generated HTML
// Attempt to generate HTML code for what is passed
// If this is an object, attempt to generate the appropriate HTML
// For this special case, we set the appropriate indentation
// and line end styles. That way uniform HTML is generated.
// The reason this does not check for each method individually
// is that it could be that setTab, for example, could
// possibly refer to setTable, etc. And such ambiguity could
// create a big mess. So this will simply bias the HTML_Page
// class family toward other HTML_Common-based classes.
// Of course, these features are not necessarily implemented
// in all HTML_Common-based packages. But at least this makes
// it possible to propagate the settings.
$element->setTabOffset ($offset);
$element->setLineEnd ($lnEnd);
// Attempt to generate code using first toHtml and then toString
// methods. The result is not parsed with _elementToHtml because
// it would improperly add one tab indentation to the initial line
// of each object's output.
$strHtml .= $element->toHtml () . $lnEnd;
$strHtml .= $element->toString () . $lnEnd;
// If the class does not have an appropriate method, an error
// should be returned rather than simply dying or outputting
// the difficult to troubleshoot 'Object' output.
PEAR ::raiseError (" Error: Content object (class $class) " .
'does not support methods toHtml() or ' .
'toString().',0 ,PEAR_ERROR_TRIGGER );
foreach ($element as $item) {
// Parse each element individually
// If we don't have an object or array, we can simply output
// the element after indenting it and properly ending the line.
$strHtml .= $tabs . $tab . $element . $lnEnd;
} // end func _elementToHtml
* Generates the HTML string for the <body> tag
$lnEnd = $this->_getLineEnd ();
$tabs = $this->_getTabs ();
// If body attributes exist, add them to the body tag.
// Many attributes are depreciated because of CSS.
$strAttr = $this->_getAttrString ($this->_attributes);
// If this is a frameset, we don't want to output the body tag, but
// rather the <noframes> tag.
if (isset ($this->_doctype['variant']) && $this->_doctype['variant'] == 'frameset') {
$tabs = $this->_getTabs ();
$strHtml = $tabs . '<noframes>' . $lnEnd;
$tabs = $this->_getTabs ();
$strHtml .= $tabs . " <body $strAttr>" . $lnEnd;
$strHtml .= $tabs . '<body>' . $lnEnd;
// Allow for mixed content in the body array, recursing into inner
// array serching for non-array types.
$strHtml .= $tabs . '</body>' . $lnEnd;
// See above comment for frameset usage
if (isset ($this->_doctype['variant']) && $this->_doctype['variant'] == 'frameset') {
$strHtml .= $this->_getTabs () . '</noframes>' . $lnEnd;
} // end func _generateBody
* Generates the HTML string for the <head> tag
// Close empty tags if XHTML for XML compliance
if ($this->_doctype['type'] == 'html'){
$lnEnd = $this->_getLineEnd ();
$tabs = $this->_getTabs ();
$strHtml = $tabs . '<head>' . $lnEnd;
foreach ($this->_metaTags as $type => $tag) {
foreach ($tag as $name => $content) {
if ($type == 'http-equiv') {
$strHtml .= $tabs . $tab . " <meta http-equiv=\"$name\" content=\"$content\"" . $tagEnd . $lnEnd;
} elseif ($type == 'standard') {
$strHtml .= $tabs . $tab . " <meta name=\"$name\" content=\"$content\"" . $tagEnd . $lnEnd;
// Generate the title tag.
// Pre-XHTML compatibility:
// This comes after meta tags because of possible
// http-equiv character set declarations.
$strHtml .= $tabs . $tab . '<title>' . $this->getTitle() . '</title>' . $lnEnd;
// Generate link declarations
foreach ($this->_links as $link) {
$strHtml .= $tabs . $tab . $link . $tagEnd . $lnEnd;
// Generate stylesheet links
foreach ($this->_styleSheets as $strSrc => $strAttr ) {
$strHtml .= $tabs . $tab . " <link rel=\"stylesheet\" href=\"$strSrc\" type=\"". $strAttr['mime']. '"';
$strHtml .= ' media="'. $strAttr['media']. '"';
$strHtml .= $tagEnd . $lnEnd;
// Generate stylesheet declarations
foreach ($this->_style as $styledecl) {
foreach ($styledecl as $type => $content) {
$strHtml .= $tabs . $tab . '<style type="' . $type . '">' . $lnEnd;
// This is for full XHTML support.
if ($this->_mime == 'text/html' ) {
$strHtml .= $tabs . $tab . $tab . '<!--' . $lnEnd;
$strHtml .= $tabs . $tab . $tab . '<![CDATA[' . $lnEnd;
// first let's propagate line endings and tabs for other HTML_Common-based objects
$content->setTabOffset (3 );
$content->setLineEnd ($lnEnd);
// now let's get a string from the object
$strHtml .= $content->toString () . $lnEnd;
PEAR ::raiseError ('Error: Style content object does not support method toString().',
$strHtml .= $content . $lnEnd;
if ($this->_mime == 'text/html' ) {
$strHtml .= $tabs . $tab . $tab . '-->' . $lnEnd;
$strHtml .= $tabs . $tab . $tab . ']]>' . $lnEnd;
$strHtml .= $tabs . $tab . '</style>' . $lnEnd;
} // end generating stylesheet blocks
// Generate script file links
foreach ($this->_scripts as $strSrc => $strType) {
$strHtml .= $tabs . $tab . " <script type=\"$strType\" src=\"$strSrc\"></script>" . $lnEnd;
// Generate script declarations
foreach ($this->_script as $script) {
foreach ($script as $type => $content) {
$strHtml .= $tabs . $tab . '<script type="' . $type . '">' . $lnEnd;
// This is for full XHTML support.
if ($this->_mime == 'text/html' ) {
$strHtml .= $tabs . $tab . $tab . '// <!--' . $lnEnd;
$strHtml .= $tabs . $tab . $tab . '<![CDATA[' . $lnEnd;
// first let's propagate line endings and tabs for other HTML_Common-based objects
$content->setTabOffset (3 );
$content->setLineEnd ($lnEnd);
// now let's get a string from the object
$strHtml .= $content->toString () . $lnEnd;
PEAR ::raiseError ('Error: Script content object does not support method toString().',
$strHtml .= $content . $lnEnd;
if ($this->_mime == 'text/html' ) {
$strHtml .= $tabs . $tab . $tab . '// -->' . $lnEnd;
$strHtml .= $tabs . $tab . $tab . '// ]]>' . $lnEnd;
$strHtml .= $tabs . $tab . '</script>' . $lnEnd;
} // end generating script blocks
$strHtml .= $tabs . '</head>' . $lnEnd;
} // end func _generateHead
* Returns the doctype declaration
require ('HTML/Page2/Doctypes.php');
if (isset ($this->_doctype['type'])) {
$type = $this->_doctype['type'];
if (isset ($this->_doctype['version'])) {
$version = $this->_doctype['version'];
if (isset ($this->_doctype['variant'])) {
$variant = $this->_doctype['variant'];
if (isset ($doctype[$type][$version][$variant][0 ])) {
foreach ( $doctype[$type][$version][$variant] as $string) {
$strDoctype .= $string. $this->_getLineEnd ();
} elseif (isset ($version)) {
if (isset ($doctype[$type][$version][0 ])) {
foreach ( $doctype[$type][$version] as $string) {
$strDoctype .= $string. $this->_getLineEnd ();
if (isset ($default[$type][$version][0 ])) {
$this->_doctype = $this->_parseDoctypeString ($default[$type][$version][0 ]);
$strDoctype = $this->_getDoctype ();
} elseif (isset ($type)) {
if (isset ($default[$type][0 ])){
$this->_doctype = $this->_parseDoctypeString ($default[$type][0 ]);
$strDoctype = $this->_getDoctype ();
$this->_doctype = $this->_parseDoctypeString ($default['default'][0 ]);
$strDoctype = $this->_getDoctype ();
PEAR ::raiseError ('Error: "'. $this->getDoctypeString(). '" is an unsupported or illegal document type.',
} // end func _getDoctype
* Retrieves the document namespace
require ('HTML/Page2/Namespaces.php');
if (isset ($this->_doctype['type'])) {
$type = $this->_doctype['type'];
if (isset ($this->_doctype['version'])) {
$version = $this->_doctype['version'];
if (isset ($this->_doctype['variant'])) {
$variant = $this->_doctype['variant'];
if (isset ($namespace[$type][$version][$variant][0 ]) && is_string($namespace[$type][$version][$variant][0 ])) {
$strNamespace = $namespace[$type][$version][$variant][0 ];
} elseif (isset ($namespace[$type][$version][0 ]) && is_string($namespace[$type][$version][0 ]) ) {
$strNamespace = $namespace[$type][$version][0 ];
} elseif (isset ($namespace[$type][0 ]) && is_string($namespace[$type][0 ]) ) {
$strNamespace = $namespace[$type][0 ];
} elseif (isset ($version)) {
if (isset ($namespace[$type][$version][0 ]) && is_string($namespace[$type][$version][0 ]) ) {
$strNamespace = $namespace[$type][$version][0 ];
} elseif (isset ($namespace[$type][0 ]) && is_string($namespace[$type][0 ]) ) {
$strNamespace = $namespace[$type][0 ];
if (isset ($namespace[$type][0 ]) && is_string($namespace[$type][0 ]) ) {
$strNamespace = $namespace[$type][0 ];
'" does not have a default namespace.' .
' Use setNamespace() to define your namespace.',
} // end func _getNamespace
* Parses a doctype declaration like "XHTML 1.0 Strict" to an array
* @param string $string The string to be parsed
function _parseDoctypeString ($string)
$elements = count($split);
$array = array ('type'=> $split[0 ],'version'=> $split[1 ],'variant'=> $split[2 ]);
} elseif (isset ($split[1 ])){
$array = array ('type'=> $split[0 ],'version'=> $split[1 ]);
$array = array ('type'=> $split[0 ]);
} // end func _parseDoctypeString
* Sets the content of the <body> tag
* <p>It is possible to add objects, strings or an array of strings
* and/or objects. Objects must have a toHtml or toString method.</p>
* <p>By default, if content already exists, the new content is appended.
* If you wish to overwrite whatever is in the body, use {@link setBody};
* {@link unsetBody} completely empties the body without inserting new
* content. You can also use {@link prependBodyContent} to prepend content
* to whatever is currently in the array of body elements.</p>
* <p>The following constants are defined to be passed as the flag
* attribute: HTML_PAGE2_APPEND, HTML_PAGE2_PREPEND and HTML_PAGE2_REPLACE.
* Their usage should be quite clear from their names.</p>
* @param mixed $content New <body> tag content (may be passed as a
* @param int $flag Determines whether to prepend, append or replace
* the content. Use pre-defined constants.
$this->_body[] = & $content;
} else { // appends content to the body
$this->_body[] = & $content;
* Adds a linked script to the page
* @param string $url URL to the linked script
* @param string $type Type of script. Defaults to 'text/javascript'
function addScript($url, $type= "text/javascript")
$this->_scripts[$url] = $type;
* Adds a script to the page
* <p>Content can be a string or an object with a toString method.
* Defaults to text/javascript.</p>
* @param mixed $content Script (may be passed as a reference)
* @param string $type Scripting mime (defaults to 'text/javascript')
} // end func addScriptDeclaration
* Adds a linked stylesheet to the page
* @param string $url URL to the linked style sheet
* @param string $type Mime encoding type
* @param string $media Media type that this stylesheet applies to
$this->_styleSheets[$url]['mime'] = $type;
$this->_styleSheets[$url]['media'] = $media;
} // end func addStyleSheet
* Adds a stylesheet declaration to the page
* <p>Content can be a string or an object with a toString method.
* Defaults to text/css.</p>
* @param mixed $content Style declarations (may be passed as a reference)
* @param string $type Type of stylesheet (defaults to 'text/css')
} // end func addStyleDeclaration
* Adds a shortcut icon (favicon)
* <p>This adds a link to the icon shown in the favorites list or on
* the left of the url in the address bar. Some browsers display
* it on the tab, as well.</p>
* @param string $href The link that is being related.
* @param string $type File type
* @param string $relation Relation of link
function addFavicon($href, $type = 'image/x-icon', $relation = 'shortcut icon') {
$this->_links[] = " <link href=\"$href\" rel=\"$relation\" type=\"$type\"";
* Adds <link> tags to the head of the document
* <p>$relType defaults to 'rel' as it is the most common relation type used.
* ('rev' refers to reverse relation, 'rel' indicates normal, forward relation.)
* Typical tag: <link href="index.php" rel="Start"></p>
* @param string $href The link that is being related.
* @param string $relation Relation of link.
* @param string $relType Relation type attribute. Either rel or rev (default: 'rel').
* @param array $attributes Associative array of remaining attributes.
function addHeadLink($href, $relation, $relType = 'rel', $attributes = array ()) {
$attributes = $this->_parseAttributes ($attributes);
$generatedTag = $this->_getAttrString ($attributes);
$generatedTag = " <link href=\"$href\" $relType=\"$relation\"" . $generatedTag;
$this->_links[] = $generatedTag;
} // end func addHeadLink
* Returns the current API version
* Disables prepending the XML prolog for XHTML documents
* <p>Normally, XHTML documents require the XML prolog to be on the first
* line of each valid document:</p>
* <?xml version="1.0" encoding="utf-8"?>
* PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
* "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
* <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
* <p>However, some browsers, most noticeably Microsoft Internet Explorer,
* have difficulties properly displaying XHTML in compatibility mode with
* this. This method is for workaround solutions, such as the infamous CSS
* <p>The opposite (and default) effect can be achieved using the
* {@link enableXmlProlog} method.</p>
* $page->disableXmlProlog();
$this->_xmlProlog = false;
} // end func disableXmlProlog
* Enables prepending the XML prolog for XHTML documents (default)
* <p>This method enables the default XHTML output, with an XML prolog on
* the first line of the document. See {@link disableXmlProlog} for more
* details on why it may or may not be advantageous to have the XML prolog
* $page->enableXmlProlog();
$this->_xmlProlog = true;
} // end func enableXmlProlog
* Returns the document charset encoding.
* Returns the document type string
$strDoctype = strtoupper($this->_doctype['type']);
if ($this->_doctype['variant']) {
return trim($strDoctype);
} // end func getDoctypeString
* Returns the document language.
* Return the title of the page.
* Prepends content to the content of the <body> tag. Wrapper for {@link addBodyContent}
* <p>If you wish to overwrite whatever is in the body, use {@link setBody};
* {@link addBodyContent} provides full functionality including appending;
* {@link unsetBody} completely empties the body without inserting new content.
* It is possible to add objects, strings or an array of strings and/or objects
* Objects must have a toString method.</p>
* @param mixed $content New <body> tag content (may be passed as a reference)
} // end func prependBodyContent
* Sets the content of the <body> tag.
* <p>If content exists, it is overwritten. If you wish to use a "safe"
* version, use {@link addBodyContent}. Objects must have a toString
* <p>This function acts as a wrapper for {@link addBodyContent}. If you
* are using PHP 4.x and would like to pass an object by reference, this
* is not the function to use. Use {@link addBodyContent} with the flag
* HTML_PAGE2_REPLACE instead.</p>
* @param mixed $content New <body> tag content. May be an object.
* (may be passed as a reference)
* Unsets the content of the <body> tag.
* Sets the attributes of the <body> tag
* <p>If attributes exist, they are overwritten. In XHTML, all attribute
* names must be lowercase. As lowercase attributes are legal in SGML, all
* attributes are automatically lowercased. This also prevents accidentally
* creating duplicate attributes when attempting to update one.</p>
* @param array $attributes <body> tag attributes.
$this->setAttributes ($attributes);
} // end setBodyAttributes
* Defines if the document should be cached by the browser
* <p>Defaults to false.</p>
* <p>A fully configurable cache header is in the works. for now, though
* if you would like to determine exactly what caching headers are sent to
* to the browser, set cache to true, and then output your own headers
* before calling {@link display}.</p>
* @param string $cache Options are currently 'true' or 'false'
* Sets the document charset
* <p>By default, HTML_Page2 uses UTF-8 encoding. This is properly handled
* by PHP, but remember to use the htmlentities attribute for charset so
* that whatever you get from a database is properly handled by the
* <p>The current most popular encoding: iso-8859-1. If it is used,
* htmlentities and htmlspecialchars can be used without any special
* @param string $type Charset encoding string
* Sets or alters the !DOCTYPE declaration.
* <p>Can be set to "strict", "transitional" or "frameset".
* Defaults to "XHTML 1.0 Transitional".</p>
* <p>This must come <i>after</i> declaring the character encoding with
* {@link setCharset} or directly when the class is initiated
* {@link HTML_Page2}. Use in conjunction with {@link setMimeEncoding}</p>
* <p>Framesets are not yet implemented.</p>
* @param string $type String containing a document type
function setDoctype($type = "XHTML 1.0 Transitional")
$this->_doctype = $this->_parseDoctypeString ($type);
if($this->_doctype['variant'] == 'frameset') {
$options = array ('master' => true );
if ($this->_doctype['type'] == 'xhtml') {
$options['xhtml'] = true;
// make sure we have the frameset class loaded
require_once 'HTML/Page2/Frameset.php';
$this->frameset = new HTML_Page2_Frameset ($options);
* Sets the <head> profile
* <p>Profiles allow for adding various uncommented links, etc. to the
* head section. For more details, see the W3C documents
* ({@link http://www.w3.org/TR/html4/struct/global.html#h-7.4.4.3}
* http://www.w3.org/TR/html4/struct/global.html#h-7.4.4.3} and
* {@link http://www.w3.org/TR/html401/types.html#type-links}
* http://www.w3.org/TR/html401/types.html#type-links})
* detailing proper use.</p>
* @param string $profile URL to profile
$this->_profile = $profile;
} // end func setHeadProfile
* Sets the global document language declaration. Default is English.
* @param string $lang Two-letter language designation
* Sets or alters a meta tag.
* @param string $name Value of name or http-equiv tag
* @param string $content Value of the content tag
* @param bool $http_equiv META type "http-equiv" defaults to null
function setMetaData($name, $content, $http_equiv = false )
if ($http_equiv == true ) {
$this->_metaTags['http-equiv'][$name] = $content;
$this->_metaTags['standard'][$name] = $content;
} // end func setMetaData
* @param string $name Value of name or http-equiv tag
* @param bool $http_equiv META type "http-equiv" defaults to null
if ($http_equiv == true ) {
unset ($this->_metaTags['http-equiv'][$name]);
unset ($this->_metaTags['standard'][$name]);
} // end func unsetMetaData
* Sets an http-equiv Content-Type meta tag
$this->setMetaData('Content-Type', $this->_mime . '; charset=' . $this->_charset , true );
} // end func setMetaContentType
* Shortcut to set or alter a refresh meta tag
* <p>If no $url is passed, "self" is presupposed, and the appropriate URL
* will be automatically generated. In this case, an optional third
* boolean parameter enables https redirects to self.</p>
* @param int $time Time till refresh (in seconds)
* @param string $url Absolute URL or "self"
* @param bool $https If $url == self, this allows for the https
* protocol defaults to null
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$this->setMetaData("Refresh", " $time; url=$url" , true );
} // end func setMetaRefresh
* Sets the document MIME encoding that is sent to the browser.
* <p>This usually will be text/html because most browsers cannot yet
* accept the proper mime settings for XHTML: application/xhtml+xml
* and to a lesser extent application/xml and text/xml. See the W3C note
* ({@link http://www.w3.org/TR/xhtml-media-types/}
* http://www.w3.org/TR/xhtml-media-types/}) for more details.</p>
* <p>Here is a possible way of automatically including the proper mime
* type for XHTML 1.0 if the requesting browser supports it:</p>
* // Initialize the HTML_Page2 object:
* require 'HTML/Page2.php';
* $page = new HTML_Page2();
* // Check if browse can take the proper mime type
* if ( strpos($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml') ) {
* $page->setDoctype('XHTML 1.0 Strict');
* $page->setMimeEncoding('application/xhtml+xml');
* // HTML that qualifies for XHTML 1.0 Strict automatically
* // also complies with XHTML 1.0 Transitional, so if the
* // requesting browser doesn't take the necessary mime type
* // for XHTML 1.0 Strict, let's give it what it can take.
* $page->setDoctype('XHTML 1.0 Transitional');
* // finish building your page here..
} // end func setMimeEncoding
* Sets the document namespace
* <p>By default, W3C namespaces are used. However, if you need to define
* your own namespace, you can set it here.</p>
* // This is how you can set your own namespace:
* $page->setNamespace('http://www.w3.org/1999/xhtml');
* // This reverts to default setting and retrieves the appropriate
* // W3C namespace for the document type:
* @param string $namespace Optional. W3C namespaces are used by default.
$this->_namespace = $namespace;
$this->_namespace = $this->_getNamespace ();
* Sets the title of the page
* $page->setTitle('My Page');
* Generates and returns the complete page as a string
* <p>This is what you would call if you want to save the page in a
* database. It creates a complete, valid HTML document, and returns
* require "HTML/Page2.php";
* $page = new HTML_Page2();
* $page->setTitle('My Page');
* $page->addBodyContent('<h1>My Page</h1>');
* $page->addBodyContent('<p>First Paragraph.</p>');
* $page->addBodyContent('<p>Second Paragraph.</p>');
* $html = $page->toHtml();
* // here you insert HTML into a database
$lnEnd = $this->_getLineEnd ();
// get the doctype declaration
$strDoctype = $this->_getDoctype ();
// This determines how the doctype is declared and enables various
// features depending on whether the the document is XHTML, HTML or
// if no doctype declaration is desired
$strHtml = '<html>' . $lnEnd;
} elseif ($this->_doctype['type'] == 'xhtml') {
// get the namespace if not already set
$this->_namespace = $this->_getNamespace ();
$strHtml = $strDoctype . $lnEnd;
$strHtml .= '<html xmlns="' . $this->_namespace . '" xml:lang="' . $this->_language . '"';
// If a special profile is defined, make sure it is included
// in the opening html tag. Normally this will not be the case.
$strHtml .= ' profile="'. $this->_profile. '"';
$strHtml .= '>' . $lnEnd;
// check whether the XML prolog should be prepended
$strHtml = '<?xml version="1.0" encoding="' . $this->_charset . '"?>' . $lnEnd . $strHtml;
$strHtml = $strDoctype . $lnEnd;
// If a special profile is defined, make sure it is included
// in the opening html tag. Normally this will not be the case.
$strHtml .= ' profile="'. $this->_profile. '"';
$strHtml .= '>' . $lnEnd;
// indent all nodes of <html> one place
$strHtml .= $this->_generateHead ();
if (isset ($this->_doctype['variant']) && $this->_doctype['variant'] == 'frameset') {
// pass on settings to the frameset
$this->frameset->setTab ($this->_getTab ());
$this->frameset->setTabOffset ($this->getTabOffset ());
$strHtml .= $this->_generateBody ();
$strHtml .= $this->_getTabs () . '</frameset>' . $lnEnd;
$strHtml .= $this->_generateBody ();
// In case something else is going to be done with this object,
// let's set the offset back to normal.
* Generates the document and outputs it to a file.
* <p>Uses {@link file_put_contents} when available. Includes a workaround
* for older versions of PHP.</p>
* require "HTML/Page2.php";
* $page = new HTML_Page2();
* $page->setTitle('My Page');
* $page->addBodyContent('<h1>My Page</h1>');
* $page->addBodyContent('<p>First Paragraph.</p>');
* $page->addBodyContent('<p>Second Paragraph.</p>');
* $page->toFile('myPage.html');
PEAR ::raiseError (" HTML_Page::toFile() error: Failed to write to $filename" ,0 ,PEAR_ERROR_TRIGGER );
* Outputs the HTML content to the browser
* <p>This method outputs to the default display device. Normally that
* will be the browser.</p>
* <p>If caching is turned off, which is the default case, this generates
* the appropriate headers:</p>
* header("Expires: Tue, 1 Jan 1980 12:00:00 GMT");
* header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
* header("Cache-Control: no-cache");
* header("Pragma: no-cache");
* <p>This functionality can be disabled:</p>
* $page->setCache('true');
// If caching is to be implemented, this bit of code will need to be
// replaced with a private function. Else it may be possible to
// borrow from Cache or Cache_Lite.
header("Expires: Tue, 1 Jan 1980 12:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache");
// Set mime type and character encoding
header('Content-Type: ' . $this->_mime . '; charset=' . $this->_charset);
// Generate HTML document
$strHtml = $this->toHTML ();
// Output to browser, screen or other default device
Documentation generated on Thu, 29 Jan 2009 09:30:08 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|