【问题标题】:How to implement rel=prev and rel=next to Symfony2.3 pagination?Symfony2.3分页如何实现rel=prev和rel=next?
【发布时间】:2014-02-14 11:11:48
【问题描述】:

我在我的项目中使用 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 %}

谢谢!

【问题讨论】:

    标签: php symfony twig knppaginator


    【解决方案1】:

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

    【讨论】:

      【解决方案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

      【讨论】:

      • 谢谢你的回答,但我有点困惑你能给我一些例子吗?
      • 非常感谢,但我想要 rel=prev 和 rel=next 示例?你能帮我吗?
      • 您想将rel 属性添加到分页的上一个\下一个链接,对吗?只需更改模板中的这些链接即可。
      • 这不起作用,因为 rel=prev 和 rel=next 是元标记。分页模板使用 twig 包含,您不能从包含覆盖扩展模板。
      • rel=prev 和 rel=next 是 LINK 标签,需要放在 HEAD 中。 @BoShurik 提供了更好的解决方案(无论您是否决定使用宏)
      猜你喜欢
      • 2012-01-29
      • 2012-05-10
      • 1970-01-01
      • 2023-01-05
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多