_Thor_
Aktives Mitglied
Na komm, dann aber auch richtig.
Ne schöne Klasse, die den Doctype und den Head generiert und nachträglich Stylesheets und Javascripte hinzugefügt werden können.
In der Datei schreib man dann nur noch ein neues Objekt erzeugen,
den Doctype ausgeben lassen,
zusätzliche Stylesheets hinzufügen,
zusätzliche Javascripte hinzufügen,
und den generierten head ausgeben lassen.
Da drunter dann noch den body und das Dokument wieder schließen.
Das wars ;)
Der Footer fehlt noch, den bau ich bei Gelegenheit noch ein.
Ne schöne Klasse, die den Doctype und den Head generiert und nachträglich Stylesheets und Javascripte hinzugefügt werden können.
PHP:
<?php
class Page {
private $doctype;
private $head;
private $title;
private $description;
private $author;
private $keywords;
private $css = array();
private $jscript = array();
/*
* Constructs a new Page
*
* @param title
* The title of the pase as string.
* @param description
* The description of the page as string.
* @param author
* The name of the author as sting.
* @param keywords
* Keywords associated with this page as string, separated by comma.
*/
public function __construct($title, $description, $author, $keywords) {
$this->title = $title;
$this->description = $description;
$this->author = $author;
$this->keywords = $keywords;
}
/* Returns the Doctype */
public function getDoctype() {
$this->doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"";
$this->doctype .= "\n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\n";
return $this->doctype;
}
/* Generates the head with the default stylesheets and javascripts. */
public function getHead() {
$this->head = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
$this->head .= "\t<head>\n";
$this->head .= "\t\t<title>$this->title</title>\n";
$this->head .= "\t\t<meta name=\"description\" content=\"$this->description\" />\n";
$this->head .= "\t\t<meta name=\"author\" content=\"$this->author\" />\n";
$this->head .= "\t\t<meta name=\"keywords\" content=\"$this->keywords\" />\n";
$this->head .= "\t\t<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />\n";
$this->head .= "\t\t<meta http-equiv=\"content-style-type\" content=\"text/css\" />\n";
$this->head .= "\t\t<meta http-equiv=\"content-script-type\" content=\"text/javascript\" />\n";
$this->head .= $this->printStyleSheets();
$this->head .= $this->printJavaScripts();
$this->head .= "\t</head>\n";
return $this->head;
}
/*
* Addes the given stylesheet to the head. This stylesheet is placed after the default stylesheets.
*
* @param path
* The path to the stylesheet as string.
* @param medias
* The media types for this stylesheet [f.e. addStyleSheet('css/example.css', array('screen', 'projection'))]
*/
public function addStyleSheet($path, $medias) {
if (!in_array(array($path => $medias), $this->css)) {
$this->getDefaultCSS();
$this->css[$path] = $medias;
}
}
/*
* Addes the given javascript to the head. This javascript is placed after the default javascripts.
*
* @param path
* The path to the script as string.
*/
public function addJavaScript($path) {
if (!in_array($path, $this->jscript)) {
$this->jscript[] = $path;
}
}
/* Returns an associative array with the path as key and an array of media types as value. */
private function getDefaultCSS() {
$this->css["css/main.css"] = array("screen", "projection");
$this->css["css/print.css"] = array("print");
return $this->css;
}
/* Returns an 'indexed' array with the path as value. */
private function getDefaultJS() {
$this->jscript[] = "javascript/script.js";
return $this->jscript;
}
/* Returns the stylesheets as string. */
private function printStyleSheets() {
$css = "";
foreach($this->getDefaultCSS() as $path => $value) {
$media = implode(", ", $value);
$css .= "\t\t<link type=\"text/css\" rel=\"stylesheet\" media=\"$media\" href=\"$path\" />\n";
}
return $css;
}
/* Returns the javascripts as string. */
private function printJavaScripts() {
$this->js = "";
foreach($this->getDefaultJS() as $path) {
$this->js .= "\t\t<script type=\"text/javascript\" src=\"$path\"></script>\n";
}
return $this->js;
}
}
?>
PHP:
$page = new Page("Index", "Index", "Author", "Index");
PHP:
echo $page->getDoctype();
PHP:
$page->addStyleSheet('css/playground.css', array('screen', 'projection'));
PHP:
$page->addJavaScript('javascript/test.js');
PHP:
echo $page->getHead();
PHP:
<body>
Inhalt
</body>
</html>
Der Footer fehlt noch, den bau ich bei Gelegenheit noch ein.