Home » HTML » HTML_Page2 » Manual
API to create HTML Pages
Creation
Creation – Making a new page
Description
A HTML page is created by instantiating a new HTML_Page2 object. While you may pass an array of attributes to the constructor, it isn't required since the default settings are fairly intuitive.
- Unix line endings (\n)
-
UTF-8charset -
text/htmlMIME type -
XHTML 1.0 Transitionaldoctype - English language
If you don't like these settings, you can either pass an array of settings to the constructor or use setter methods such as setDoctype(), setMimeEncoding() or setCharset() .
Creating a HTML page
<?php
require_once 'HTML/Page2.php';
$page = new HTML_Page2();
$page->setTitle('My first HTML page');
$page->addBodyContent('<h1>Hello!</h1>');
$page->display();
?>
Headers
Headers – Setting title and other meta data
Setting headers
The method you surely will use is setTitle() that sets the title of the page; the one that your browser displays in its title/window bar.
<?php
$page->setTitle('My first HTML page');
?>
Meta tags can be set with setMetaData() which takes two parameters, the tag name and the content.
<?php
$page->setMetaData('author', 'Christian Weiske');
?>
A link to a bookmark icon (favicon) can be set using addFavicon() .
Links to related pages (previous, next, home etc.) are added with addHeadLink() - the first parameter determines the URL, the second the relation.
<?php
$page->addHeadLink('home.htm', 'Start');
?>
Adding CSS - Cascading stylesheets
A HTML page may contain two types of stylesheets: Inline
css and a link to external .css file.
Inline CSS declarations are made with addStyleDeclaration()
<?php
$page->addStyleDeclaration('
div.block {
border: 1px solid #000;
}
');
?>
Links to external CSS files are created by using addStyleSheet(). The optional third parameter determines which media type the stylesheet shall be used for.
<?php
$page->addStyleSheet('css/dark.css');
$page->addStyleSheet('css/print.css', 'text/css', 'print');
?>
Adding scripts
You may want to add some javascript code to your page; either inline
or link to an external .js file.
Inline code blocks are added with
addScriptDeclaration()
. The optional second parameter defaults to text/javascript
but can be changed as needed.
<?php
$page->addScriptDeclaration('
function javaScriptTest() {
alert("Test");
}
');
?>
Links to external script files are created by using addScript().
<?php
$page->addScript('js/functions.js');
?>
Body
Body – Adding content
Description
After creating the page and setting headers, you want to add actual content to the page. The method in charge is addBodyContent().
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.
<?php
$page->addBodyContent('<h1>Hello!</h1>');
$page->addBodyContent('<p>This is a paragraph.</p>');
?>
Output
Output – Finishing the page
Description
After adding all the headers and content to the page, you want to get the page to the user. display() directly echoes the page to the browser.
If you want to do something with the result page before pushing it to the browser, use toHtml() . It returns the page as string that can be modified further.
<?php
//push it to the browser
$page->display();
//or put it into a variable
$html = $page->toHtml();
?>