Писать отдельные модули (регистрация галлерей) не греет.
В общем решил сделать "заготовку для цмс".
И даже сумел выдрать шаблонизатор на основе цмс DLE:
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class Template {
- public $dir = '.';
- public $template = null;
- public $copy_template = null;
- public $template_parse_time = 0;
- //задаём параметры основных переменных подгрузки шаблона
- public function set($name , $var) {
- foreach ($var as $key => $key_var) {
- $this->set($key , $key_var);
- } } else $this->data[$name] = $var;
- }
- //обозначаем блоки
- public function set_block($name , $var) {
- foreach ($var as $key => $key_var) {
- $this->set_block($key , $key_var);
- } } else $this->block_data[$name] = $var;
- }
- //производим загрузку каркасного шаблона
- public function load_template($tpl_name) {
- $time_before = $this->get_real_time();
- if ($tpl_name == '' || !file_exists($this->dir . DIRECTORY_SEPARATOR . $tpl_name)) { die ("Невозможно загрузить шаблон: ". $tpl_name); return false;}
- $this->template = preg_replace( "#\\{include file=['\"](.+?)['\"]\\}#ies","\$this->sub_load_template('\\1')", $this->template);
- }
- $this->copy_template = $this->template;
- $this->template_parse_time += $this->get_real_time() - $time_before;
- return true;
- }
- // этой функцией загружаем "подшаблоны"
- public function sub_load_template($tpl_name) {
- if ($tpl_name == '' || !file_exists($this->dir . DIRECTORY_SEPARATOR . $tpl_name)) { die ("Невозможно загрузить шаблон: ". $tpl_name); return false;}
- return $template;
- }
- // очистка переменных шаблона
- public function _clear() {
- $this->copy_template = $this->template;
- }
- public function clear() {
- $this->copy_template = null;
- $this->template = null;
- }
- //полная очистка включая результаты сборки шаблона
- public function global_clear() {
- $this->copy_template = null;
- $this->template = null;
- }
- //сборка шаблона в единое целое
- public function compile($tpl) {
- $time_before = $this->get_real_time();
- foreach ($this->data as $key_find => $key_replace) {
- $find[] = $key_find;
- $replace[] = $key_replace;
- }
- foreach ($this->block_data as $key_find => $key_replace) {
- $find_preg[] = $key_find;
- $replace_preg[] = $key_replace;
- }
- }
- $this->_clear();
- $this->template_parse_time += $this->get_real_time() - $time_before;
- }
- //счётчик времени выполнения запросов сборки
- public function get_real_time()
- {
- return ((float)$seconds + (float)$microSeconds);
- }
- }
- $tpl = new Template; //инициируем класс
- $tpl->dir = 'view/tpl'; //задаём местоположение папки с шаблонами
- $tpl->load_template('main.tpl'); //загружаем каркас
- $tpl->set('{sub_templ}', $tpl->sub_load_template('sub.tpl')); //подключаем код блока
- //подставляем значения переменных
- $tpl->set('{var1}', 'блок2');
- $tpl->set('{var2}', 'блок3');
- $tpl->compile('main'); //собираем шаблон
- echo $tpl->result['main']; //выводим результат работы
- $tpl->global_clear(); //очищаем все переменные для возможно следующего шаблона
- ?>