2014-02-14 48 views
0

我正在使用Symfony2.3作爲我的項目和分頁使用KNP paginator Bundle。 我想知道如何在頁面中實現rel = prev和rel = next?如何在Symfony2.3分頁之後實現rel = prev和rel =?

我的控制器:

<?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 ([email protected]) 
*/ 
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 %} 

謝謝!

回答

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

+0

感謝的答案,但我有點迷惑你能給我一些些例子嗎? – Sid

+0

當然,我更新了我的答案。 –

+0

很多非常感謝,但我希望rel = prev和rel = next示例?你能幫我解決嗎? – Sid

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