View file lib/template.php

File size: 2.79Kb
<?
class template {

        protected $vars;
        protected $template_path;

        public function assign($data_vars) {
                $this->SetVars($data_vars);
        }

        protected function SetVars($data_vars) {
                if (!empty($data_vars) && is_array($data_vars)) {
                        foreach ($data_vars as $key => $value) {
                                $this->vars[$key] = $value;
                        }
                }
        }

        public function display($file_template, $data_vars = array()) {
                if (file_exists($this->template_path . $file_template)) {

                        # Сохранение переменных
                        $this->SetVars($data_vars);

                        # Распаковка массива в переменные
                        if (!empty($this->vars))
                                extract($this->vars, EXTR_REFS);

                        ob_start();
                        include ($this->template_path . $file_template);
                        echo $this->carline(ob_get_clean());
                } else {
                        die('Шаблон <b>' . $file_template . '</b> не найден');
                }
        }

        protected function carline($html) {
                return preg_replace('#(.*)$#i', "$1". PHP_EOL, $html);
        }

}

class pattern extends template {
        
        public function display($file, $data_vars = array()) {
                $MainConfig = Registry :: get('config');

                ######################
                # Конфигурация темы
                ######################
                if ($MainConfig['themes']) {
                        $them_inifile = ROOT . 'themes/' . $MainConfig['themes'] . '/config.ini';

                        if (file_exists($them_inifile)) {
                                $them_config = parse_ini_file($them_inifile);
                        } else {
                                $them_config['them_template_dir'] = $MainConfig['themes'];
                        }
                }

                ######################
                if (strpos($file, '.tpl') === false)
                        $file .= '.tpl';
                
                if (file_exists(ROOT . 'themes/' . $MainConfig['themes'] . '/' . $file)) {
                        $this->template_path = ROOT . 'themes/' . $MainConfig['themes'] . '/';
                } else {
                        $this->template_path = ROOT . 'modules/' . ROUTE_MODULE_NAME . '/view/' . $them_config['them_template_dir'] . '/';
                }
                
                ######################
                parent :: display($file, $data_vars);
        }

}

?>