/admin/classes/utlTemplate.php

https://github.com/fmake/fmake · PHP · 126 lines · 99 code · 26 blank · 1 comment · 6 complexity · 71693fbee2b7b9876f4657c419995f79 MD5 · raw file

  1. <?php
  2. class utlTemplate
  3. {
  4. public static $tplExt = ".tpl";
  5. public static $tplDir = "template";
  6. public static $phpExt = ".php";
  7. public static $phpDir = "temp";
  8. public $data = null;
  9. public $content = null;
  10. function __construct() {}
  11. public function getFileData($tpl_file) // ���������� ������ �� �����
  12. {
  13. //echo $tpl_file."<br />";
  14. if (!is_file($tpl_file))
  15. $tpl_file = self::$tplDir.DIRECTORY_SEPARATOR.$tpl_file.self::$tplExt;
  16. if (!is_file($tpl_file))
  17. die("utlTemplate::parse() - {$tpl_file} file not found.");
  18. return $this->data = file_get_contents($tpl_file);
  19. }
  20. public function parse($data = null) // ������� �������
  21. {
  22. $tpl_content = ($data)?$data:$this->data;
  23. preg_match_all("~\{(.*?)\}~si", $tpl_content, $matches);
  24. foreach($matches[1] as $var)
  25. {
  26. switch(TRUE)
  27. {
  28. case ereg("^\\$([_a-zA-z0-9\(\)\'\>\-]*)$", $var, $var): /* ������ ���������� */
  29. $replace = "<?PHP echo $" . $var[1] . "; ?>";
  30. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  31. break;
  32. case ereg("^Include \"(.*)\"$", $var, $var): /* ������� ������� */
  33. switch(TRUE)
  34. {
  35. case ereg("^\\$([\/_a-zA-z0-9\'\>\-]*)$", $var[1], $varNew):
  36. $replace = $this->parse($this->getFileData($GLOBALS[$varNew[1]]));
  37. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  38. break;
  39. case ereg("^([\/_a-zA-z0-9\-]*)$", $var[1], $varNew):
  40. $replace = $this->parse($this->getFileData($varNew[1]));
  41. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  42. break;
  43. }
  44. break;
  45. case ereg("^If \"(.*)\"$", $var, $var): /* ���������� ��������� IF */
  46. $replace = "<?PHP if (" . $var[1] . ") { ?>";
  47. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  48. break;
  49. case ereg("^elseIf$", $var, $var): /* ������ ���������� ��������� IF */
  50. $replace = "<?PHP } else { ?>";
  51. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  52. break;
  53. case ereg("^Foreach \"(.*)\"$", $var, $var): /* ���������� ��������� FOREACH */
  54. $replace = "<?PHP foreach (" . $var[1] . ") { ?>";
  55. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  56. break;
  57. case ereg("^While \"(.*)\"$", $var, $var): /* ���������� ��������� FOREACH */
  58. $replace = "<?PHP while (" . $var[1] . ") { ?>";
  59. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  60. break;
  61. case ereg("^For \"(.*)\"$", $var, $var): /* ���������� ��������� FOR */
  62. $replace = "<?PHP for (" . $var[1] . ") { ?>";
  63. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  64. break;
  65. case ereg("^endIf$", $var, $var): /* ������ ���������� ��������� IF */
  66. case ereg("^endForeach$", $var, $var): /* ������ ���������� ��������� FOREACH */
  67. case ereg("^endWhile$", $var, $var): /* ������ ���������� ��������� WHILE */
  68. case ereg("^endFor$", $var, $var): /* ������ ���������� ��������� FOR */
  69. case ereg("^\/$", $var, $var): /* ������� ������*/
  70. $replace = "<?PHP } ?>";
  71. $tpl_content = str_replace('{' . $var[0] . '}' , $replace, $tpl_content);
  72. break;
  73. }
  74. }
  75. return $this->content = $tpl_content;
  76. }
  77. function display()
  78. {
  79. $temp_file = ROOT.DIRECTORY_SEPARATOR.self::$phpDir.DIRECTORY_SEPARATOR.microtime().self::$phpExt;
  80. $handle = fopen($temp_file, 'w+');
  81. fwrite($handle, $this->content);
  82. fclose($handle);
  83. reset($GLOBALS);
  84. foreach($GLOBALS as $key => $value)
  85. $$key = $value;
  86. include($temp_file);
  87. unlink($temp_file);
  88. }
  89. function display_file($file_name)
  90. {
  91. $this->parse($this->getFileData($file_name));
  92. $this->display();
  93. }
  94. function display_data($data)
  95. {
  96. $this->parse($data);
  97. $this->display();
  98. }
  99. }
  100. ?>