Advertisement
Guest User

Untitled

a guest
Nov 30th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. <?php
  2.  
  3. class Template {
  4.  
  5. public $dir = '.';
  6. public $template = null;
  7. public $copy_template = null;
  8. public $data = array();
  9. public $block_data = array();
  10. public $result = array('info' => '', 'content' => '');
  11. public $template_parse_time = 0;
  12.  
  13. //задаём параметры основных переменных подгрузки шаблона
  14.  
  15. public function set($name , $var) {
  16. if (is_array($var) && count($var)) {
  17. foreach ($var as $key => $key_var) {
  18. $this->set($key , $key_var);
  19. } } else $this->data[$name] = $var;
  20. }
  21.  
  22. //обозначаем блоки
  23.  
  24. public function set_block($name , $var) {
  25. if (is_array($var) && count($var)) {
  26. foreach ($var as $key => $key_var) {
  27. $this->set_block($key , $key_var);
  28. } } else $this->block_data[$name] = $var;
  29. }
  30.  
  31. //производим загрузку каркасного шаблона
  32.  
  33. public function load_template($tpl_name) {
  34. $time_before = $this->get_real_time();
  35. if ($tpl_name == '' || !file_exists($this->dir . DIRECTORY_SEPARATOR . $tpl_name)) { die ("Невозможно загрузить шаблон: ". $tpl_name); return false;}
  36. $this->template = file_get_contents($this->dir . DIRECTORY_SEPARATOR . $tpl_name);
  37. if ( stristr( $this->template, "{include file=" ) ) {
  38. $this->template = preg_replace( "#\\{include file=['\"](.+?)['\"]\\}#ies","\$this->sub_load_template('\\1')", $this->template);
  39. }
  40. $this->copy_template = $this->template;
  41. $this->template_parse_time += $this->get_real_time() - $time_before;
  42. return true;
  43. }
  44.  
  45. // этой функцией загружаем "подшаблоны"
  46.  
  47. public function sub_load_template($tpl_name) {
  48. if ($tpl_name == '' || !file_exists($this->dir . DIRECTORY_SEPARATOR . $tpl_name)) { die ("Невозможно загрузить шаблон: ". $tpl_name); return false;}
  49. $template = file_get_contents($this->dir . DIRECTORY_SEPARATOR . $tpl_name);
  50. return $template;
  51. }
  52.  
  53. // очистка переменных шаблона
  54. public function _clear() {
  55. $this->data = array();
  56. $this->block_data = array();
  57. $this->copy_template = $this->template;
  58. }
  59.  
  60. public function clear() {
  61. $this->data = array();
  62. $this->block_data = array();
  63. $this->copy_template = null;
  64. $this->template = null;
  65. }
  66. //полная очистка включая результаты сборки шаблона
  67. public function global_clear() {
  68. $this->data = array();
  69. $this->block_data = array();
  70. $this->result = array();
  71. $this->copy_template = null;
  72. $this->template = null;
  73. }
  74. //сборка шаблона в единое целое
  75. public function compile($tpl) {
  76. $time_before = $this->get_real_time();
  77. foreach ($this->data as $key_find => $key_replace) {
  78. $find[] = $key_find;
  79. $replace[] = $key_replace;
  80. }
  81. $result = str_replace($find, $replace, $this->copy_template);
  82. if (count($this->block_data)) {
  83. foreach ($this->block_data as $key_find => $key_replace) {
  84. $find_preg[] = $key_find;
  85. $replace_preg[] = $key_replace;
  86. }
  87. $result = preg_replace($find_preg, $replace_preg, $result);
  88. }
  89. if (isset($this->result[$tpl])) $this->result[$tpl] .= $result; else $this->result[$tpl] = $result;
  90. $this->_clear();
  91. $this->template_parse_time += $this->get_real_time() - $time_before;
  92. }
  93. //счётчик времени выполнения запросов сборки
  94. public function get_real_time()
  95. {
  96. list($seconds, $microSeconds) = explode(' ', microtime());
  97. return ((float)$seconds + (float)$microSeconds);
  98. }
  99. }
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement