Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] System Szablonow: Kubu v.0.2
Adrian Staniszew...
post 7.11.2006, 12:50:29
Post #1





Grupa: Zarejestrowani
Postów: 94
Pomógł: 0
Dołączył: 17.10.2006
Skąd: Łódź

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


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>


Ten post edytował Adrian Staniszewski 7.11.2006, 12:56:28


--------------------
Programy antywirusowe
Komputery, internet
Twój Słownik OnLine
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: 19.07.2025 - 04:18