fname=$fname; $this->BC="<$"; $this->EC="$>"; $this->AA=array(); $this->AAA=array(); $this->Load(); $this->Prepared=FALSE; } // load template if exists function Load() { if(file_exists($this->fname)){ $this->T=file($this->fname); } } function SetValue($vals)//array of values { $this->AA=array(); foreach($vals as $vk=>$vv){ $this->AA[$this->BC.$vk.$this->EC]=$vv; } $this->Prepared=FALSE; } function SetValueA($vala) { $this->AA=array(); $this->AAA=$vala; } function AddVal($var,$val) { $this->AA[$this->BC.$var.$this->EC]=$val; } function AddValA($val) { $this->AAA[]=$val; } function Prepare() { if($this->T){ $v=join("",$this->T); if(count($this->AA)>0) $this->O=strtr($v,$this->AA); else $this->O=$v; $this->Prepared=TRUE; } } function PrepareA() { if($this->T){ $v=join("",$this->T); $ret=""; if(count($this->AAA)>0){ foreach($this->AAA as $i){ $this->SetValue($i); if(count($this->AA)>0) $ret.=strtr($v,$this->AA); else $ret.=$v; } } else $ret=$v; $this->O=$ret; $this->Prepared=TRUE; } } function Out() { if($this->Prepared==FALSE) { if(count($this->AAA)>0)$this->PrepareA(); else $this->Prepare(); } if($this->Prepared==FALSE) return ""; ob_start(); eval("?>".$this->O.""); $out = ob_get_contents(); ob_end_clean(); return $out; } /*public function block($block, array $data = NULL) { if (file_exists($block)) { if ($data !== NULL) extract($data); ob_start(); require $block; $out = ob_get_contents(); ob_end_clean(); return $out; } else { return 'File ' . $block . ' not exists.'; } }*/ /* 1._template = $template; 12. } 13. else 14. { 15. exit('File ' . $template . ' not exists.'); 16. } 17. } 18. 19. public function __set($key, $value) 20. { 21. $this->_data[$key] = $value; 22. } 23. 24. 1. public function block($template_block, array $data = NULL) 2. { 3. if (file_exists($template_block)) 4. { 5. if ($data !== NULL) extract($data, EXTR_SKIP); 6. ob_start(); 7. require $template_block; 8. $out = ob_get_contents(); 9. ob_end_clean(); 10. return $out; 11. } 12. else 13. { 14. return 'File ' . $template_block . ' not exists.'; 15. } 16. } 40. 41. public function display() 42. { 43. extract($this->_data); 44. require ($this->_template); 45. } 46. 47.} 48.?> 1.title = 'Тест шаблонизатора'; 8.$view->hello = 'Добро пожаловать!'; 9.// создаем массив данных для блока меню 10.$data_menu = array( 11. 'block_name' => 'Блок меню', 12. 'links' => array( 13. 'index.php' => 'Главная', 14. 'news.php' => 'Новости', 15. 'about.php' => 'Контакты', 16. ) 17.); 18.// создаем сам блок меню из подшаблона menu.php и массива данных $data_menu 19.$view->block_menu = $view->block('views/menu.php', $data_menu); 20.// выводим все на экран 21.$view->display(); 22.?> 1. 2. 3.
4. 5.