Question

I am using Symfony2.3 for my project and for pagination I am using KNP paginator Bundle. I want to know that How I can implement rel=prev and rel=next in our page ?

My Controller :

<?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 File :-

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

Thanks !

Was it helpful?

Solution 2

You can do it by overriding default pagination template. Check Templates part of the official documentation:

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

for example:

Tell KnpPaginatorBundle to load your template for rendering pagination, add these lines of code to config.yml:

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

And copy default pagination template code to your template in src/YourBundle/Resources/views/pagination.html.twig. You can get the code here:

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

OTHER TIPS

Accepted answer does not work because rel=prev and rel=next must be inside head tag.

I use this macros:

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

Inside a template usage looks like this:

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

{% block head %}
    {{ parent() }}
    {{ macros.pagination_meta(entities) }}
{% endblock %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top