Publishing System Settings Logout Login Register
Pear Module: HTML_Page
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on July 23rd, 2005
5197 views
PHP Coding
HTML_Page
This tutorial is about Pear's HTML_Page and how to use it

Table of Content:
1. Introduction to PEAR
2. Introduction to HTML_Page
3. Installing HTML_Page
4. Using HTML_Page

1. Introduction to PEAR
What is PEAR? (from the PEAR website)


PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:

- A structured library of open-sourced code for PHP users
- A system for code distribution and package maintenance
- A standard style for code written in PHP
- The PHP Foundation Classes (PFC)
- The PHP Extension Community Library (PECL)
- A web site, mailing lists and download mirrors to support the PHP/PEAR community

PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.


How do I install PEAR?
The installation guide on how to install PEAR

2. Introduction to HTML_Page
HTML_Page is a PEAR package, an XHTML page generator.
This package is used to generate XHTML pages easily. Doctypes, metas and stylesheets are
easy to load.


3. Installing HTML_Page
HTML_Page can be installed in many different ways. The easiest way is the PEAR package manager. (Root access is required for this)
(requires the latest version of the PEAR package manager to be installed.)

Automatic Installation:
Go to your shell and run:
$ pear install HTML_Page

Replace with the name of the package you wish to install.
PEAR package manager now downloads and installs the specified package.

Semi-Automatic Installation:
Another way to install a package offline is putting the package in your PEAR dir.
Then run the following command:
$ pear install HTML_Page.tgz

This installs the package (downloaded as a .tgz) without needing to have an internet connection.

Manual Installation:
To install the package manually, let's put the package in /includes/
Use the following code to tell PHP where to look for our package:
<?php
ini_set(\"include_path\", &#039;/var/www/www.example.com/includes/&#039;);
?>


Now we have set our include_path, we can simply require our package like this:
<?php
require_once &#039;HTML/Page.php&#039;;
?>


After installing HTML_Page, read on for the instructions on how to use it.

4. Using HTML_Page
This step of the tutorial will teach you the basics of HTML_Page and how to use it.
We will be learning:

- How to set up the doctype
- How to set meta and page title
- How to insert a stylesheet
- How to set body content and do output

Generating our page!
Below you will find the example code by PEAR. I will be explaining this bit by bit
so that you have an idea on how to use it.

<?php
require(&#039;HTML/Page.php&#039;);
$p = new HTML_Page(array (
&#039;lineend&#039; => 'unix',
'doctype' => 'XHTML 1.0 Strict',
'language' => 'en',
'cache' => 'false'
));

// Page title defaults to \"New XHTML 1.0 Strict Page\"
$p->setTitle(\"HTML_Page Color Chart example\");
$p->setMetaData(\"author\", \"Klaus Guenther\");

// Load a CSS
$p->addStyleSheet( './style.css' );

// let's add a Content-Type meta tag
$p->setMetaContentType();

$p->addBodyContent(\"<p>hello world</p>\");

// output to browser
$p->display();
// or to a file
//$p->toFile('example.html');
?>


First thing you should know is how to set the doctype and such.

$p = new HTML_Page(array (
'lineend' => 'unix',
'doctype' => 'XHTML 1.0 Strict',
'language' => 'en',
'cache' => 'false'
));

This will be initalizing the HTML_Page class. We tell it the line end, doctype and language.
Enabling cache is not recommended on really dynamic pages!!

// Page title defaults to \"New XHTML 1.0 Strict Page\"
$p->setTitle(\"Pear's HTML_Page Package Example\");
$p->setMetaData(\"author\", \"Cliff\");

This bit of code sets the title to Pear's HTML_Page Package Example.
You can set meta data using setMetaData().

// Load a CSS
$p->addStyleSheet( './style.css' );

This loads style.css from the current dir. There's not really much to explain.

// let's add a Content-Type meta tag
$p->setMetaContentType();

This actually initializes the meta content-type field.

$p->addBodyContent(\"hello world\");

The addBodyContent() function adds content into the body tag.. Use this function to
add any content to your page. Friendly reminder: You can't directly put text into when
doctype is XHTML. Put the text into a P or in a div to make it valid.


// output to browser
$p->display();

This prints the actual output, stored in the class, to the screen.
When you are done with generating the page, use this function to print the saved bits to the screen.

// or to a file
$p->toFile('example.html');

This bit of code saves the output to a .html file, usefull when trying to save things!

I hope you enjoyed reading this tutorial, actually learnt something and will put the knowledge to good use!
Tip of the day: You can use HTML_Page in combination with HTML_Template_IT, by doing $p->addBodyContent( $tpl->get );
Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Ruben K

i am totally awesome
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top