Daeva
In Runtime
- Messages
- 407
I was digging through some of my code earlier today and I found this script I wrote a while ago that makes creating multiple pages with the same look and feel a breeze. Here's the scenario:
I was working on a very large site at the time and it would take me a while to create the pages by hand every time typing the same lines of code at the top with just minor changes. Finally I got fed up and wrote a class that renders the top portion of an html page for you, allowing you to specify parameters like whether or not to use a css file, it's location, the page encoding, meta tag attributes, javascript files, etc...
Here is the class:
Then, you just include this file at the beginning of your php document. Here is an example of it's usage:
I just thought i'd pass this along. It helped me out a lot since I don't like to use IDE's or any code aids. I suppose if you're using an editor though this is kind of useless. Let me know if you have any questions or problems getting it to work I'll be more than happy to help you out. Maybe you'll find a better use for it than I did.
Just trying to get some conversation started in this Forum since most people are pretty inactive on the programming side.
I was working on a very large site at the time and it would take me a while to create the pages by hand every time typing the same lines of code at the top with just minor changes. Finally I got fed up and wrote a class that renders the top portion of an html page for you, allowing you to specify parameters like whether or not to use a css file, it's location, the page encoding, meta tag attributes, javascript files, etc...
Here is the class:
Code:
<?php
/*
Author: David Kyle
Date: 07/16/2006
Class pageHeader is an includeable class the will maintain the same look and feel across multiple pages.
Including it at the beginning of your page will allow you to specify certain parameters to customize the rendering
and look of your page.
Defaults have been implemented to expedite the addition of new pages with minimal setup.
*/
class pageHeader { // class declaration
private $pageTitle; // html title attribute value
private $pageEncoding; // html encoding attribute value
private $stylesheet = false; // whether to use a stylesheet or not
private $styleUrl;
public $extraContent;
private $metaKeywords;
private $metaDescription;
public $scripts;
function __construct($title,$encoding) {
$this->scripts = new jScripts();
if (strlen($title) > 0) {
$this->pageTitle = $title;
} else {
$this->pageTitle = "Untitled Document";
} // end if/else
if (strlen($encoding) > 0) {
$this->pageEncoding = $encoding;
} else {
$this->pageEncoding = "utf-8";
} // end if/else
} // end no-argument constructor
public function getEncoding() {
return $this->pageEncoding;
} // end function getEncoding
public function getTitle() {
return $this->pageTitle;
} // end function getTitle
public function hasStylesheet($hasStyle) {
switch ($hasStyle) {
case 0:
$this->stylesheet = false;
return;
case 1:
$this->stylesheet = true;
return;
default:
return $this->stylesheet;
} // end switch
} // end function hasStylesheet
public function setStyleUrl($url) {
if (strlen($url) > 0) {
$this->styleUrl = $url;
} else {
$this->hasStylesheet(0);
} // end if/else
} // end function setStyleUrl
private function getStyle() {
return $this->styleUrl;
} // end function getStyle
public function setMetaTags($description, $keywords) {
$this->metaKeywords = $keywords;
$this->metaDescription = $description;
} // end function setMetaTags
public function setExtraContent($content) {
$this->extraContent = $content;
} // end function setExtraContent
public function write() {
echo "<?xml version=\"1.0\" encoding=\"".$this->getEncoding()."\"?>\n";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-us\" lang=\"en-us\" dir=\"ltr\">\n";
echo " <head>\n";
echo " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$this->getEncoding()."\" />\n";
if (strlen($this->metaKeywords) > 0) {
echo " <meta name=\"keywords\" content=\"".$this->metaKeywords."\" />\n";
} // end if
if (strlen($this->metaDescription) > 0) {
echo " <meta name=\"description\" content=\"".$this->metaDescription."\" />\n";
} // end if
echo " <title>".$this->getTitle()."</title>\n";
if ($this->hasStylesheet(-1) == true) {
echo " <link rel=\"stylesheet\" type=\"text/css\" href=\"".$this->getStyle()."\" />\n";
} else {
} // end if/else
for ($i = 0; $i<count($this->scripts->item(-1)); $i++) {
echo " <script type=\"text/javascript\" src=\"".$this->scripts->item($i)."\"></script>\n";
} // end if/else
echo " ".$this->extraContent . "\n";
echo " </head>\n";
} // end function write
} // end class pageHeader
class jScripts { // class that handles all javascripts on a page
private $scripts;
function __construct() {
$this->scripts = array();
} // end constructor
public function add($src) {
array_push($this->scripts,$src);
} // end function add
public function item($index) {
if ($index == -1) {
return $this->scripts;
} else {
return $this->scripts[$index];
} // end if/else
} // end function item
} // end class jScripts
?>
Code:
<?php
include "includes/pageHeader.php";
session_start();
$pageTop = new pageHeader("Page title","utf-8"); // where utf-8 is the page encoding
$pageTop->hasStyleSheet(true); // confirm stylesheet existence
$pageTop->setStyleUrl("Themes/main.css"); // location of stylesheet
$pageTop->scripts->add("main.js"); // add a javascript file
$pageTop->scripts->add("ajax.js");
$pageTop->write(); // output the header
?>
Just trying to get some conversation started in this Forum since most people are pretty inactive on the programming side.