Вывод подуровней меню (подменю)

#
Re: Вывод подуровней меню (подменю)
Вот полный код шаблона меню и ниже код из макета.
В шаблон меню вставил div (возможно не туда). Исправьте пожалуйста. Я недопонимаю.


<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output xmlns="http://www.w3.org/TR/xhtml1/strict" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" encoding="Windows-1251" indent="yes" method="html" omit-xml-declaration="no" version="1.0" media-type="text/xml"/>
   
   <!-- ЛевоеМеню -->
   
   <xsl:template match="/document">
      <!-- Выбираем узлы структуры первого уровня -->
      <xsl:apply-templates select="structure[show=1]"/>
      
      <!-- Если вывод подменю -->
      
      <xsl:if test="submenu/node() and count(structure[show=1]) > 0">
         <div id="submenu_h"></div>
      </xsl:if>
   </xsl:template>
   
   <xsl:template match="structure">
      
      <!-- Запишем в константу ID структуры, данные для которой будут выводиться пользователю -->
      <xsl:variable name="current_structure_id" select="/document/structure/current_structure_id"/>
      
      <!-- Показывать ссылку, или нет -->
      <xsl:choose>
         <xsl:when test="show_link = 1">
            <!-- Определяем адрес ссылки -->
            <xsl:variable name="link">
               <xsl:choose>
                  <!-- Если внешняя ссылка -->
                  <xsl:when test="is_external_link = 1">
                     <xsl:value-of disable-output-escaping="yes" select="external_link"/>
                  </xsl:when>
                  <!-- Иначе если внутренняя ссылка -->
                  <xsl:otherwise>
                     <xsl:value-of disable-output-escaping="yes" select="link"/>
                  </xsl:otherwise>
               </xsl:choose>
            </xsl:variable>
            <!-- Ссылка на пункт меню -->
            <a href="{$link}" title="{name}">
               <!--
               Выделяем текущую страницу добавлением к <a> стиля font-weight: bold,
                  если это текущая страница, либо у нее есть ребенок с атрибутом id, равным текущей uheggt.
                  -->
                  <xsl:if test="current_structure_id = @id or count(.//structure[@id=$current_structure_id]) = 1">
                     <xsl:attribute name="style">font-weight: bold</xsl:attribute>
                  </xsl:if>
                  <xsl:value-of disable-output-escaping="yes" select="name"/>
               </a>
            </xsl:when>
            <!-- Если не показывать ссылку - выводим просто имя ссылки -->
            <xsl:otherwise>
               <xsl:value-of disable-output-escaping="yes" select="name"/>
            </xsl:otherwise>
         </xsl:choose>
         
      </xsl:template>
   </xsl:stylesheet>&#8201;



Код из макета:
      <!-- Подменю от Левого меню -->
      <?php
         $Structure = new Structure();
         $row_structure = $Structure->GetStructureItem(CURRENT_STRUCTURE_ID);
         $structure_parent_id = $row_structure['structure_parent_id'];
         $param['parent_id'] = CURRENT_STRUCTURE_ID;

         while ($structure_parent_id!=0)
         {
            $row_structure = $Structure->GetStructureItem($structure_parent_id);
            $structure_parent_id = $row_structure['structure_parent_id'];
         }

         $param['parent_id'] = $row_structure['structure_id'];
         $Structure->ShowStructure(2,'ЛевоеМеню',$param);
      ?>
Модератор
#
Re: Вывод подуровней меню (подменю)
Учитывая, что у Вас submenu не передается, то
/document/submenu/node() and
вычеркнули.
<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output xmlns="http://www.w3.org/TR/xhtml1/strict" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" encoding="Windows-1251" indent="yes" method="html" omit-xml-declaration="no" version="1.0" media-type="text/xml"/>
  
   <!-- ЛевоеМеню -->
  
   <xsl:template match="/document">
      <!-- Выбираем узлы структуры первого уровня -->
      <xsl:apply-templates select="structure[show=1]"/>
   </xsl:template>
  
   <xsl:template match="structure">
      
      <!-- Запишем в константу ID структуры, данные для которой будут выводиться пользователю -->
      <xsl:variable name="current_structure_id" select="/document/structure/current_structure_id"/>
      
      <!-- Показывать ссылку, или нет -->
      <xsl:choose>
         <xsl:when test="show_link = 1">
            <!-- Определяем адрес ссылки -->
            <xsl:variable name="link">
               <xsl:choose>
                  <!-- Если внешняя ссылка -->
                  <xsl:when test="is_external_link = 1">
                     <xsl:value-of disable-output-escaping="yes" select="external_link"/>
                  </xsl:when>
                  <!-- Иначе если внутренняя ссылка -->
                  <xsl:otherwise>
                     <xsl:value-of disable-output-escaping="yes" select="link"/>
                  </xsl:otherwise>
               </xsl:choose>
            </xsl:variable>
            <!-- Ссылка на пункт меню -->
            <a href="{$link}" title="{name}">
               <!--
               Выделяем текущую страницу добавлением к <a> стиля font-weight: bold,
                  если это текущая страница, либо у нее есть ребенок с атрибутом id, равным текущей uheggt.
                  -->
                  <xsl:if test="current_structure_id = @id or count(.//structure[@id=$current_structure_id]) = 1">
                     <xsl:attribute name="style">font-weight: bold</xsl:attribute>
                  </xsl:if>
                  <xsl:value-of disable-output-escaping="yes" select="name"/>
               </a>
            </xsl:when>
            <!-- Если не показывать ссылку - выводим просто имя ссылки -->
            <xsl:otherwise>
               <xsl:value-of disable-output-escaping="yes" select="name"/>
            </xsl:otherwise>
         </xsl:choose>
        
      <!-- Если вывод подменю -->
      <xsl:if test="count(structure[show=1]) > 0 and current_structure_id = @id or count(.//structure[@id=$current_structure_id]) = 1">
         <div id="submenu_h">
            <xsl:apply-templates select="structure[show=1]" />
         </div>
      </xsl:if>

      </xsl:template>
   </xsl:stylesheet>
#
Re: Вывод подуровней меню (подменю)
Все отлично, стили div'a на меню отразились, только он показывает все подменю сразу. А как сделать так, чтобы он показывал только то что нужно. Например зашли в раздел 1 и он нам показал только его подразделы. И второй вопрос: сейчас подразделы вывелись в районе основного меню, хотя у меня прописан вывод подменю отдельно. Как это исправить?

   
// Вывожу так:

   <!-- Подменю от Левого меню -->
         if (CURRENT_STRUCTURE_ID != 0)
         {
            $Structure = & singleton('Structure');
            $Structure->ShowStructure(2, 'ЛевоеМеню', array('parent_id' => CURRENT_STRUCTURE_ID), array('submenu' => 1));
         }
         
// Или так:

$Structure = new Structure();
         $row_structure = $Structure->GetStructureItem(CURRENT_STRUCTURE_ID);
         $structure_parent_id = $row_structure['structure_parent_id'];
         $param['parent_id'] = CURRENT_STRUCTURE_ID;

         while ($structure_parent_id!=0)
         {
            $row_structure = $Structure->GetStructureItem($structure_parent_id);
            $structure_parent_id = $row_structure['structure_parent_id'];
         }

         $param['parent_id'] = $row_structure['structure_id'];
         $Structure->ShowStructure(2,'ЛевоеМеню',$param);
#
Re: Вывод подуровней меню (подменю)
XSL:

<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output xmlns="http://www.w3.org/TR/xhtml1/strict" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
encoding="Windows-1251" indent="yes" method="html" omit-xml-declaration="no" version="1.0" media-type="text/xml" />

<xsl:template match="/document">
   <div class="left_menu">
      <ul>
      <!-- Выбираем узлы структуры -->
      <xsl:apply-templates select="structure[show=1]"/>
      </ul>
   </div>
</xsl:template>

<xsl:template match="structure">
<li>
   <!-- Запишем в константу ID структуры, данные для которой будут выводиться пользователю -->
   <xsl:variable name="current_structure_id" select="/document/structure/current_structure_id"/>
  
      <!-- Показывать ссылку, или нет -->
      <xsl:if test="show_link = 1">
      
         <!-- Определяем адрес ссылки -->
         <xsl:variable name="link"><xsl:choose>
            <!-- Если внешняя ссылка -->
            <xsl:when test="is_external_link=1"><xsl:value-of disable-output-escaping="yes" select="external_link"/></xsl:when>
            <!-- Иначе если внутренняя ссылка -->
            <xsl:otherwise><xsl:value-of disable-output-escaping="yes" select="link"/></xsl:otherwise>
         </xsl:choose></xsl:variable>
        
         <!-- Определяем стиль вывода ссылки -->
         <xsl:variable name="link_style"><xsl:choose>
            <!-- Выделяем текущую страницу жирным (если это текущая страница, либо у нее есть ребенок с ID, равным текущей) -->
            <xsl:when test="current_structure_id=@id or count(.//structure[@id=$current_structure_id])=1">font-weight: bold; color: #0e6ad2; text-decoration: underline; </xsl:when>
            <!-- Иначе обычный вывод с пустым стилем -->
            <xsl:otherwise></xsl:otherwise>
         </xsl:choose></xsl:variable>
        
         <a href="{$link}"><span style="{$link_style}"><xsl:value-of disable-output-escaping="yes" select="name"/></span></a>
      </xsl:if>

      <!-- Если не показывать ссылку - выводим просто имя ссылки -->
      <xsl:if test="show_link=0">
         <!-- <img border="0" src="/images/menu_point.gif" class="menu_point" width="4" height="7" /> -->
         <xsl:value-of disable-output-escaping="yes" select="name"/>
      </xsl:if>

   </li>
</xsl:template>
</xsl:stylesheet>


Вызов:

<?
   $Structure = new Structure();
   $structure_parent_id = CURRENT_STRUCTURE_ID;
  
   if ( $structure_parent_id == 1 || $structure_parent_id == 5 ) { $structure_parent_id = 2; }

   while ($structure_parent_id != 0)
   {
      $row_structure = $Structure->GetStructureItem($structure_parent_id);
      $structure_parent_id = $row_structure['structure_parent_id'];
   }

   $Structure->ShowStructure(false, 'Левое меню', array('parent_id' => $row_structure['structure_id'], 'show_groups' => true));
?>
#
Re: Вывод подуровней меню (подменю)
vkharkov,
нужно не такая реализация. у вас в div'e оказывается основное меню.
#
Re: Вывод подуровней меню (подменю)
goldpiramida,
Вынесите подразделы в отдельное меню в сайте. и тогда ShowStructure( id меню, шаблон )
Модератор
#
Re: Вывод подуровней меню (подменю)
goldpiramida,
поправил свой пример выше, доп. условие выделено полужирным.
#
Re: Вывод подуровней меню (подменю)
Все почти так. За это вам уже большое спасибо. Остался один нюанс: подразделы выводяться в районе основного левого меню, а мне надо в отдельном месте. Для этого в макете прописан отдельный вывод. Возможно с ним что-то не так и его надо корректировать:

1 вариант:

<!-- Подменю от Левого меню -->
<?php
if (CURRENT_STRUCTURE_ID != 0)
{
   $Structure = & singleton('Structure');
   $Structure->ShowStructure(2, 'ЛевоеМеню', array('parent_id' => CURRENT_STRUCTURE_ID), array('submenu' => 1));
}         
?>

2 вариант:

<?php
$Structure = new Structure();
$row_structure = $Structure->GetStructureItem(CURRENT_STRUCTURE_ID);
$structure_parent_id = $row_structure['structure_parent_id'];
$param['parent_id'] = CURRENT_STRUCTURE_ID;
while ($structure_parent_id!=0)
{
   $row_structure = $Structure->GetStructureItem($structure_parent_id);
   $structure_parent_id = $row_structure['structure_parent_id'];
}
$param['parent_id'] = $row_structure['structure_id'];
$Structure->ShowStructure(2,'ЛевоеМеню',$param);
?>



А вывод основного меню такой:


<!-- Левое меню -->
<?php
   $Structure = & singleton('Structure');
   $Structure->ShowStructure(2, 'ЛевоеМеню');
?>
#
Re: Вывод подуровней меню (подменю)
подскажите пожалуйста по выводу
Авторизация