Skip to content
Aug 19 / kkrizka

Tutorial

Introduction

The HTMLDoc class is an unique approach to generating web pages on the fly. It allows programmers to quickly create complex and separate pages, and since it buffers all the text before it is sent out, there can be a lot of flexibility in the code. For example, pages can be embedded inside other pages, while the structure stays as valid XHTML. Also it is possible to send HTTP headers after you have “echo’ed” any output.

While the entire task is handled by the HTMLDoc class, one does not use it directly. Instead there are wrapper functions that make the whole task easier, and more efficient. For example, you would not have to create a global every time you wanted to use one of the more advanced features inside another function.

This tutorial describes only the basic usage of the HTMLDoc class to get the developer familiarized with it’s functionality. There will be more articles in the future concerning more advanced usage, but in the meantime you can check out theHTMLDoc API documentation for more information.

The Basics
The class is also very easy to use, programmers can create a full page in a few lines. Also adding meta data like RSS feeds or CSS stylesheets does not require memorizing complex tags and their arguments, and can be done in the middle of the output. Here is an example of a basic page using the HTMLDoc class:

<?php
include 'HTMLDoc.class.php';
HTMLDoc("Example");
?>
That is all!

As you can see, there is really only one important line in this file, the call of the HTMLDoc() function. This function has several responsibilities, including initialization of the HTMLDoc class and setting of the page title. It’s only argument is the title of the page. After you have called this function, you can write the rest of the page as normally. The class is already taking care of buffering all the output, and sending it to the browser at the end.

Adding External References
All modern pages link to external references like CSS stylesheets or JavaScript files. This is done by adding complex tags that are hard to remember to the HEAD section of the page, but the HTML Document class makes it much easier. The following document will link to anCSS stylesheet, RSS feed and an external JavaScript file.

<?php
include 'HTMLDoc.class.php';
HTMLDoc("Example");
?>
That is all!
<?php
echo "Let's add the style.css stylesheet!";
addStyleSheet("style.css");
echo "You can RSS this page through feed.php, but that doesn't matter because you will see it in your browser as Test Feed";
addRSSFeed("Test Feed","feed.php");
echo "Let's use the prototype library. :D ";
addJavaScript("prototype.js");
?>

As you can see, it dosn’t matter if output has been sent or not to add header information.

Adding Custom Metadata
There are many meta tags that are not supported by the HTMLDoc class because there is just too many of them. To get around this, HTMLDoc has a general function that will add custom meta tags to the HEAD section of the document. The following demonstration adds the keywords and description of a page.
<?php
include 'HTMLDoc.class.php';
HTMLDoc("Example");
addMetaData("description","This is a test page.");
addMetaData("keywords","html generation example tutorial code");
?>
That is all!

As you can see, the addMetaData() function takes two arguments. The first is the name of the data, and the second is it’s value. This will add a tag in the form of <meta name=“data name” content=“data value”/> to the HEAD section.

Leave a comment