Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pierwszy system szablonów - błąd związany z foreach()
Michael2318
post 16.12.2012, 14:52:53
Post #1





Grupa: Zarejestrowani
Postów: 651
Pomógł: 116
Dołączył: 3.06.2012
Skąd: Lędziny

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


Mam takie coś:

  1. <?php
  2. class template
  3. {
  4. public $dir = "templates/default/"; // katalog z szablonami
  5. public $file; // plik szablonu
  6. public $tags; // tagi
  7.  
  8. public function __construct($file, $tags = NULL)
  9. {
  10. $this->file = $file; // ustala plik
  11. foreach($tags as $tag => $tagvalue)
  12. {
  13. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  14. }
  15. }// end of construct
  16.  
  17. public function tag($tags, $value = NULL) // dodaje nowe tagi
  18. {
  19. if(!isset($value))
  20. {
  21. foreach($tags as $tag => $tagvalue)
  22. {
  23. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  24. }
  25. }
  26. else
  27. {
  28. $this->tags["{".$tag."}"] = $tagvalue; // dodaje tag
  29. }
  30. }// end of tag
  31.  
  32. public function display() // wyświetla szablon
  33. {
  34. $tmp = file_get_contents($this->dir.$this->file); // pobira zawartość
  35. $tmp = str_replace(array_keys($this->tags), array_values($this->tags), $tmp); // zamienia tagi na wartości
  36. $tmp = preg_replace("#\(\.(.*?)\.\)#", "", $tmp); // komentaarze
  37. echo $tmp; // wyświetla obrobiony wynik
  38. }// end of display
  39. }// end of template
  40. ?>


A następnie tak się do tego odwołuję:

  1. $error_type = "Bład";
  2. $szablon = new template("msg_error.tpl");
  3. $szablon->tag(array(
  4. "U_TYPE_ERROR" => $error_type,
  5. "U_MSG_ERROR" => $msg_error,
  6. "U_FILE" => $file,
  7. "U_LINE" => $line)
  8. );


Efekt:

Invalid argument supplied for foreach() in /home/majkelo/public_html/strona/includes/template.class.php on line 11

Przy czym, linia 11 to:

  1. foreach($tags as $tag => $tagvalue)


Przecież jest jak byk tablica tutaj:

  1. $szablon->tag(array(
  2. "U_TYPE_ERROR" => $error_type,
  3. "U_MSG_ERROR" => $msg_error,
  4. "U_FILE" => $file,
  5. "U_LINE" => $line)


więc czemu strzela takie 'fochy'?
Go to the top of the page
+Quote Post

Posty w temacie


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 Wersja Lo-Fi Aktualny czas: 15.07.2025 - 20:01