[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Шаблонизатор dle (вывод подменю)
maragon
Ситуация следующая, не могу вывести подменю в меню(dle шаблонизатор).

Шаблонизатор:

class template {
var $dir = '.';
var $template = null;
var $copy_template = null;
var $data = array ();
var $block_data = array ();
var $result = array ('error-top' => '', 'error-bottom' => '', 'menu' => '', 'content' => '' );
var $template_parse_time = 0;

function set($name, $var) {
if( is_array( $var ) && count( $var ) ) {
foreach ( $var as $key => $key_var ) {
$this->set( $key, $key_var );
}
}
else
$this->data[$name] = $var;
}

function set_block($name, $var) {
if( is_array( $var ) && count( $var ) ) {
foreach ( $var as $key => $key_var ) {
$this->set_block( $key, $key_var );
}
}
else
$this->block_data[$name] = $var;
}

function load_template($tpl_name) {

$time_before = $this->get_real_time();

if( $tpl_name == '' || ! file_exists( $this->dir . DIRECTORY_SEPARATOR . $tpl_name ) ) {
die( "Невозможно загрузить шаблон: " . $this->dir . DIRECTORY_SEPARATOR . $tpl_name );
return false;
}

$this->template = file_get_contents( $this->dir . DIRECTORY_SEPARATOR . $tpl_name );
$this->template = str_replace('{theme}', HOST . '/templates/'. TEMPLATE, $this->template);
$this->copy_template = $this->template;
$this->template_parse_time += $this->get_real_time() - $time_before;
return true;
}

function sub_load_template( $tpl_name ) {

$tpl_name = ( $tpl_name );

if( $tpl_name == '' || ! file_exists( $this->dir . DIRECTORY_SEPARATOR . $tpl_name ) ) {
return "Отсутствует файл шаблона: " . $tpl_name ;
return false;
}
$template .= file_get_contents( $this->dir . DIRECTORY_SEPARATOR . $tpl_name );

return $template;
}

function _clear() {

$this->data = array ();
$this->block_data = array ();
$this->copy_template = $this->template;

}

function clear() {
$this->data = array ();
$this->block_data = array ();
$this->copy_template = null;
$this->template = null;

}

function global_clear() {
$this->data = array ();
$this->block_data = array ();
$this->result = array ();
$this->copy_template = null;
$this->template = null;

}

function compile($tpl) {

$time_before = $this->get_real_time();

if( count( $this->block_data ) ) {
foreach ( $this->block_data as $key_find => $key_replace ) {
$find_preg[] = $key_find;
$replace_preg[] = $key_replace;
}

$this->copy_template = preg_replace( $find_preg, $replace_preg, $this->copy_template );
}

foreach ( $this->data as $key_find => $key_replace ) {
$find[] = $key_find;
$replace[] = $key_replace;
}

$find = isset($find) ? $find : null;
$replace = isset($replace) ? $replace : null;

$this->copy_template = str_replace($find, $replace, $this->copy_template );
$this->copy_template = $this->copy_template;
if( isset( $this->result[$tpl] ) ) $this->result[$tpl] .= $this->copy_template;
else $this->result[$tpl] = $this->copy_template;
$this->_clear();
$this->template_parse_time += $this->get_real_time() - $time_before;
}

function get_real_time() {
list ( $seconds, $microSeconds ) = explode( ' ', microtime() );
return (( float ) $seconds + ( float ) $microSeconds);
}
}



Скриптик:

$menu = $db->super_query("SELECT `id`,`title`,`link`,`submenu`,`status` FROM `".PREFIX."_menu` ORDER by `position` ASC", 1);
if($menu){
foreach($menu As $row){

//Собираем в кучу подменю
$sql = $db->super_query("SELECT `id`,`title`,`link` FROM `".PREFIX."_submenu` WHERE `menu_id` = '".$row['id']."' ORDER by `position` ASC", 1);
if($sql){
foreach($sql AS $rows){
$tpl_module->load_template('menu/submenu.tpl');
$tpl_module->set('{id}', $rows['id']);
$tpl_module->set('{title}', $rows['title']);
$tpl_module->set('{link}', $rows['link']);
$tpl_module->compile('submenu');
}
}


//Собираем в кучу основное меню
$tpl_module->load_template('menu/a.tpl');
if(!empty($row['submenu'])){
$tpl_module->set('[submenu]', '');
$tpl_module->set('[/submenu]', '');
} else {
$tpl_module->set_block("'\\[submenu\\](.*?)\\[/submenu\\]'si","");
}
$tpl_module->set('{id}', $row['id']);
$tpl_module->set('{title}', $row['title']);
$tpl_module->set('{link}', $row['link']);
if($row['status'] == 1) {
$tpl_module->set('[disable]', '');
$tpl_module->set('[/disable]', '');
} else {
$tpl_module->set_block("'\\[disable\\](.*?)\\[/disable\\]'si","");
}
$tpl_module->set('{submenu}', isset($tpl_module->result['submenu']) ? $tpl_module->result['submenu'] : null); //Вывод подменю
$tpl_module->compile('a');
}

//Собираем основной шаблон меню
$tpl_module->load_template('menu/menu.tpl');
$tpl_module->set('{menu}', isset($tpl_module->result['a']) ? $tpl_module->result['a'] : null); //Вывод меню и их подменю
$tpl_module->compile('content');


На выходе:
# Главная
# Новости
> Зарубежные
> Спортивные
> Еще

# Тестовое меню
> Зарубежные
> Спортивные
> Еще

> Еще 2
> Еще 3
Почему-то данные дублируются.. (отмечено красным)

И еще:
Если же выводить так:

$submenu = null;
$sql = $db->super_query("SELECT ..... FROM `".PREFIX."_submenu`", 1);
foreach($sql AS $row){
$submenu .= '<div>' . $row['title'] . '</div>';
}

//Собираем в кучу основное меню
$tpl_module->load_template('menu/a.tpl');
if(!empty($row['submenu'])){
$tpl_module->set('[submenu]', '');
$tpl_module->set('[/submenu]', '');
} else {
$tpl_module->set_block("'\\[submenu\\](.*?)\\[/submenu\\]'si","");
}
$tpl_module->set('{id}', $row['id']);
$tpl_module->set('{title}', $row['title']);
$tpl_module->set('{link}', $row['link']);
if($row['status'] == 1) {
$tpl_module->set('[disable]', '');
$tpl_module->set('[/disable]', '');
} else {
$tpl_module->set_block("'\\[disable\\](.*?)\\[/disable\\]'si","");
}
$tpl_module->set('{submenu}', isset($submenu) ? $submenu : null); //Вывод подменю ($submenu -> выше)
$tpl_module->compile('a');
}

//Собираем основной шаблон меню
$tpl_module->load_template('menu/menu.tpl');
$tpl_module->set('{menu}', isset($tpl_module->result['a']) ? $tpl_module->result['a'] : null); //Вывод меню и их подменю
$tpl_module->compile('content');

Tо все прекрасно выводится. Но не хочется смешивать php с html. Есть же какой-то выход.
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.