morozgrafix/paginator-twig-extension
Composer 安装命令:
composer require morozgrafix/paginator-twig-extension
包简介
Twig extension to help generate simple paginations in Twig templates.
关键字:
README 文档
README
Twig extension to help generate simple paginations in Twig templates.
Introduction
This extension helps building pagination in Twig templates. It will return an array with all necessary information to quickly display pagination links. It doesn't generate any HTML and it's up to you how you'd like to display it. Example below shows one of the ways of generating pagination compatible with Bootstrap CSS framework.
Extension is called with following function and accepts following parameters:
paginator(int current_page, int last_page, int num_items, string separator)
Parameters in detail:
current_page- current page visitor is onlast_page- last page in data set (total number of pages)num_items- number of items that will be displayed in a pagination strip. Note: minimum allowed value is 7 and if even number is passed it will be bumped to next odd number for symmetry.separator(optional) - custom separator for numbers not visible in pagination strip. Default is set to...
Basic example:
Calling
{% set paginator = paginator(1, 10 , 7) %}
will set paginator twig variable to:
array ( 'curr_page' => 1, 'last_page' => 10, 'num_items' => 7, 'separator' => '...', 'pagination' => array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => '...', 6 => 10, ), )
Installation
Using Composer:
composer require morozgrafix/paginator-twig-extension
Logic in Twig template
Following example is compatible with Bootstrap 4 CSS framework.
<nav aria-label="pagination"> <ul class="pagination justify-content-center"> {# Optional Prev #} <li class="page-item {% if paginator.curr_page == 1 %} disabled{% endif %}"> <a class="page-link" href="?page={{ paginator.curr_page-1 }}">Prev</a> </li> {# Main Pagination Loop #} {% for i in paginator.pagination %} <li class="page-item{% if i == paginator.curr_page %} active{% endif %}{% if i == paginator.separator %} disabled{% endif %}"> <a class="page-link" href="?page={{ i }}">{{ i }}</a> </li> {% endfor %} {# Optional Next #} <li class="page-item {% if paginator.curr_page == paginator.last_page %} disabled{% endif %}"> <a class="page-link" href="?page={{ paginator.curr_page+1 }}">Next</a> </li> </ul> </nav>
Result with optional Prev/Next:
Result without Prev/Next:
Additional Examples
Custom separator:
{% set paginator = paginator(10, 25 , 11, '¯\_(ツ)_/¯') %}
Result:
array ( 'curr_page' => 10, 'last_page' => 25, 'num_items' => 11, 'separator' => '¯\\_(ツ)_/¯', 'pagination' => array ( 0 => 1, 1 => '¯\\_(ツ)_/¯', 2 => 7, 3 => 8, 4 => 9, 5 => 10, 6 => 11, 7 => 12, 8 => 13, 9 => '¯\\_(ツ)_/¯', 10 => 25, ), )
Even number of items was specified, it gets bumped to next odd number for display symmetry (10 -> 11):
{% set paginator = paginator(420, 999 , 10) %}
Result:
array ( 'curr_page' => 420, 'last_page' => 999, 'num_items' => 11, 'separator' => '...', 'pagination' => array ( 0 => 1, 1 => '...', 2 => 417, 3 => 418, 4 => 419, 5 => 420, 6 => 421, 7 => 422, 8 => 423, 9 => '...', 10 => 999, ), )
P.S.
This is my first Twig extension and it was quickly developed while I was working on a bigger project that needed pagination. I'm sure there are some pitfalls and additional features that can be added. Feel free to open an issue or submit a PR with improvements. Thanks. 🙇
morozgrafix/paginator-twig-extension 适用场景与选型建议
morozgrafix/paginator-twig-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「page」 「twig」 「pagination」 「pages」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 morozgrafix/paginator-twig-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 morozgrafix/paginator-twig-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 morozgrafix/paginator-twig-extension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table.
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Plug-ins for DataTables
Caching and compression for Twig assets (JavaScript and CSS).
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-08




