[英]How to properly center pagination of KNP Paginator Bundle?
I have a Symfony 5 project where I use the KNP Paginator Bundle to paginate some results.我有一个 Symfony 5 项目,我使用KNP Paginator Bundle对一些结果进行分页。 I use the
@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig
template to render the pagination block.我使用
@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig
模板来呈现分页块。 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 :所以我使用了一个全局 Twig 变量来将我的分页块添加到 Twig 配置中:
# 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...不过,我想可以通过其他方式设置 KNP Paginator 模板中的这个
align
Twig 变量......
If anyone has a less disruptive way to center this block, it would be gladly welcome.如果有人有一种破坏性较小的方式来将这个块居中,我们将非常欢迎。
You're completely right.你是完全正确的。 This
align
variable can be set via the setCustomParameters
method.这个
align
变量可以通过setCustomParameters
方法设置。 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您可以在此处找到更多详细信息: https : //github.com/KnpLabs/KnpPaginatorBundle/blob/master/docs/templates.md
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.