Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa Template
Forum PHP.pl > Forum > PHP
MajareQ
Mam kod (który podam za chwilę...) i chcę go rozszerzyć o obsługę ifów i Java, pomoże ktoś? Bo nie wychodzi mi zbytnio...


Kod
class Template {
  var $data;
  var $template;

  function Template () {
    $this->data = Array();
  }

  function addVar ($name, $value) {
    if (is_array($value)) {
      $this->data[$name] = Array();
      array_push($this->data[$name], $value);
    }
    else if (!empty($value)) {
      $this->data[$name] = $value;
    }
  }

  private function getIncluded ($match) {
    return implode('', file($match[1]));
  }

  private function getVars ($match) {
    return $this->data[$match[1]];
  }

  private function includeTemplates () {
    $this->template = preg_replace_callback('/{include->([^>][0-9a-zA-z._]+)}/', Array($this,'getIncluded'), $this->template);
  }

  private function includeVars () {
    $this->template = preg_replace_callback('/{([^}]+)}/', Array($this, 'getVars'), $this->template);
  }

  private function fillTable ($match) {
    $table = '<table>'."\n";

    if ((isset($match[2])) && ($match[2] != null) ) {
      $headers = $this->data[$match[2]][0];
      if (is_array($headers)) {
        $table .= '<tr>';
        foreach ($headers as $header) {
          $table .= '<th>'.$header.'</th>';
        }
        $table .= '</tr>'."\n";
      } else {
        $table .= '<tr><th>'.$headers.'</th></tr>'."\n";
      }
    }

    $data = $this->data[$match[1]][0];
    if (is_array($data)) {
      foreach ($data as $row) {
        if (is_array($row)) {
          $table .= '<tr>';
          foreach ($row as $col) {
            $table .= '<td>'.$col.'</td>';
          }
          $table .= '</tr>'."\n";
        } else {
          $table .= '<tr><td>'.$row.'</td></tr>'."\n";
        }
      }
    }
    $table .= '</table>'."\n";
    return $table;
  }

  private function printTables () {
    $this->template = preg_replace_callback('/{table->([^>][0-9a-zA-Z_]+),?([^,][0-9a-zA-Z_]+)?}/', Array($this, 'fillTable'), $this->template);
  }

  function display ($template) {
    $this->template = implode('', file($template));
    while (preg_match('/{include->([^>][0-9a-zA-z._]+)}/', $this->template)) {
      $this->includeTemplates();
    }
    $this->printTables();
    $this->includeVars();
    echo $this->template;
  }
}
Cysiaczek
Przenoszę na PHP
MajareQ
Odświeżam temat.
Black-Berry
Mógłbyś napisac jakie są cele takiej klasy bo chyba nie jestem w temacie a chciałbym smile.gif
MajareQ
a jaki jest cel klasy Template? tongue.gif
Black-Berry
Może.... wysyłanie massmaili... albo nie, już wiem - template służy do grupowania użytkowników czarodziej.gif Ha. Widzisz! nie zwiedziesz mnie:) Wiem wszystko o klasach template guitar.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.