如何在Symfony2.3分页旁边实现rel = prev和rel =?

时间:2014-02-14 11:11:48

标签: php symfony twig knppaginator

我正在使用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);
}

}

Twig文件: -

 {% 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 %}

谢谢!

2 个答案:

答案 0 :(得分:2)

接受的答案不起作用,因为rel=prevrel=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 %}

答案 1 :(得分:1)

您可以通过覆盖默认分页模板来完成此操作。查看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