Просмотр файла includes/template.php

Размер файла: 7.55Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. class Template {
  7. var $classname = "Template";
  8. var $_tpldata = array();
  9. var $files = array();
  10. var $root = "";
  11. var $compiled_code = array();
  12. var $uncompiled_code = array();
  13.  
  14. function Template($root = ".")
  15. {
  16. $this->set_rootdir($root);
  17. }
  18.  
  19. function destroy()
  20. {
  21. $this->_tpldata = array();
  22. }
  23.  
  24. function set_rootdir($dir)
  25. {
  26. if (!is_dir($dir))
  27. {
  28. return false;
  29. }
  30.  
  31. $this->root = $dir;
  32. return true;
  33. }
  34.  
  35. function set_filenames($filename_array)
  36. {
  37. if (!is_array($filename_array))
  38. {
  39. return false;
  40. }
  41.  
  42. reset($filename_array);
  43. while(list($handle, $filename) = each($filename_array))
  44. {
  45. $this->files[$handle] = $this->make_filename($filename);
  46. }
  47.  
  48. return true;
  49. }
  50.  
  51. function pparse($handle)
  52. {
  53. if (!$this->loadfile($handle))
  54. {
  55. die("Template->pparse(): Couldn't load template file for handle $handle");
  56. }
  57.  
  58. if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle]))
  59. {
  60. $this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]);
  61. }
  62. eval($this->compiled_code[$handle]);
  63. return true;
  64. }
  65.  
  66. function assign_var_from_handle($varname, $handle)
  67. {
  68. if (!$this->loadfile($handle))
  69. {
  70. die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle");
  71. }
  72. $_str = "";
  73. $code = $this->compile($this->uncompiled_code[$handle], true, '_str');
  74. eval($code);
  75. $this->assign_var($varname, $_str);
  76.  
  77. return true;
  78. }
  79.  
  80. function assign_block_vars($blockname, $vararray)
  81. {
  82. if (strstr($blockname, '.'))
  83. {
  84. $blocks = explode('.', $blockname);
  85. $blockcount = sizeof($blocks) - 1;
  86. $str = '$this->_tpldata';
  87. for ($i = 0; $i < $blockcount; $i++)
  88. {
  89. $str .= '[\'' . $blocks[$i] . '.\']';
  90. eval('$lastiteration = sizeof(' . $str . ') - 1;');
  91. $str .= '[' . $lastiteration . ']';
  92. }
  93.  
  94. $str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;';
  95.  
  96. eval($str);
  97. }
  98. else
  99. {
  100.  
  101. $this->_tpldata[$blockname . '.'][] = $vararray;
  102. }
  103.  
  104. return true;
  105. }
  106.  
  107. function assign_vars($vararray)
  108. {
  109. reset ($vararray);
  110. while (list($key, $val) = each($vararray))
  111. {
  112. $this->_tpldata['.'][0][$key] = $val;
  113. }
  114.  
  115. return true;
  116. }
  117.  
  118. function assign_var($varname, $varval)
  119. {
  120. $this->_tpldata['.'][0][$varname] = $varval;
  121.  
  122. return true;
  123. }
  124.  
  125. function make_filename($filename)
  126. {
  127. if (substr($filename, 0, 1) != '/')
  128. {
  129. $filename = ($rp_filename = phpbb_realpath($this->root . '/' . $filename)) ? $rp_filename : $filename;
  130. }
  131.  
  132. if (!file_exists($filename))
  133. {
  134. die("Template->make_filename(): Error - file $filename does not exist");
  135. }
  136.  
  137. return $filename;
  138. }
  139.  
  140. function loadfile($handle)
  141. {
  142. if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle]))
  143. {
  144. return true;
  145. }
  146. if (!isset($this->files[$handle]))
  147. {
  148. die("Template->loadfile(): No file specified for handle $handle");
  149. }
  150.  
  151. $filename = $this->files[$handle];
  152.  
  153. $str = implode("", @file($filename));
  154. if (empty($str))
  155. {
  156. die("Template->loadfile(): File $filename for handle $handle is empty");
  157. }
  158.  
  159. $this->uncompiled_code[$handle] = $str;
  160.  
  161. return true;
  162. }
  163.  
  164. function compile($code, $do_not_echo = false, $retvar = '')
  165. {
  166. $code = str_replace('\\', '\\\\', $code);
  167. $code = str_replace('\'', '\\\'', $code);
  168.  
  169. $varrefs = array();
  170. preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs);
  171. $varcount = sizeof($varrefs[1]);
  172. for ($i = 0; $i < $varcount; $i++)
  173. {
  174. $namespace = $varrefs[1][$i];
  175. $varname = $varrefs[3][$i];
  176. $new = $this->generate_block_varref($namespace, $varname);
  177.  
  178. $code = str_replace($varrefs[0][$i], $new, $code);
  179. }
  180.  
  181. $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . ( ( isset($this->_tpldata[\'.\'][0][\'\1\']) ) ? $this->_tpldata[\'.\'][0][\'\1\'] : \'\' ) . \'', $code);
  182. $code_lines = explode("\n", $code);
  183.  
  184. $block_nesting_level = 0;
  185. $block_names = array();
  186. $block_names[0] = ".";
  187.  
  188. $line_count = sizeof($code_lines);
  189. for ($i = 0; $i < $line_count; $i++)
  190. {
  191. $code_lines[$i] = chop($code_lines[$i]);
  192. if (preg_match('#<!-- BEGIN (.*?) -->#', $code_lines[$i], $m))
  193. {
  194. $n[0] = $m[0];
  195. $n[1] = $m[1];
  196.  
  197. if ( preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $n) )
  198. {
  199. $block_nesting_level++;
  200. $block_names[$block_nesting_level] = $m[1];
  201. if ($block_nesting_level < 2)
  202. {
  203. $code_lines[$i] = '$_' . $n[1] . '_count = ( isset($this->_tpldata[\'' . $n[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $n[1] . '.\']) : 0;';
  204. $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)';
  205. $code_lines[$i] .= "\n" . '{';
  206. }
  207. else
  208. {
  209. $namespace = implode('.', $block_names);
  210. $namespace = substr($namespace, 2);
  211. $varref = $this->generate_block_data_ref($namespace, false);
  212. $code_lines[$i] = '$_' . $n[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;';
  213. $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)';
  214. $code_lines[$i] .= "\n" . '{';
  215. }
  216. unset($block_names[$block_nesting_level]);
  217. $block_nesting_level--;
  218. $code_lines[$i] .= '} // END ' . $n[1];
  219. $m[0] = $n[0];
  220. $m[1] = $n[1];
  221. }
  222. else
  223. {
  224. $block_nesting_level++;
  225. $block_names[$block_nesting_level] = $m[1];
  226. if ($block_nesting_level < 2)
  227. {
  228. $code_lines[$i] = '$_' . $m[1] . '_count = ( isset($this->_tpldata[\'' . $m[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $m[1] . '.\']) : 0;';
  229. $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)';
  230. $code_lines[$i] .= "\n" . '{';
  231. }
  232. else
  233. {
  234. $namespace = implode('.', $block_names);
  235. $namespace = substr($namespace, 2);
  236. $varref = $this->generate_block_data_ref($namespace, false);
  237. $code_lines[$i] = '$_' . $m[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;';
  238. $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)';
  239. $code_lines[$i] .= "\n" . '{';
  240. }
  241. }
  242. }
  243. else if (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m))
  244. {
  245. unset($block_names[$block_nesting_level]);
  246. $block_nesting_level--;
  247. $code_lines[$i] = '} // END ' . $m[1];
  248. }
  249. else
  250. {
  251. if (!$do_not_echo)
  252. {
  253. $code_lines[$i] = 'echo \'' . $code_lines[$i] . '\' . "\\n";';
  254. }
  255. else
  256. {
  257. $code_lines[$i] = '$' . $retvar . '.= \'' . $code_lines[$i] . '\' . "\\n";';
  258. }
  259. }
  260. }
  261.  
  262. $code = implode("\n", $code_lines);
  263. return $code ;
  264.  
  265. }
  266.  
  267. function generate_block_varref($namespace, $varname)
  268. {
  269. $namespace = substr($namespace, 0, strlen($namespace) - 1);
  270. $varref = $this->generate_block_data_ref($namespace, true);
  271. $varref .= '[\'' . $varname . '\']';
  272. $varref = '\' . ( ( isset(' . $varref . ') ) ? ' . $varref . ' : \'\' ) . \'';
  273.  
  274. return $varref;
  275.  
  276. }
  277.  
  278. function generate_block_data_ref($blockname, $include_last_iterator)
  279. {
  280. $blocks = explode(".", $blockname);
  281. $blockcount = sizeof($blocks) - 1;
  282. $varref = '$this->_tpldata';
  283. for ($i = 0; $i < $blockcount; $i++)
  284. {
  285. $varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]';
  286. }
  287. $varref .= '[\'' . $blocks[$blockcount] . '.\']';
  288. if ($include_last_iterator)
  289. {
  290. $varref .= '[$_' . $blocks[$blockcount] . '_i]';
  291. }
  292.  
  293. return $varref;
  294. }
  295.  
  296. }
  297.  
  298. ?>