0

我在我的项目中使用 Symfony2.3,而对于分页,我正在使用 KNP paginator Bundle。我想知道如何在我们的页面中实现 rel=prev 和 rel=next ?

我的控制器:

<?php

namespace XXX\ABCBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityManager;

/**
 * author Siddharth Singh (siddharth.find@gmail.com)
 */
class ArcController extends Controller {

/**
 * @Route("/arch", name="_arch")
 * @Template()
 */
public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $AArtEntity = $em->getRepository('XXXABCBundle:Arc')->findAll(array('updated'=>'DESC'));
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
                $AArtEntity, $this->get('request')->query->get('page', 1)/* page number */, 10/* limit per page */

        );

    return array('article' => $pagination);
}

}

树枝文件:-

 {% extends 'XXXABCBundle::layout.html.twig' %}
 {% block body %}
            {% for artic in article %}
                    <div class="txt">
                        <p>{{artic.description|striptags|titletruncate(250)}}</p>
                    </div>
            {% endfor %}
            <div class="arcive_Paging">
                <ul class="ul_paging">
                    <span class="acitve">{{ knp_pagination_render(article) }}</span>
                </ul>
            </div>
{% endblock %}

谢谢 !

4

2 回答 2

3

接受的答案不起作用,因为rel=prev并且rel=next必须在head标签内。

我使用这个宏:

 {% macro pagination_meta(pagination) %}
    {% set total = pagination.totalItemCount %}
    {% set items_per_age = pagination.itemNumberPerPage %}
    {% set current = pagination.currentPageNumber %}
    {% set last = (total / items_per_age) | round(0, 'ceil') %}
    {% set page_parameter = pagination.paginatorOption('pageParameterName') %}

    {% if current != 1 %}
        <link rel="prev" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params') | merge({ (page_parameter): (current - 1) })) }}" />
    {% endif %}
    {% if current != last %}
        <link rel="next" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params') | merge({ (page_parameter): (current + 1) })) }}" />
    {% endif %}
{% endmacro %}

内部模板用法如下所示:

{% import 'AppBundle::macros.html.twig' as macros %}

{% block head %}
    {{ parent() }}
    {{ macros.pagination_meta(entities) }}
{% endblock %}
于 2016-07-18T10:06:23.327 回答
2

您可以通过覆盖默认分页模板来做到这一点。查看Templates部分官方文档:

https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/templates.md

例如:

告诉 KnpPaginatorBundle 加载您的模板以渲染分页,将这些代码行添加到 config.yml:

knp_paginator:
    template:
        pagination: YourBundle::pagination.html.twig

并将默认分页模板代码复制到src/YourBundle/Resources/views/pagination.html.twig. 您可以在此处获取代码:

https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/views/Pagination/sliding.html.twig

于 2014-02-14T11:15:58.493 回答