123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- class View
- {
- private $data;
- private $template;
- private $layout;
- private $tool_path;
-
- public function __construct($toolname = '', $template_path = null)
- {
- if (!empty($toolname)) {
- if (isset($template_path)) {
- $path = $template_path.$toolname.'/';
- } else {
- $path = api_get_path(SYS_CODE_PATH).$toolname.'/';
- }
- if (is_dir($path)) {
- $this->tool_path = $path;
- } else {
- throw new Exception('View::__construct() $path directory does not exist '.$path);
- }
- }
- }
-
- public function set_data($data)
- {
- if (!is_array($data)) {
- throw new Exception('View::set_data() $data must to be an array, you have sent a'.gettype($data));
- }
- $this->data = $data;
- }
-
- public function set_layout($layout)
- {
- $this->layout = $layout;
- }
-
- public function set_template($template)
- {
- $this->template = $template;
- }
-
- public function render()
- {
- $content = $this->render_template();
- $target = $this->tool_path.$this->layout.'.php';
- if (file_exists($target)) {
- require_once $target;
- } else {
- throw new Exception('View::render() invalid file path '.$target);
- }
- }
-
- private function render_template()
- {
- $target = $this->tool_path.$this->template.'.php';
- if (file_exists($target)) {
- ob_start();
- @extract($this->data, EXTR_OVERWRITE);
- require_once $target;
- $content = ob_get_clean();
- return $content;
- } else {
- throw new Exception('View::render_template() invalid file path '.$target);
- }
- }
- }
|