• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

mehrere Frames gleichzeitig neu bestücken / Navigationsproblem

Status
Für weitere Antworten geschlossen.
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.

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;
  }
}
?>
In der Datei schreib man dann nur noch ein neues Objekt erzeugen,

PHP:
$page = new Page("Index", "Index", "Author", "Index");
den Doctype ausgeben lassen,

PHP:
echo $page->getDoctype();
zusätzliche Stylesheets hinzufügen,

PHP:
$page->addStyleSheet('css/playground.css', array('screen', 'projection'));
zusätzliche Javascripte hinzufügen,

PHP:
$page->addJavaScript('javascript/test.js');
und den generierten head ausgeben lassen.

PHP:
echo $page->getHead();
Da drunter dann noch den body und das Dokument wieder schließen.

PHP:
  <body>
    Inhalt
  </body>
</html>
Das wars ;)

Der Footer fehlt noch, den bau ich bei Gelegenheit noch ein.
 
Ich habs jetzt ähnlich gemacht. War zwar einiges zum umstellen, aber es hat sich gelohnt. Jetzt funktioniert eigentlich alles so wie es soll, und ich bin weiterhin sehr flexibel. und da reicht wirklich include funktion alleine. Danke nochmal für den Hinweis.
 
Status
Für weitere Antworten geschlossen.
Zurück
Oben