我正在使用Symfony2.3进行项目,对于分页,我正在使用KNP分页器捆绑包。我想知道我如何在我们的页面中实现 rel=prev 和 rel=next?
我的控制器 :
<?php
namespace XXXABCBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationTemplate;
use SymfonyComponentHttpFoundationSessionSession;
use DoctrineORMEntityManager;
/**
* 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 %}
谢谢!
接受的答案不起作用,因为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 %}
您可以通过覆盖默认分页模板来做到这一点。检查官方文档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