anonymous@RULINUX.NET~# Last login: 2024-05-06 15:12:44
Регистрация Вход Новости | Разметка | Пользователи | Галерея | Форум | Статьи | Неподтвержденное | Трекер | Правила форума | F.A.Q. | Ссылки | Поиск
[#] [Добавить метку] [Редактировать]
Скрыть

Касательно шаблонизатора.

Пока-что реализация выглядит так.

]<?php

final class templates extends object
{
        private $theme;
        private $filename;
        private $variables;
        private $l_delimiter = '\<!--\{';
        private $r_delimiter = '\}--\>';
        static $baseC = null;
        public function templates()
        {
                self::$baseC = new base;
                $this->variables = array();
        }
        public function set_theme($theme_id)
        {
                $where_arr = array(array("key"=>'id', "value"=>$theme_id, "oper"=>'='));
                $theme = self::$baseC->select('themes', '', '*', $where_arr);
                if (!is_dir('themes/'.$theme[0]['directory']))
                        $this->theme = $theme[0];
                else
                        $this->theme = 'default';
        }
        public function set_file($file)
        {
                $path = 'themes/default/templates/'.$file;
                if(!empty($this->theme))
                {
                        $th_path = 'themes/'.$this->theme.'/templates/'.$file;
                        if(file_exists($th_path))
                        {
                                $path = $th_path;
                        }
                }
                $this->filename = $path;
                $this->clear_variables();
        }
        public function assign($key, $value)
        {
                $this->variables[$key]=$value;
        }
        public function clear_variables()
        {
                $this->variables = array();
        }
        public function draw($block)
        {
                $variables = (object)$this->variables;
                $tpl_blocks = $this->parse_template();
                $code = $tpl_blocks[$block];
                eval(' ?>'.$code.'<?php ');
                $this->clear_variables();
        }
        private function set_delimiters($left, $right)
        {
                $this->l_delimiter = $left;
                $this->r_delimiter = $right;
        }
        private function parse_template()
        {
                $ret = array();
                $tpl_val = file_get_contents($this->filename);
                $arr = preg_split('
#'.$this->l_delimiter.'#sim', $tpl_val, -1, PREG_SPLIT_NO_EMPTY);
                foreach($arr as $value)
                {
                        $tpls_arr = preg_split('#'.$this->r_delimiter.'#sim', $value, -1, PREG_SPLIT_NO_EMPTY);
                        $ret[$tpls_arr[0]] = $tpls_arr[1];
                }
                return $ret;
        }
}
?>
 

Шаблон выглядит пока так

]
<!--{top}-->
<h1>Список зарегистрированных пользователей</h1>
<br>
<table width="100%" class="message-table">
<thead>
<tr class="msg_table_title_tr">
<th style="padding: 0px;"><span class="msg_table_title">Список пользователей</span></th>
</tr>
</thead>
<tfoot>
<tr><td></td></tr>
</tfoot>
<tbody>

<!--{middle}-->
<tr>
<td>
<img src="<?=$variables->avatar?>"  height="100" align="right" border = "2" vspace="3" hspace="3" alt="user avatar"><b>Ник:</b> <?=$variables->nick?><br>
<b>Группа:</b> <?=$variables->group_name?><br>
<b>Имя:</b> <?=$variables->name?><br>
<b>Город:</b> <?=$variables->city?><br>
<b>Страна:</b> <?=$variables->country?><br>
<b>e-mail:</b> <?=$variables->email?><br>
<b>IM:</b> <?=$variables->im?><br>
<td>
</tr>

<!--{bottom}-->
</tbody>
</table>
<div align=center><p>
<?=$variables->pages?>
<p>
</div>
 

Применять так.

]
$templatesC = new templates;
$templatesC->set_theme($uinfo['theme']);
$templatesC->set_file('users.tpl');
$templatesC->draw('top');
//...
$templatesC->clear_variables();
$templatesC->assign('pages', $pages);
$templatesC->draw('bottom');
 

Готовая страница с применением этого класса тут

Все это пока на стадии проектирования. Жду вашей конструктивной критики, советов, замечаний.

З.Ы. Переписать все на симфонии или прикрутить смарти не предлагать. Аналоги тоже :)

Tux-oid(*) (2012-05-03 13:40:36)
Отредактировано Tux-oid по причине "не указана"
Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120312 Firefox/11.0 SeaMonkey/2.8

[Ответить на это сообщение]
Этот тред читают 1 пользователь:
Анонимных: 1
Зарегистрированных: 0




(c) 2010-2020 LOR-NG Developers Group
Powered by TimeMachine

Valid HTML 4.01 Transitional Правильный CSS!