【问题标题】:How to properly center pagination of KNP Paginator Bundle?如何正确地将 KNP Paginator Bundle 的分页居中?
【发布时间】:2020-01-23 10:46:37
【问题描述】:

我有一个 Symfony 5 项目,我使用 KNP Paginator Bundle 对一些结果进行分页。我使用@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig 模板来呈现分页块。但是,我希望它在我的页面中居中。

我看过模板,也看过这段代码:

{% if pageCount > 1 %}
    <nav>
        {% set classAlign = (align is not defined) ? '' : align=='center' ? ' justify-content-center' : (align=='right' ? ' justify-content-end' : '') %}
        {% set classSize = (size is not defined) ? '' : size=='large' ? ' pagination-lg' : (size=='small' ? ' pagination-sm' : '') %}
        <ul class="pagination{{ classAlign }}{{ classSize }}">
        {# ... #}

所以我使用了一个全局 Twig 变量,通过将它添加到 Twig 配置中来使我的分页块居中:

# config/packages/twig.yaml
twig:
    # ...
    globals:
        align: 'center'

不过,我认为这不是一种正确的做法,因为它可能会导致与其他一些捆绑包甚至我自己的代码发生冲突。我还认为覆盖模板以手动使块居中并不是一种真正正确的方法,而且我在文档中没有找到任何可以简单而优雅地做我想做的事情。不过,我想 KNP Paginator 模板中的这个 align Twig 变量可以通过其他方式设置...

如果有人有一种不那么破坏性的方式来使这个街区居中,那将很受欢迎。

【问题讨论】:

    标签: php symfony pagination


    【解决方案1】:

    你完全正确。这个align 变量可以通过setCustomParameters 方法设置。这里的例子:

    $pagination->setCustomParameters([
        'align' => 'center', # center|right (for template: twitter_bootstrap_v4_pagination)
        'size' => 'large', # small|large (for template: twitter_bootstrap_v4_pagination)
        'style' => 'bottom',
        'span_class' => 'whatever',
    ]);
    

    您可以在此处找到更多详细信息:https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/docs/templates.md

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2012-12-08
      • 2014-12-19
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      相关资源
      最近更新 更多