簡體   English   中英

如何正確地將 KNP Paginator Bundle 的分頁居中?

[英]How to properly center pagination of KNP Paginator Bundle?

我有一個 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 變量......

如果有人有一種破壞性較小的方式來將這個塊居中,我們將非常歡迎。

你是完全正確的。 這個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM