Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [klasa] System Szablonow: Kubu v.0.2
Forum PHP.pl > Inne > Oceny
Adrian Staniszewski
Wytykajcie błedy smile.gif

  1. <?php
  2. ###############################################################
  3. # Adrian Staniszewski
  4. # adrian_s@toya.net.pl
  5. # GG: 5513236
  6. #
  7. # File: Class.Template.php5
  8. # Version:  0.2
  9. # Modif:  7.11.2006
  10. # Description:  Kubu Template Class
  11. #
  12. ##############################################################
  13.  
  14. class TT_Template {
  15.  
  16. // _base id directory to template folder
  17. public $_base = 'template/';
  18. // _dir is directory to currently used template
  19. private $_dir = '';
  20. // _var is library with varibles and array
  21. public $_var = array();
  22. // _file is file library
  23. public $_file = array();
  24. // _files is currently used file
  25. private $_files = '';
  26.  
  27. public function __construct($Temp = 'base') {
  28. $this -> _dir = ''.$this -> _base.''.$Temp.'';
  29. }
  30.  
  31. // function add is adding new file to _file library
  32. public function add($file) {
  33. if(file_exists($this -> _dir.'/'.$file) && is_readable($this -> _dir.'/'.$file)) {
  34. $this -> _file[$file] = array('var' => array(), 'array' => array());
  35. // set currently used file
  36. $this -> _files = $file; 
  37. } else {
  38. echo 'Error !! Invalidy File.';
  39. }
  40. }
  41.  
  42. // function write is adding new varibles or arrays to definited file varibles libr
    ary 
  43. public function write($key, $value, $point='') {
  44.  if($point=='') $point = $this -> _files;
  45. // checks is $value is array or varible
  46. if(is_array($value)) {
  47. $this -> _var[$point]['array'][$key] = $value;
  48. } else {
  49. $this -> _var[$point]['var']['{'.$key.'}'] = $value;
  50. }
  51. }
  52.  
  53. // function getFile is getting sources of $point file
  54. private function getFile($point='') {
  55. if($point=='') $point = $this -> _files;
  56. $this -> _file[$point]['source'] = @file_get_contents($this -> _dir.'/'.$point);
  57. }
  58.  
  59. // function parseVar is replace flags for added varibles defined file
  60. private function parseVar($point='') {
  61. if($point=='') $point = $this -> _files;
  62. $this -> _file[$point]['result'] = $this -> _file[$point]['source'];
  63.  
  64. foreach ($this -> _var[$point]['var'] as $Key => $Value) {
  65. $this -> _file[$point]['result'] = str_replace($Key, $Value, $this -> _file[$point]['result']);
  66. }
  67. }
  68.  
  69. // function parseWhile is adding loop While and replace flags for added varibles d
    efined file
  70. private function parseWhile($point='') {
  71. if($point=='') $point = $this -> _files;
  72. if($this -> _file[$point]['result']=='') $this -> _file[$point]['result'] = $this -> _file[$point]['source'];
  73. // searching for While loop
  74. preg_match_all('/{while@(.*?)}(.*?){while@end}/sie', $this -> _file[$point]['result'], $doWhile);
  75. $c = 0;
  76. foreach($doWhile['1'] as $while) {
  77. $Value = $doWhile['2'][$c];
  78. $row = $this -> _var[$point]['array'][$while];
  79. $B = count($row);
  80. $b = 0; 
  81. while($b < $B) {
  82. $Result = preg_replace('/{@(.*?)}/sie', '$row[$b][$1]', $Value);
  83. $_Result .= $Result;
  84. $b++;
  85. }
  86. // saves results
  87. $this -> _file[$point]['result'] = str_replace($doWhile['0'][$c], $_Result, $this -> _file[$point]['result']);
  88.  $c++;
  89. }
  90. }
  91.  
  92. public function Parse($point='') {
  93. if($point == '') $point = $this -> _files;
  94. $this -> getFile($point);
  95. $this -> parseVar($point);
  96. $this -> parseWhile($point);
  97. echo $this -> _file[$point]['result'];
  98. }
  99.  
  100. }
  101. ?>


Przykładzik

  1. <?php
  2. $t = new TT_Template('standard');
  3. $t -> add('index2.tpl');
  4. $news[] = array('id' => '2', 'name' => 'gruszka');
  5. $news[] = array('id' => '3', 'name' => 'pomarancz');
  6. $tablica = array('jablko', 'gruszka', 'pomarancz');
  7. $t -> write('title', 'to jest moja super strona !! musisz to zobaczyc');
  8. $t -> write('meta', 'ISO-8859-2');
  9. $t -> write('news', $tablica);
  10. echo $t -> Parse();
  11. ?>


zródło :

  1. <table border="1" align="center">
  2. <tr>
  3. <td>
  4. <b>{meta}</b>
  5. </td>
  6. </tr>
  7. <tr>
  8. <td>
  9. {while@news}<b>{@id}</b> - {@name}<br />{while@end}
  10. </td>
  11. </tr>


po parsowaniu:

  1. <table border="1" align="center">
  2. <tr>
  3. <td>
  4. <b>ISO-8859-2</b>
  5. </td>
  6. </tr>
  7. <tr>
  8. <td>
  9. <b>2</b> - gruszka<br /><b>3</b> - pomarancz<br />
  10. </td>
  11. </tr>
UDAT
Kod
{while@news}<b>{@id}</b> - {@name}<br />{while@end}

  1. <?php
  2. $news[] = array('id' => '2', 'name' => 'gruszka');
  3. $news[] = array('id' => '3', 'name' => 'pomarancz');
  4. $tablica = array('jablko', 'gruszka', 'pomarancz');
  5. $t -> write('title', 'to jest moja super strona !! musisz to zobaczyc');
  6. $t -> write('meta', 'ISO-8859-2');
  7. $t -> write('news', $tablica);
  8. ?>


Nie powinno być:
  1. <?php
  2. $t -> write('news', $news);
  3. ?>



  1. <?php
  2. public $_var = array();
  3. ?>


Czemu nie dasz co najmniej protected

  1. <?php
  2. } else {
  3. echo 'Error !! Invalidy File.';
  4. }
  5. ?>


Używasz PHP5 więc użyj wyjątków

  1. <?php
  2. // _base id directory to template folder
  3. public $_base = 'template/';
  4. ?>


Jak dajesz komentarze dawaj z PHPDoc'owe
+ Mala ilość kodu
- brak cache'u
- niespójne nazewnictwo np ($this -> _dir = ''.$this -> _base.''.$Temp.'';) - małe czy duże zdecyduj się
Do prostych zastosowań fajne, do bardziej zaawansowanych nie zdatne, choć pewnie nie takie było przeznaczenie.

Edit: Chyba lepsze byłoby forum Oceny
pikey
a jezeli tam gdzie mam przykładowo "{tytul}" chciał zrobic include jakiegos pliku php to jak?
UDAT
Cytat(pikey @ 8.11.2006, 15:44:37 ) *
a jezeli tam gdzie mam przykładowo "{tytul}" chciał zrobic include jakiegos pliku php to jak?


Pobaw się ob_start" title="Zobacz w manualu php" target="_manual, ob_ get_ clean" title="Zobacz w manualu php" target="_manual, ob_ get_ contents" title="Zobacz w manualu php" target="_manual, bo ta klasa nie wspiera includowania
SHiP
Ubogo w opcje. Ot lekki parser szablonów ;] Tylko po co? Skoro inny duży kombajn wygeneruje raz cache i bedzie dzialal szybciej. Nie wiem czy dobrze widze że brakuje instrukcji warunkowych(to taki totalne minimum), przydaly by sie jeszcze modyfikatory i tablice(jeśli są to przykłady moze jakies?)
Adrian Staniszewski
Cytat(SHiP @ 8.11.2006, 19:32:13 ) *
Ubogo w opcje. Ot lekki parser szablonów ;] Tylko po co? Skoro inny duży kombajn wygeneruje raz cache i bedzie dzialal szybciej. Nie wiem czy dobrze widze że brakuje instrukcji warunkowych(to taki totalne minimum), przydaly by sie jeszcze modyfikatory i tablice(jeśli są to przykłady moze jakies?)


Spokojnie, spokojnie smile.gif
Pisałem to godzinke z kawałkiem tongue.gif

Nie uzywam php'Doc bo nigdy z niego nie korzystalem - stad teraz tez.. Na szybko napisalem wlasne komentarze, bo chcialem zobaczyc co swiat na to co napisalem. Dotychczas nie zajmowalem sie Szablonami - jest to moje pierwsze starcie z pisaniem wlasnej klasy - stad wersja 0.2.

Brak includowania jest tez dla mnie problemem dlatego postaram sie go dodac.

Klasa jest narazie malutka, ale nie kazdy potrzebuje duzy kombajn ;] A czy cach jest szybszy ? Moze, ale wyniki na poziomie 0.006sec mnie nie zalamuja ;] Cache dojdzie, ale dopiero po napisaniu kilku opcji.
nospor
Cytat
bo chcialem zobaczyc co swiat na to co napisalem
no to przeniesiemy to jeszcze na wlasciwe forum i wszystko bedzie cacy smile.gif

uzytkownik zagląda do tego dzialu z myśla ze znajdzie jakis gotowy, sprawdzony kod o jakiejs konkretnej funkcjonalnosci
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.