This class allows you to easily manipulate every aspect of an HTML page while it is being created. I wrote this class to be the main foundation for all of the code that I write. With this class you will be able to:

  • Change or add anything to the head section of your html page at any time
    • Add css and javascript files
    • Include a favicon.ico image
    • Set, and or retrieve the page's title
    • Define the description and keyword meta tags
    • Include unlimited custom meta data
    • Define the page's character encoding
    • Switch easily between html and xhtml doctypes
    • Declare a page's doctype to be strict, transitional, or frameset
    • Allow or disallow robots from crawling your site
  • Insert code into the body tag of your page
    • Include onload handlers
    • Make style declarations
  • Give a page access permissions
    • Keep a page in the public domain
    • Only allow users who have not logged into your site (register, forgot password, etc.)
    • Only allow users who have logged into your site (logout, data entry, premium content, etc.)
    • Give administrative rights (edit code or posts, view errors, check stats, etc.)
      • Unlimited admin levels based on a number scale from 1 (all powerful) to however far down the scale you want to define access
  • Eject a user to another page
    • This method goes hand in hand with setting a page's access permissions, but also comes in quite handy with forms
  • Easily modify (or not) the query string of any url - the default being the current page
  • Create a single function that defines your page's template and delivers your html
    • Remember to never echo or print anything until you have finished with the page and can deliver it all in one fell swoop
  • Make your code legible and readible so that you can know and edit in an instant what is going on

This class allows each and every one of your pages to be truly dynamic. Something that is extremely difficult when using templates with separate header and footer files for layout.

This is the constructor for the class, and it creates a page object.

$title You can set the page title here if you already know what it's going to be.
The default is your website's name as defined in SITE_NAME.
Example
$page = new Page ('Page Title');

This sets the page's doctype, and makes the appropriate declaration for it in the head section of your html.
You can set this site wide by defining DOCTYPE, and still override it at any time by using this method.

$type Either 'html', or 'xhtml'. The default is 'html'.
$standard Either 'strict', 'transitional', or 'frameset'. The default is 'strict'.
Example
$page->doctype ('xhtml', 'transitional');

If you're particular about who views a certain page you can set permissions here.

$users Can be 'users', 'admin', or 'others'.
If 'users', only those who have signed in and have a $_SESSION['user_id'] can view the page.
If 'admin', only those who have $_SESSION['admin'] set can access the page.
If 'others', only those who have no $_SESSION['user_id'] can access the page.
$admin This is for 'admin' access, and allows you to create unlimited levels of access based on the users $_SESSION['admin'] value. The default is 1.
Example
$page->access ('users');

This is a handy method for forms especially. It sends the user somewhere else if needed.

$url Where the user is sent.
The default is an empty string which sends them to the BASE_URL.
$message What you would like to tell the user when they are ejected. Use '<br />' tags for newlines.
Example
$page->eject ('sorry.html', "It's nothing personal.");

Sets or changes the page title.

$title What you want the page title to be. Leave blank if you just want the return value.
Returns The current page title.
Example
$page->title ('PHP Page Class');

Sets the content for the meta description tag - default is the $page->title().

$description a string
Example
$page->description ('Allows you to easily manipulate every aspect of an html page while it is being created.');

Sets the content for the meta keywords tag - default is the $page->title().

$keywords a string
Example
$page->keywords ('php, html, page, class');

Tells the search engines if you would like them to crawl and index your page.

$allow Set to false if you don't want robots (crawlers) to index or follow this page. Default is true.
Example
$page->robots (false);

This declares the character set for your page.

$charset Default is 'utf-8'.
Example
$page->charset ('iso-8859-1');

Use this method to include any external files in the head section of your page.

$external_file You can link to one file or many (in an array), and you can call this method as many times as you want. If there are any duplicates they will be taken out. To include anything else such as a google maps api you can include it here, you just need to spell everything out.
$prepend Set to 'true' if you want the $external_file(s) to come first. Default is 'false'.
Example
$page->link ('/include/head/style.css'); 
$page->link (array('/include/head/layout.css', '/include/head/style.css', '/include/head/fancy.js', '/include/head/favicon.ico')); // the duplicate style.css will be thrown out 
$page->link ('<script type="text/javascript" src="http://maps.google.com/maps?file=api&v=2&key=' . $key . '"></script>');

This method allows you to aggregate all of your jQuery calls and put them all under one $(document).ready(function(){}) in the head section of your page.

$code Your jQuery code
$oneliner If you don't want this code to be a oneliner in your head section then set this to false. It comes in handy for debugging.
Example
$page->jquery('$("p.neat").addClass("ohmy").show("slow");');

Sometimes you need to put something in the body tag, so here you go.

$insert a string
Example
$page->body ('onload="googleMaps()" onunload="GUnload()"');

This method allows you to add or remove key and value pairs from a url string.

$action What you would like to do to the url. Either 'add', 'delete', 'ampify', or just leave it blank if you just want the current url returned.
$url The url string, if you have one. The default is the page's current url.
$key The key you either want to add, or delete.
$value The value of the key you want to add.
Returns Your modified url.
Example
$url = 'http://www.mysite.com/content.php?showme=more&now=please';  
$url = $page->url ('delete', $url, 'now');  
$url = $page->url ('add', $url, 'showme', 'themoney');  
$url = $page->url ('add', $url, 'hurryit', 'up');  
$url = $page->url (); // Returns the current page's url  
$url = $page->url ('ampify'); // Turn the &'s into &amp;'s (handy for inserting into a logout string)

Returns the entire page, so this is the last method of all that you will call.

$html The entire content of your page between the body tags. If you echo or print anything before this method call, it will screw up the whole (x)html layout. So instead of echo or print, just keep adding everything to the $html variable then echo $page->display ($html); and there you have it - fast, efficient, beautiful code.
Returns Everything from beginning to end.
Example
echo $page->display ($html);
Download