Witam, zrobiłem sobie klasę (poniżej) i nie mogę sobie poradzić z jednym problemem... jak mogę zmodyfikować tą klasę aby możliwe było dodawanie podbloków, tak jak w tej z phpBB? Jeżeli bym wiedział jak to nie musiałbym tworzyć dodatkowych metod (assign_dynamic i assign_inblock_dynamic)...

  1. <?php
  2. class Template
  3. {
  4. var $classname = 'Template';
  5. var $blocks = array();
  6. var $last_id = 0;
  7. var $vars = array();
  8. var $idynamic = array();
  9. var $dynamic = array();
  10. var $root_dir;
  11. var $filenames = array();
  12.  
  13. function set_rootdir($dir)
  14. {
  15. if(substr($dir, -1) == '/')
  16. $dir = subst($dir, 0, -1);
  17.  
  18. $this->root_dir = $dir;
  19.  
  20. return true;
  21. }
  22.  
  23. function set_filenames($arr)
  24. {
  25. if($this->root_dir == "")
  26. die($this->error('set_filenames', 'root_dir_empty'));
  27.  
  28. if(is_array($arr))
  29. {
  30. foreach($arr as $name => $file)
  31. {
  32. if(!array_key_exists($name, $this->filenames))
  33. $this->filenames[$name] = $this->root_dir.'/'.$file;
  34. }
  35. }
  36. else
  37. {
  38. die($this->error('set_filenames', 'not_array'));
  39. }
  40.  
  41. return true;
  42. }
  43.  
  44. function error($fname, $errtype)
  45. {
  46. if($errtype == 'not_array')
  47. $err = 'This is not an array';
  48. elseif($errtype == 'root_dir_empty')
  49. $err = 'Set root dir before seting filenames';
  50. else
  51. $err = $errtype;
  52.  
  53. return $this->classname.'->'.$fname.': '.$err;
  54. }
  55.  
  56. function assign_var($key, $value)
  57. {
  58. if(!array_key_exists($key, $this->vars))
  59. $this->vars[$key] = $value;
  60.  
  61. return true;
  62. }
  63.  
  64. function assign_vars($arr)
  65. {
  66. if(is_array($arr))
  67. {
  68. foreach($arr as $key => $value)
  69. {
  70. if(!array_key_exists($key, $this->vars))
  71. $this->vars[$key] = $value;
  72.  
  73. }
  74. return true;
  75. }
  76. else
  77. {
  78. die($this->error('assign_vars', 'not_array'));
  79. }
  80. }
  81.  
  82. function assign_block_vars($block = '', $arr)
  83. {
  84. if($block == '')
  85. die($this->error('assign_block_vars', 'Block name is empty'));
  86.  
  87. if(!is_array($arr))
  88. die($this->error('assign_block_vars', 'not_array'));
  89.  
  90. if(array_key_exists($block, $this->blocks))
  91. {
  92. $i = count($this->blocks[$block]);
  93. $this->last_id = $i;
  94. }
  95. else
  96. {
  97. $i = 0;
  98. }
  99.  
  100. foreach($arr as $a => $b)
  101. {
  102. $this->blocks[$block][$i][$a] = $b;
  103. }
  104. }
  105.  
  106. function assign_inblock_dynamic($dynamic)
  107. {
  108. $this->idynamic[$dynamic][$this->last_id] = true;
  109. }
  110.  
  111. function assign_dynamic($dynamic)
  112. {
  113. $this->dynamic[$dynamic] = true;
  114. }
  115.  
  116. function pparse($name)
  117. {
  118. $code = implode('', file($this->filenames[$name]));
  119. $code = addslashes($code);
  120.  
  121. #
  122. ## BEGIN - parsing blocks
  123. #
  124. foreach($this->blocks as $blockname => $variables)
  125. {
  126. if(!strpos('switch_', $blockname))
  127. {
  128. if(preg_match('#<!-- BEGIN '.$blockname.' -->(.*?)<!-- END '.$blockname.' -->#is', $code, $result))
  129. {
  130. $uncompiled_blocks[$blockname] = $result[0];
  131. for($i=0; $i<count($variables); $i++)
  132. {
  133. $found_blocks[$blockname][$i] = $result[1];
  134. }
  135. }
  136. }
  137. }
  138. foreach($found_blocks as $blockname => $variables)
  139. {
  140. # Variables is an array
  141. foreach($variables as $key => $value)
  142. {
  143. foreach($this->blocks[$blockname][$key] as $k => $v)
  144. {
  145. if($this->idynamic['switch_'.$blockname][$key] != true)
  146. {
  147. $found_blocks[$blockname][$key] = preg_replace('#<!-- BEGIN switch_'.$blockname.' -->(.*?)<!-- END switch_'.$blockname.' -->#is', '', $found_blocks[$blockname][$key]);
  148. }
  149. else
  150. {
  151. $found_blocks[$blockname][$key] = preg_replace('#<!-- BEGIN switch_'.$blockname.' -->(.*?)<!-- END switch_'.$blockname.' -->#is', '1', $found_blocks[$blockname][$key]);
  152. }
  153. $found_blocks[$blockname][$key] = str_replace('{'.$blockname.'.'.$k.'}', $v, $found_blocks[$blockname][$key]);
  154. }
  155. }
  156. }
  157. foreach($found_blocks as $blockname => $values)
  158. {
  159. $replace = implode("", $values);
  160. $code = str_replace($uncompiled_blocks[$blockname], $replace, $code);
  161. }
  162. #
  163. ## END - parsing blocks
  164. #
  165.  
  166. #
  167. ## BEGIN - parsing dynamic
  168. #
  169. foreach($this->dynamic as $dynamic => $x)
  170. {
  171. if(strpos('switch_', $dynamic))
  172. {
  173. $code = preg_replace('#<!-- BEGIN '.$dynamic.' -->(.*?)<!-- END '.$dynamic.' -->#is', '1', $code);
  174. }
  175. }
  176. #
  177. ## END - parsing dynamic
  178. #
  179.  
  180. #
  181. ## BEGIN - parsing variables
  182. #
  183. foreach($this->vars as $key => $value)
  184. {
  185. $code = str_replace('{'.$key.'}', $value, $code);
  186. }
  187. #
  188. ## END - parsing variables
  189. #
  190.  
  191. #
  192. ## Clearing dynamics
  193. #
  194. $code = preg_replace('#<!-- BEGIN switch_(.*?) -->(.*?)<!-- END switch_(.*?) -->#is', '', $code);
  195.  
  196. $code = stripslashes($code);
  197.  
  198. eval('?>'.$code.'<?');
  199. }
  200. }
  201. ?>