XSL / ШАБЛОН "МагазинСамыеПопулярныеТовары"

Заметка создана: 25 мая 2024 г.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hostcms="http://www.hostcms.ru/" exclude-result-prefixes="hostcms">
     <xsl:output xmlns="http://www.w3.org/TR/xhtml1/strict" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" encoding="UTF-8" indent="yes" method="html" omit-xml-declaration="no" version="1.0" media-type="text/xml"/>

     <!-- Шаблон показа популярных товаров.
     Основан на шаблоне "МагазинКаталогТоваровНаГлавнойСпецПред" -->

     <!-- Кол-во товаров в строку
     Если указано 0 то не разбивать, выводить все в одну строку -->
     <xsl:param name="items_per_line" select="'3'"/>

     <!--Формат вывода чисел-->
     <xsl:decimal-format name="my" decimal-separator="," grouping-separator=" "/>

     <!--Создадим ключ по идентификатору товара, чтобы оптимизировать выборку товаров при показе в предопределенном порядке -->
     <xsl:key name="items_id" match="/shop/shop_item" use="@id"/>

     <!--Признак того, что нужно показывать товары в порядке убывания популярности-->
     <xsl:variable name="is_ordered_show" select="/shop/popular/method = 'orders' and /shop/popular/item"/>


     <xsl:template match="/shop">
          <!-- Есть товары -->
          <xsl:if test="shop_item">
               <h1>Популярные товары</h1>

               <!-- Выводим товары магазина -->
               <xsl:choose>

                    <!--Если в XML существуют узлы, описывающие порядок показа товаров-->
                    <xsl:when test="$is_ordered_show">

                         <!--то запустим цикл по этим узлам-->
                         <xsl:for-each select="popular/item">
                              <xsl:sort select="@order" data-type="number" order="ascending"/>

                              <!--и для каждого id товара будем вызывать темплейт item, передавая в него узел соответствующего товара, выбранный с помощью ключа-->
                              <xsl:apply-templates select="key('items_id', .)">
                                   <xsl:with-param name="ordered_count" select="current()/@count"/>
                              </xsl:apply-templates>

                              <!--Если показываем товары в порядке убывания популярности,
                              то добавляем разбивку по N товаров в строке
                              ЗДЕСЬ.-->
                              <xsl:if test="$items_per_line &gt; 3 and position() mod $items_per_line = 3 and position() != last()">
                                   <div style="margin-top:20px;" class="clear"/>
                              </xsl:if>
                         </xsl:for-each>
                    </xsl:when>

                    <!--а в случае рандомного показа просто вызовем темплейт item для всех товаров описанных в XML-->
                    <xsl:otherwise>
                         <xsl:apply-templates select="shop_item"/>
                    </xsl:otherwise>
               </xsl:choose>

               <div class="clear"></div>
          </xsl:if>
     </xsl:template>

     <!-- Шаблон для товара -->
     <xsl:template match="shop_item">
          <div class="shop-item">
               <div class="inner">
                    <a href="{url}" title="Подробная информация // {name} // {shop_producer/name}"  class="item-images-inner">

                         <!-- Изображение для товара, если есть -->
                         <xsl:choose>
                              <xsl:when test="image_small!=''" >
                                   <img src="{dir}{image_small}" alt="{name}" class="item-image"  />
                              </xsl:when>
                              <xsl:otherwise>
                                   <img src="/images/no_img.jpg"  alt="{name}" />
                              </xsl:otherwise>
                         </xsl:choose>

                         <img src="/images/bg-item.png" alt="{name}" class="item-image-back"  />
                    </a>
                    <xsl:if test="property_value[tag_name = 'hit'] != ''">
                         <span class="hit"></span>
                    </xsl:if>

                    <!-- Название товара -->
                    <span class="price">
                         <xsl:value-of select="shop_producer/name" />
                         <!--<xsl:value-of disable-output-escaping="yes" select="name"/>--><xsl:text> / </xsl:text>

                         <xsl:if test="marking != ''">
                         <span><xsl:value-of disable-output-escaping="yes" select="marking"/></span><xsl:text> / </xsl:text>
                         </xsl:if>

                         <!-- Цена товара -->
                         <xsl:choose>
                              <xsl:when test="price != 0">
                                   <span>
                                        <xsl:variable name="price" select="price"/>
                                        <xsl:value-of select="format-number($price, '### ##0', 'my')"/>&#xA0;
                                        <!-- Валюта товара -->
                                        <xsl:value-of disable-output-escaping="yes" select="currency"/>
                                   </span>
                              </xsl:when>
                              <xsl:otherwise>
                                   <span class="shop_price">под заказ</span>
                              </xsl:otherwise>
                         </xsl:choose>

                         <!-- Если цена со скидкой - выводим ее -->
                         <xsl:if test="price_tax != price">
                              <span style="color: gray; text-decoration: line-through;">
                                   <xsl:variable name="price_tax" select="price_tax"/>
                                   <span style="font-size: 11pt">
                                        <xsl:value-of select="format-number($price_tax, '### ##0', 'my')"/>&#xA0;<xsl:value-of disable-output-escaping="yes" select="currency"/>
                                   </span>
                              </span>
                         </xsl:if>

                    </span>
               </div>
               <div class="clear"></div>
          </div>
          <!-- Перевод строки после каждого 3-го элемента -->
          <xsl:if test="((position() - 3) mod 3 = 0) and position() != last()">
               <xsl:text disable-output-escaping="yes">
                    &lt;/div&gt;
                    &lt;div class="wrapper"&gt;
               </xsl:text>
          </xsl:if>
     </xsl:template>

     <!-- Склонение после числительных -->
     <xsl:template name="declension">
          <xsl:param name="number" select="number"/>
          <!-- Именительный падеж -->
          <xsl:param name="nominative" select="nominative"/>
          <!-- Родительный падеж, единственное число -->
          <xsl:param name="genitive_singular" select="genitive_singular"/>
          <!-- Родительный падеж, множественное число -->
          <xsl:param name="genitive_plural" select="genitive_plural"/>

          <xsl:variable name="last_digit">
               <xsl:value-of select="$number mod 10"/>
          </xsl:variable>

          <xsl:variable name="last_two_digits">
               <xsl:value-of select="$number mod 100"/>
          </xsl:variable>
          <xsl:choose>
               <xsl:when test="$last_digit = 1 and $last_two_digits != 11">
                    <xsl:value-of select="$nominative"/>
               </xsl:when>
               <xsl:when test="$last_digit = 2 and $last_two_digits != 12 or $last_digit = 3 and $last_two_digits != 13 or $last_digit = 4 and $last_two_digits != 14">
                    <xsl:value-of select="$genitive_singular"/>
               </xsl:when>
               <xsl:otherwise>
                    <xsl:value-of select="$genitive_plural"/>
               </xsl:otherwise>
          </xsl:choose>
     </xsl:template>
</xsl:stylesheet>
XSL