HTML_Page2
[ class tree: HTML_Page2 ] [ index: HTML_Page2 ] [ all elements ]

Class: HTML_Page2

Source Location: /HTML_Page2-0.6.3/HTML/Page2.php

Class Overview

HTML_Common
   |
   --HTML_Page2

(X)HTML Page generation class


Author(s):

Version:

  • 2.0.0

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 214]
(X)HTML Page generation class

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.

The body may be a string, object, or array of objects or strings. Objects with toHtml() and toString() methods are supported.

XHTML Examples:

Simplest example:

  1.  // the default doctype is XHTML 1.0 Transitional
  2.  // All doctypes and defaults are set in HTML/Page/Doctypes.php
  3.  $p = new HTML_Page2();
  4.  
  5.  //add some content
  6.  $p->addBodyContent("<p>some text</p>");
  7.  
  8.  // print to browser
  9.  $p->display();
  10.  ?>
Complex XHTML example:

  1.  <?php
  2.  // The array takes an array of attributes that determine many important
  3.  // aspects of the page generations.
  4.  
  5.  // Possible attributes are: charset, mime, lineend, tab, doctype, namespace,
  6.  // language and cache
  7.  
  8.  $p = new HTML_Page2(array (
  9.  
  10.                           // Sets the charset encoding (default: utf-8)
  11.                           'charset'  => 'utf-8',
  12.  
  13.                           // Sets the line end character (default: unix (\n))
  14.                           'lineend'  => 'unix',
  15.  
  16.                           // Sets the tab string for autoindent (default: tab (\t))
  17.                           'tab'  => '  ',
  18.  
  19.                           // This is where you define the doctype
  20.                           'doctype'  => "XHTML 1.0 Strict",
  21.  
  22.                           // Global page language setting
  23.                           'language' => 'en',
  24.  
  25.                           // If cache is set to true, the browser may cache the output.
  26.                           'cache'    => 'false'
  27.                           ));
  28.  
  29.  // Here we go
  30.  
  31.  // Set the page title
  32.  $p->setTitle("My page");
  33.  
  34.  // Add optional meta data
  35.  $p->setMetaData("author""My Name");
  36.  
  37.  // Put something into the body
  38.  $p->addBodyContent("<p>some text</p>");
  39.  
  40.  // If at some point you want to clear the page content
  41.  // and output an error message, you can easily do that
  42.  // See the source for <a href="../HTML_Page2/HTML_Page2.html#methodtoHtml">toHtml</a> and _getDoctype
  43.  // for more details
  44.  if ($error{
  45.      $p->setTitle("Error!");
  46.      $p->setBody("<p>Houston, we have a problem: $error</p>");
  47.      $p->display();
  48.      die;
  49.  // end error handling
  50.  
  51.  // print to browser
  52.  $p->display();
  53.  // output to file
  54.  $p->toFile('example.html');
  55.  ?>
Simple XHTML declaration example:
  1.  <?php
  2.  $p = new HTML_Page2();
  3.  // An XHTML compliant page (with title) is automatically generated
  4.  
  5.  // This overrides the XHTML 1.0 Transitional default
  6.  $p->setDoctype('XHTML 1.0 Strict');
  7.  
  8.  // Put some content in here
  9.  $p->addBodyContent("<p>some text</p>");
  10.  
  11.  // print to browser
  12.  $p->display();
  13.  ?>
HTML examples:

HTML 4.01 example:

  1.  <?php
  2.  $p = new HTML_Page2('doctype="HTML 4.01 Strict"');
  3.  $p->addBodyContent = "<p>some text</p>";
  4.  $p->display();
  5.  ?>
nuke doctype declaration:

  1.  <?php
  2.  $p = new HTML_Page2('doctype="none"');
  3.  $p->addBodyContent = "<p>some text</p>";
  4.  $p->display();
  5.  ?>



[ Top ]


Class Variables

$frameset =

[line 376]

Contains an instance of {@see HTML_Page2_Frameset}
  • Since: 2.0
  • Access: public

Type:   object


[ Top ]



Method Detail

HTML_Page2 (Constructor)   [line 423]

HTML_Page2 HTML_Page2( [mixed $attributes = array()])

Class constructor.

Accepts an array of attributes

General options:

  • "lineend" => "unix|win|mac" (Sets line ending style; defaults to unix.) See also setLineEnd.
  • "tab" => string (Sets line ending style; defaults to \t.) See also setTab.
  • "cache" => "false|true" See also setCache.
  • "charset" => charset string (Sets charset encoding; defaults to utf-8) See also setCharset and getCharset.
  • "mime" => mime encoding string (Sets document mime type; defaults to text/html) See also setMimeEncoding.
XHTML specific options:

  • "doctype" => string (Sets XHTML doctype; defaults to XHTML 1.0 Transitional.) See also setDoctype.
  • "language" => two letter language designation. (Defines global document language; defaults to "en".) See also setLang.
  • "namespace" => string (Sets document namespace; defaults to the W3C defined namespace.) See also setNamespace.
  • "profile" => string (Sets head section profile) See also setHeadProfile.
  • "prolog" => bool (Enables or disables the XML prolog. This is usually unwanted, as it makes the page invalid XHTML.) See also disableXmlProlog and enableXmlProlog.
For extensive usage examples, see class-level documentation ({@see HTML_Page2}).

  • Since: 2.0
  • Access: public

Parameters:

mixed   $attributes     Associative array of table tag attributes

[ Top ]

addBodyContent   [line 938]

void addBodyContent( mixed $content, [int $flag = HTML_PAGE2_APPEND])

Sets the content of the <body> tag

It is possible to add objects, strings or an array of strings and/or objects. Objects must have a toHtml or toString method.

By default, if content already exists, the new content is appended. If you wish to overwrite whatever is in the body, use setBody; unsetBody completely empties the body without inserting new content. You can also use prependBodyContent to prepend content to whatever is currently in the array of body elements.

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.

  • Access: public

Parameters:

mixed   $content     New <body> tag content (may be passed as a reference)
int   $flag     Determines whether to prepend, append or replace the content. Use pre-defined constants.

[ Top ]

addFavicon   [line 1024]

void addFavicon( string $href, [string $type = 'image/x-icon'], [string $relation = 'shortcut icon'])

Adds a shortcut icon (favicon)

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.

  • Access: public

Parameters:

string   $href     The link that is being related.
string   $type     File type
string   $relation     Relation of link

[ Top ]

addHeadLink   [line 1042]

void addHeadLink( string $href, string $relation, [string $relType = 'rel'], [array $attributes = array()])

Adds <link> tags to the head of the document

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

  • Access: public

Parameters:

string   $href     The link that is being related.
string   $relation     Relation of link.
string   $relType     Relation type attribute. Either rel or rev (default: 'rel').
array   $attributes     Associative array of remaining attributes.

[ Top ]

addRawHeaderData   [line 1722]

void addRawHeaderData( string $content)

Adds raw data to the head of the document

Use this function to add raw data strings to the header.

  • Access: public

Parameters:

string   $content     Raw data to be added.

[ Top ]

addScript   [line 959]

void addScript( string $url, [string $type = "text/javascript"])

Adds a linked script to the page
  • Access: public

Parameters:

string   $url     URL to the linked script
string   $type     Type of script. Defaults to 'text/javascript'

[ Top ]

addScriptDeclaration   [line 975]

void addScriptDeclaration( mixed $content, [string $type = 'text/javascript'])

Adds a script to the page

Content can be a string or an object with a toString method. Defaults to text/javascript.

  • Access: public

Parameters:

mixed   $content     Script (may be passed as a reference)
string   $type     Scripting mime (defaults to 'text/javascript')

[ Top ]

addStyleDeclaration   [line 1006]

void addStyleDeclaration( mixed $content, [string $type = 'text/css'])

Adds a stylesheet declaration to the page

Content can be a string or an object with a toString method. Defaults to text/css.

  • Access: public

Parameters:

mixed   $content     Style declarations (may be passed as a reference)
string   $type     Type of stylesheet (defaults to 'text/css')

[ Top ]

addStyleSheet   [line 989]

void addStyleSheet( string $url, [string $type = 'text/css'], [string $media = null])

Adds a linked stylesheet to the page
  • Access: public

Parameters:

string   $url     URL to the linked style sheet
string   $type     Mime encoding type
string   $media     Media type that this stylesheet applies to

[ Top ]

apiVersion   [line 1055]

double apiVersion( )

Returns the current API version
  • Access: public

[ Top ]

disableXmlProlog   [line 1094]

void disableXmlProlog( )

Disables prepending the XML prolog for XHTML documents

Normally, XHTML documents require the XML prolog to be on the first line of each valid document:

  1.  <?xml version="1.0" encoding="utf-8"?>
  2.  <!DOCTYPE html
  3.      PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  4.      "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5.  
  6.  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  7.  <head>
  8.  ...
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 Box Hack.

The opposite (and default) effect can be achieved using the enableXmlProlog method.

Usage:

  1.  $page->disableXmlProlog();

  • Access: public

[ Top ]

display   [line 1689]

void display( )

Outputs the HTML content to the browser

This method outputs to the default display device. Normally that will be the browser.

If caching is turned off, which is the default case, this generates the appropriate headers:

  1.  header("Expires: Tue, 1 Jan 1980 12:00:00 GMT");
  2.  header("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  3.  header("Cache-Control: no-cache");
  4.  header("Pragma: no-cache");
This functionality can be disabled:

  1.  $page->setCache('true');

  • Access: public

[ Top ]

enableXmlProlog   [line 1116]

void enableXmlProlog( )

Enables prepending the XML prolog for XHTML documents (default)

This method enables the default XHTML output, with an XML prolog on the first line of the document. See disableXmlProlog for more details on why it may or may not be advantageous to have the XML prolog disabled.

Usage:

  1.  $page->enableXmlProlog();

  • Access: public

[ Top ]

getCharset   [line 1127]

string getCharset( )

Returns the document charset encoding.
  • Access: public

[ Top ]

getDoctypeString   [line 1138]

string getDoctypeString( )

Returns the document type string
  • Access: public

[ Top ]

getLang   [line 1154]

string getLang( )

Returns the document language.
  • Access: public

[ Top ]

getTitle   [line 1165]

string getTitle( )

Return the title of the page.
  • Access: public

[ Top ]

prependBodyContent   [line 1190]

void prependBodyContent( mixed $content)

Prepends content to the content of the <body> tag. Wrapper for addBodyContent

If you wish to overwrite whatever is in the body, use setBody; addBodyContent provides full functionality including appending; 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.

  • Access: public

Parameters:

mixed   $content     New <body> tag content (may be passed as a reference)

[ Top ]

setBody   [line 1211]

void setBody( mixed $content)

Sets the content of the <body> tag.

If content exists, it is overwritten. If you wish to use a "safe" version, use addBodyContent. Objects must have a toString method.

This function acts as a wrapper for 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 addBodyContent with the flag HTML_PAGE2_REPLACE instead.

  • Access: public

Parameters:

mixed   $content     New <body> tag content. May be an object. (may be passed as a reference)

[ Top ]

setBodyAttributes   [line 1237]

void setBodyAttributes( array $attributes)

Sets the attributes of the <body> tag

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.

  • Access: public

Parameters:

array   $attributes     <body> tag attributes.

[ Top ]

setCache   [line 1255]

void setCache( [string $cache = 'false'])

Defines if the document should be cached by the browser

Defaults to false.

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 display.

  • Access: public

Parameters:

string   $cache     Options are currently 'true' or 'false'

[ Top ]

setCharset   [line 1280]

void setCharset( [string $type = 'utf-8'])

Sets the document charset

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 browser.

The current most popular encoding: iso-8859-1. If it is used, htmlentities and htmlspecialchars can be used without any special settings.

  • Access: public

Parameters:

string   $type     Charset encoding string

[ Top ]

setDoctype   [line 1301]

void setDoctype( [string $type = "XHTML 1.0 Transitional"])

Sets or alters the !DOCTYPE declaration.

Can be set to "strict", "transitional" or "frameset". Defaults to "XHTML 1.0 Transitional".

This must come after declaring the character encoding with setCharset or directly when the class is initiated HTML_Page2. Use in conjunction with setMimeEncoding

Framesets are not yet implemented.

  • Access: public

Parameters:

string   $type     String containing a document type

[ Top ]

setHeadProfile   [line 1332]

void setHeadProfile( [string $profile = ''])

Sets the <head> profile

Profiles allow for adding various uncommented links, etc. to the head section. For more details, see the W3C documents (http://www.w3.org/TR/html4/struct/global.html#h-7.4.4.3 and http://www.w3.org/TR/html401/types.html#type-links) detailing proper use.

  • Access: public

Parameters:

string   $profile     URL to profile

[ Top ]

setLang   [line 1343]

void setLang( [string $lang = "en"])

Sets the global document language declaration. Default is English.
  • Access: public

Parameters:

string   $lang     Two-letter language designation

[ Top ]

setMetaContentType   [line 1393]

void setMetaContentType( )

Sets an http-equiv Content-Type meta tag
  • Access: public

[ Top ]

setMetaData   [line 1357]

void setMetaData( string $name, string $content, [bool $http_equiv = false])

Sets or alters a meta tag.
  • Access: public

Parameters:

string   $name     Value of name or http-equiv tag
string   $content     Value of the content tag
bool   $http_equiv     META type "http-equiv" defaults to null

[ Top ]

setMetaRefresh   [line 1412]

void setMetaRefresh( int $time, [string $url = 'self'], [bool $https = false])

Shortcut to set or alter a refresh meta tag

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.

  • Access: public

Parameters:

int   $time     Time till refresh (in seconds)
string   $url     Absolute URL or "self"
bool   $https     If $url == self, this allows for the https protocol defaults to null

[ Top ]

setMimeEncoding   [line 1465]

void setMimeEncoding( [string $type = 'text/html'])

Sets the document MIME encoding that is sent to the browser.

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 (http://www.w3.org/TR/xhtml-media-types/) for more details.

Here is a possible way of automatically including the proper mime type for XHTML 1.0 if the requesting browser supports it:

  1.  <?php
  2.  // Initialize the HTML_Page2 object:
  3.  require 'HTML/Page2.php';
  4.  $page = new HTML_Page2();
  5.  
  6.  // Check if browse can take the proper mime type
  7.  if strpos($_SERVER['HTTP_ACCEPT']'application/xhtml+xml') ) {
  8.      $page->setDoctype('XHTML 1.0 Strict');
  9.      $page->setMimeEncoding('application/xhtml+xml');
  10.  else {
  11.      // HTML that qualifies for XHTML 1.0 Strict automatically
  12.      // also complies with XHTML 1.0 Transitional, so if the
  13.      // requesting browser doesn't take the necessary mime type
  14.      // for XHTML 1.0 Strict, let's give it what it can take.
  15.      $page->setDoctype('XHTML 1.0 Transitional');
  16.  }
  17.  
  18.  // finish building your page here..
  19.  
  20.  $page->display();
  21.  ?>

  • Access: public

Parameters:

string   $type     

[ Top ]

setNamespace   [line 1491]

void setNamespace( [string $namespace = ''])

Sets the document namespace

By default, W3C namespaces are used. However, if you need to define your own namespace, you can set it here.

Usage:

  1.  // This is how you can set your own namespace:
  2.  $page->setNamespace('http://www.w3.org/1999/xhtml');
  3.  
  4.  // This reverts to default setting and retrieves the appropriate
  5.  // W3C namespace for the document type:
  6.  $page->setNamespace();

  • Access: public

Parameters:

string   $namespace     Optional. W3C namespaces are used by default.

[ Top ]

setTitle   [line 1513]

void setTitle( string $title)

Sets the title of the page

Usage:

  1.  $page->setTitle('My Page');

  • Access: public

Parameters:

string   $title     

[ Top ]

toFile   [line 1648]

void toFile( $filename)

Generates the document and outputs it to a file.

Uses http://www.php.net/file_put_contents when available. Includes a workaround for older versions of PHP.

Usage example:

  1.  <?php
  2.  require "HTML/Page2.php";
  3.  $page = new HTML_Page2();
  4.  $page->setTitle('My Page');
  5.  $page->addBodyContent('<h1>My Page</h1>');
  6.  $page->addBodyContent('<p>First Paragraph.</p>');
  7.  $page->addBodyContent('<p>Second Paragraph.</p>');
  8.  $page->toFile('myPage.html');
  9.  ?>

  • Since: 2.0
  • Access: public

Parameters:

   $filename     

[ Top ]

toHtml   [line 1542]

string toHtml( )

Generates and returns the complete page as a string

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 it as a string.

Usage example:

  1.  <?php
  2.  require "HTML/Page2.php";
  3.  $page = new HTML_Page2();
  4.  $page->setTitle('My Page');
  5.  $page->addBodyContent('<h1>My Page</h1>');
  6.  $page->addBodyContent('<p>First Paragraph.</p>');
  7.  $page->addBodyContent('<p>Second Paragraph.</p>');
  8.  $html $page->toHtml();
  9.  // here you insert HTML into a database
  10.  ?>

  • Access: public

[ Top ]

unsetBody   [line 1221]

void unsetBody( )

Unsets the content of the <body> tag.
  • Access: public

[ Top ]

unsetMetaData   [line 1378]

void unsetMetaData( string $name, [bool $http_equiv = false])

Unsets a meta tag.
  • Access: public

Parameters:

string   $name     Value of name or http-equiv tag
bool   $http_equiv     META type "http-equiv" defaults to null

[ Top ]

_elementToHtml   [line 493]

string _elementToHtml( &$element, mixed $element)

Iterates through an array, returning an HTML string

It also handles objects, calling the toHTML or toString methods and propagating the line endings and tabs for objects that extend HTML_Common.

For more details read the well-documented source.

  • Access: protected

Parameters:

mixed   $element     The element to be processed
   &$element     

[ Top ]


Documentation generated on Mon, 09 Apr 2012 18:30:05 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.