Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Klasa Template, rozerzenie klasy
MajareQ
post
Post #1





Grupa: Zarejestrowani
Postów: 382
Pomógł: 22
Dołączył: 21.05.2007
Skąd: Elbląg

Ostrzeżenie: (0%)
-----


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;
  }
}
Go to the top of the page
+Quote Post
Cysiaczek
post
Post #2





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Przenoszę na PHP


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
MajareQ
post
Post #3





Grupa: Zarejestrowani
Postów: 382
Pomógł: 22
Dołączył: 21.05.2007
Skąd: Elbląg

Ostrzeżenie: (0%)
-----


Odświeżam temat.
Go to the top of the page
+Quote Post
Black-Berry
post
Post #4





Grupa: Zarejestrowani
Postów: 663
Pomógł: 6
Dołączył: 3.06.2007
Skąd: Kraków

Ostrzeżenie: (0%)
-----


Mógłbyś napisac jakie są cele takiej klasy bo chyba nie jestem w temacie a chciałbym smile.gif


--------------------
Go to the top of the page
+Quote Post
MajareQ
post
Post #5





Grupa: Zarejestrowani
Postów: 382
Pomógł: 22
Dołączył: 21.05.2007
Skąd: Elbląg

Ostrzeżenie: (0%)
-----


a jaki jest cel klasy Template? tongue.gif
Go to the top of the page
+Quote Post
Black-Berry
post
Post #6





Grupa: Zarejestrowani
Postów: 663
Pomógł: 6
Dołączył: 3.06.2007
Skąd: Kraków

Ostrzeżenie: (0%)
-----


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


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 06:47