0
votes

I have a Symfony 5 project where I use the KNP Paginator Bundle to paginate some results. I use the @KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig template to render the pagination block. However, I would like it centered in my page.

I have looked at the template and I have seen this piece of code :

{% 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 }}">
        {# ... #}

So I used a global Twig variable to center my pagination block by adding it to Twig configuration :

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

Still, I think this is not a proper way to do as it could cause conflicts with some other bundles or even my own code. I also think overriding the template to manually center the block is not a really proper way to do, and I have not found anything in the documentation to simply and elegantly do what I want to do. Still, I guess this align Twig variable in the KNP Paginator template can be set some other way...

If anyone has a less disruptive way to center this block, it would be gladly welcome.

1

1 Answers

3
votes

You're completely right. This align variable can be set via the setCustomParameters method. Here example:

$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',
]);

More details you can find here: https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/docs/templates.md