Разбиение комментариев по страницам для интернет-магазина
Для типовой динамической страницы "Интернет-магазин" добавляем параметр с названием "Количество комментариев на странице", даем ему название переменной "CommentsOnPage" и значение по умолчанию "10".
Редактируем в структуре сайта узлы с этой динамической страницей, указываем кол-во комментариев на страницу и сохраняем.
В коде настроек типовой динамической страницы перед:
Добавляем:
В коде типовой динамической страницы вместо:
пишем:
В XSL-шаблоне для товара после:
пишем:
Работать будет с версии 5.5.4.
Редактируем в структуре сайта узлы с этой динамической страницей, указываем кол-во комментариев на страницу и сохраняем.
В коде настроек типовой динамической страницы перед:
// получаем для пути ассоциативный массив с id группы и id/url элемента
$GLOBALS['shop_item_path'] = $shop->GetItemPath($current_shop_id, '', $break_if_path_not_found);
$GLOBALS['shop_item_path'] = $shop->GetItemPath($current_shop_id, '', $break_if_path_not_found);
Добавляем:
// Нумерация для комментариев
if (ereg ("^comment-page-([0-9]*)$", end($GLOBALS['URL_ARRAY']), $regs) && to_int($regs[1]) > 0)
{
/* Страница умножается на кол-во элементов, выводимых на страницу */
$break_if_path_not_found = false;
$GLOBALS['comment-page'] = $regs[1];
}
if (ereg ("^comment-page-([0-9]*)$", end($GLOBALS['URL_ARRAY']), $regs) && to_int($regs[1]) > 0)
{
/* Страница умножается на кол-во элементов, выводимых на страницу */
$break_if_path_not_found = false;
$GLOBALS['comment-page'] = $regs[1];
}
В коде типовой динамической страницы вместо:
$shop->ShowItem($GLOBALS['shop_item_path']['item']...
пишем:
// Для тех, кто поленился в структуре отредактировать и указать
if (!isset($GLOBALS['LA']['CommentsOnPage']))
{
$GLOBALS['LA']['CommentsOnPage'] = 10;
}
$begin = to_int($GLOBALS['comment-page']);
if ($begin <= 0)
{
$begin = 0;
$param['comments_begin'] = 0;
}
else
{
$begin--;
$param['comments_begin'] = $begin * to_int($GLOBALS['LA']['CommentsOnPage']);
}
$external_propertys['CommentsCurrentPage'] = $begin;
$param['comments_count'] = to_int($GLOBALS['LA']['CommentsOnPage']);
$external_propertys['CommentsOnPage'] = $param['comments_count'];
/* Вывод конкретного элемент */
$shop->ShowItem($GLOBALS['shop_item_path']['item'], $xsl_item, $param, $external_propertys);
if (!isset($GLOBALS['LA']['CommentsOnPage']))
{
$GLOBALS['LA']['CommentsOnPage'] = 10;
}
$begin = to_int($GLOBALS['comment-page']);
if ($begin <= 0)
{
$begin = 0;
$param['comments_begin'] = 0;
}
else
{
$begin--;
$param['comments_begin'] = $begin * to_int($GLOBALS['LA']['CommentsOnPage']);
}
$external_propertys['CommentsCurrentPage'] = $begin;
$param['comments_count'] = to_int($GLOBALS['LA']['CommentsOnPage']);
$external_propertys['CommentsOnPage'] = $param['comments_count'];
/* Вывод конкретного элемент */
$shop->ShowItem($GLOBALS['shop_item_path']['item'], $xsl_item, $param, $external_propertys);
В XSL-шаблоне для товара после:
<xsl:apply-templates select="comments/comment"/>
пишем:
<xsl:variable name="count_all_comments"><xsl:choose>
<xsl:when test="comments/count_comments/node()"><xsl:value-of select="comments/count_comments"/></xsl:when>
<xsl:otherwise></xsl:otherwise></xsl:choose></xsl:variable>
<p>
<xsl:call-template name="for_page">
<xsl:with-param name="items_on_page" select="/shop/CommentsOnPage"/>
<xsl:with-param name="current_page" select="/shop/CommentsCurrentPage"/>
<xsl:with-param name="count_items" select="$count_all_comments"/>
<xsl:with-param name="visible_pages" select="5"/>
<xsl:with-param name="prefix">comment-page</xsl:with-param>
</xsl:call-template>
</p>
...
<!-- Цикл для вывода строк ссылок -->
<xsl:template name="for_page">
<xsl:param name="i" select="0"/>
<xsl:param name="prefix">page</xsl:param>
<xsl:param name="items_on_page"/>
<xsl:param name="current_page"/>
<xsl:param name="count_items"/>
<xsl:param name="visible_pages"/>
<xsl:variable name="n" select="$count_items div $items_on_page"/>
<!-- Заносим в переменную $parent_group_id идентификатор текущей группы -->
<xsl:variable name="parent_group_id" select="/shop/@current_group_id"/>
<!-- Считаем количество выводимых ссылок перед текущим элементом -->
<xsl:variable name="pre_count_page">
<xsl:choose>
<xsl:when test="$current_page > ($n - (round($visible_pages div 2) - 1))">
<xsl:value-of select="$visible_pages - ($n - $current_page)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="round($visible_pages div 2) - 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Считаем количество выводимых ссылок после текущего элемента -->
<xsl:variable name="post_count_page">
<xsl:choose>
<xsl:when test="0 > $current_page - (round($visible_pages div 2) - 1)">
<xsl:value-of select="$visible_pages - $current_page - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="round($visible_pages div 2) = ($visible_pages div 2)">
<xsl:value-of select="$visible_pages div 2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="round($visible_pages div 2) - 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="$count_items > $items_on_page and $n > $i">
<!-- Ставим ссылку на страницу-->
<xsl:if test="$i != $current_page">
<!-- Определяем адрес тэга -->
<xsl:variable name="tag_link">
<xsl:choose>
<!-- Если не нулевой уровень -->
<xsl:when test="count(/shop/selected_tags/tag) != 0">tag/<xsl:value-of select="/shop/selected_tags/tag/tag_path_name"/>/</xsl:when>
<!-- Иначе если нулевой уровень - просто ссылка на страницу со списком элементов -->
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Определяем адрес ссылки -->
<xsl:variable name="number_link">
<xsl:choose>
<!-- Если не нулевой уровень -->
<xsl:when test="$i != 0">
<xsl:value-of select="$prefix"/>-<xsl:value-of select="$i + 1"/>/</xsl:when>
<!-- Иначе если нулевой уровень - просто ссылка на страницу со списком элементов -->
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Выводим ссылку на первую страницу -->
<xsl:if test="$current_page - $pre_count_page > 0 and $i = 0">
=<xsl:value-of select="$current_page"/>=-<xsl:value-of select="$pre_count_page"/>--<xsl:value-of select="$i"/>-
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/" class="page_link" style="text-decoration: none;">←</a>
</xsl:if>
<xsl:choose>
<xsl:when test="$i >= ($current_page - $pre_count_page) and ($current_page + $post_count_page) >= $i">
<!-- Выводим ссылки на видимые страницы -->
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/{$tag_link}{$number_link}" class="page_link">
<xsl:value-of select="$i + 1"/>
</a>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<!-- Выводим ссылку на последнюю страницу -->
<xsl:if test="$i+1 >= $n and $n > ($current_page + 1 + $post_count_page)">
<xsl:choose>
<xsl:when test="$n > round($n)">
<!-- Выводим ссылку на последнюю страницу -->
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}{$prefix}-{round($n+1)}/" class="page_link" style="text-decoration: none;">→</a>
</xsl:when>
<xsl:otherwise>
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}{$prefix}-{round($n)}/" class="page_link" style="text-decoration: none;">→</a>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:if>
<!-- Не ставим ссылку на страницу-->
<xsl:if test="$i = $current_page">
<span class="current">
<xsl:value-of select="$i+1"/>
</span>
</xsl:if>
<!-- Рекурсивный вызов шаблона. НЕОБХОДИМО ПЕРЕДАВАТЬ ВСЕ НЕОБХОДИМЫЕ ПАРАМЕТРЫ! -->
<xsl:call-template name="for_page">
<xsl:with-param name="i" select="$i + 1"/>
<xsl:with-param name="prefix" select="$prefix"/>
<xsl:with-param name="items_on_page" select="$items_on_page"/>
<xsl:with-param name="current_page" select="$current_page"/>
<xsl:with-param name="count_items" select="$count_items"/>
<xsl:with-param name="visible_pages" select="$visible_pages"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
...
<xsl:when test="comments/count_comments/node()"><xsl:value-of select="comments/count_comments"/></xsl:when>
<xsl:otherwise></xsl:otherwise></xsl:choose></xsl:variable>
<p>
<xsl:call-template name="for_page">
<xsl:with-param name="items_on_page" select="/shop/CommentsOnPage"/>
<xsl:with-param name="current_page" select="/shop/CommentsCurrentPage"/>
<xsl:with-param name="count_items" select="$count_all_comments"/>
<xsl:with-param name="visible_pages" select="5"/>
<xsl:with-param name="prefix">comment-page</xsl:with-param>
</xsl:call-template>
</p>
...
<!-- Цикл для вывода строк ссылок -->
<xsl:template name="for_page">
<xsl:param name="i" select="0"/>
<xsl:param name="prefix">page</xsl:param>
<xsl:param name="items_on_page"/>
<xsl:param name="current_page"/>
<xsl:param name="count_items"/>
<xsl:param name="visible_pages"/>
<xsl:variable name="n" select="$count_items div $items_on_page"/>
<!-- Заносим в переменную $parent_group_id идентификатор текущей группы -->
<xsl:variable name="parent_group_id" select="/shop/@current_group_id"/>
<!-- Считаем количество выводимых ссылок перед текущим элементом -->
<xsl:variable name="pre_count_page">
<xsl:choose>
<xsl:when test="$current_page > ($n - (round($visible_pages div 2) - 1))">
<xsl:value-of select="$visible_pages - ($n - $current_page)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="round($visible_pages div 2) - 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Считаем количество выводимых ссылок после текущего элемента -->
<xsl:variable name="post_count_page">
<xsl:choose>
<xsl:when test="0 > $current_page - (round($visible_pages div 2) - 1)">
<xsl:value-of select="$visible_pages - $current_page - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="round($visible_pages div 2) = ($visible_pages div 2)">
<xsl:value-of select="$visible_pages div 2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="round($visible_pages div 2) - 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="$count_items > $items_on_page and $n > $i">
<!-- Ставим ссылку на страницу-->
<xsl:if test="$i != $current_page">
<!-- Определяем адрес тэга -->
<xsl:variable name="tag_link">
<xsl:choose>
<!-- Если не нулевой уровень -->
<xsl:when test="count(/shop/selected_tags/tag) != 0">tag/<xsl:value-of select="/shop/selected_tags/tag/tag_path_name"/>/</xsl:when>
<!-- Иначе если нулевой уровень - просто ссылка на страницу со списком элементов -->
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Определяем адрес ссылки -->
<xsl:variable name="number_link">
<xsl:choose>
<!-- Если не нулевой уровень -->
<xsl:when test="$i != 0">
<xsl:value-of select="$prefix"/>-<xsl:value-of select="$i + 1"/>/</xsl:when>
<!-- Иначе если нулевой уровень - просто ссылка на страницу со списком элементов -->
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Выводим ссылку на первую страницу -->
<xsl:if test="$current_page - $pre_count_page > 0 and $i = 0">
=<xsl:value-of select="$current_page"/>=-<xsl:value-of select="$pre_count_page"/>--<xsl:value-of select="$i"/>-
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/" class="page_link" style="text-decoration: none;">←</a>
</xsl:if>
<xsl:choose>
<xsl:when test="$i >= ($current_page - $pre_count_page) and ($current_page + $post_count_page) >= $i">
<!-- Выводим ссылки на видимые страницы -->
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/{$tag_link}{$number_link}" class="page_link">
<xsl:value-of select="$i + 1"/>
</a>
</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<!-- Выводим ссылку на последнюю страницу -->
<xsl:if test="$i+1 >= $n and $n > ($current_page + 1 + $post_count_page)">
<xsl:choose>
<xsl:when test="$n > round($n)">
<!-- Выводим ссылку на последнюю страницу -->
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}{$prefix}-{round($n+1)}/" class="page_link" style="text-decoration: none;">→</a>
</xsl:when>
<xsl:otherwise>
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}{$prefix}-{round($n)}/" class="page_link" style="text-decoration: none;">→</a>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:if>
<!-- Не ставим ссылку на страницу-->
<xsl:if test="$i = $current_page">
<span class="current">
<xsl:value-of select="$i+1"/>
</span>
</xsl:if>
<!-- Рекурсивный вызов шаблона. НЕОБХОДИМО ПЕРЕДАВАТЬ ВСЕ НЕОБХОДИМЫЕ ПАРАМЕТРЫ! -->
<xsl:call-template name="for_page">
<xsl:with-param name="i" select="$i + 1"/>
<xsl:with-param name="prefix" select="$prefix"/>
<xsl:with-param name="items_on_page" select="$items_on_page"/>
<xsl:with-param name="current_page" select="$current_page"/>
<xsl:with-param name="count_items" select="$count_items"/>
<xsl:with-param name="visible_pages" select="$visible_pages"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
...
Работать будет с версии 5.5.4.
Спасибо, хороший способ.
Заметил небольшую ошибку: в хsl не хватает слэша перед {$prefix} (в представленной части кода он уже присуствует), кидал на несуществующую страницу http://адрессайта.ру/shop/НАЗВАНИЕТОВАРАcomment-page-8/
...
<!-- Выводим ссылку на последнюю страницу -->
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/{$prefix}-{round($n+1)}/" class="page_link" style="text-decoration: none;">→</a>
</xsl:when>
<xsl:otherwise>
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/{$prefix}-{round($n)}/" class="page_link" style="text-decoration: none;">→</a>
...
еще пришлось закомментировать(от коммента какая-то ошибка появлялась, убрал эту строку вообще)
т.к. выводил что-то подобное
Заметил небольшую ошибку: в хsl не хватает слэша перед {$prefix} (в представленной части кода он уже присуствует), кидал на несуществующую страницу http://адрессайта.ру/shop/НАЗВАНИЕТОВАРАcomment-page-8/
...
<!-- Выводим ссылку на последнюю страницу -->
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/{$prefix}-{round($n+1)}/" class="page_link" style="text-decoration: none;">→</a>
</xsl:when>
<xsl:otherwise>
<a href="{/shop/path}{/shop/item/fullpath}{/shop/item/path}/{$prefix}-{round($n)}/" class="page_link" style="text-decoration: none;">→</a>
...
еще пришлось закомментировать(от коммента какая-то ошибка появлялась, убрал эту строку вообще)
<!-- =<xsl:value-of select="$current_page"/>=-<xsl:value-of select="$pre_count_page"/>--<xsl:value-of select="$i"/>- -->
т.к. выводил что-то подобное
=7=-2--0-
перед ссылкой на 1 страницу
Авторизация