{"id":18103937,"url":"https://github.com/morozgrafix/twig-pagination","last_synced_at":"2025-09-11T07:37:18.316Z","repository":{"id":77887403,"uuid":"179418326","full_name":"morozgrafix/twig-pagination","owner":"morozgrafix","description":"Twig Pagination Extension","archived":false,"fork":false,"pushed_at":"2019-04-04T07:00:35.000Z","size":41,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-09T00:24:01.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/morozgrafix.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-04T03:57:43.000Z","updated_at":"2022-05-14T18:02:10.000Z","dependencies_parsed_at":"2023-03-12T02:24:22.198Z","dependency_job_id":null,"html_url":"https://github.com/morozgrafix/twig-pagination","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/morozgrafix/twig-pagination","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morozgrafix%2Ftwig-pagination","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morozgrafix%2Ftwig-pagination/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morozgrafix%2Ftwig-pagination/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morozgrafix%2Ftwig-pagination/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morozgrafix","download_url":"https://codeload.github.com/morozgrafix/twig-pagination/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morozgrafix%2Ftwig-pagination/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274595545,"owners_count":25314018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-31T22:13:34.168Z","updated_at":"2025-09-11T07:37:18.292Z","avatar_url":"https://github.com/morozgrafix.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/bertramakers/twig-pagination.svg?branch=master)](https://travis-ci.org/bertramakers/twig-pagination)\n[![Coverage Status](https://coveralls.io/repos/bertramakers/twig-pagination/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/bertramakers/twig-pagination?branch=master)\n\n# Introduction\n\nThis extension helps you determine what pagination links to display on a given \npage, depending on the total amount of pages and some configurable logic.\n\nIt will not generate any HTML for you, but simply provide you with an array of\npage numbers to display as links and if necessary where any chunks of omitted\npages are.\n\nFor example:\n\n* **Total pages**: 50\n* **Current page**: 30\n* **Omitted pages indicator**: -1\n* **Behaviour**: `FixedLength`, 11\n* **Result**: `[1, 2, 3, -1, 29, 30, 31, -1, 48, 49, 50];`\n\nIt is then up to you to display this data using a Twig template. \n(See [Usage in Twig](#usage-in-twig))\n\nIt's important to note that this extension **will not help you help you load or\nfilter the data you want to paginate**. It's simply meant to help you decide \nwhat links to other pages you should display on a given page.\n\n# Installation\n\nUsing [Composer](http://getcomposer.org):\n\n```bash\ncomposer require bertramakers/twig-pagination\n```\n\n# Setup\n\nWhen setting up the PaginationExtension, you can configure as many pagination\nfunctions as you like. \n\nEach function can have a custom name, and the behaviour that you choose. The \nonly provided behaviour at this point is `FixedLength`, but you can easily add \nyour own by implementing `PaginationBehaviourInterface`.\n\n## Plain PHP\n\nDocumentation: [http://twig.sensiolabs.org/doc/api.html](http://twig.sensiolabs.org/doc/api.html)\n\n```php\nuse DevotedCode\\Twig\\Pagination\\PaginationExtension;\nuse DevotedCode\\Twig\\Pagination\\Behaviour\\FixedLength;\nuse \\Twig_Environment;\nuse \\Twig_Loader_Filesystem;\n\n$loader = new Twig_Loader_Filesystem('/path/to/templates');\n$twig = new Twig_Environment($loader);\n\n// Configure two fixed length pagination functions, a small one and a wide one.\n// The names are completely up to you to choose.\n$paginationExtension = (new PaginationExtension())\n    -\u003ewithFunction('small', new FixedLength(7))\n    -\u003ewithFunction('wide', new FixedLength(21));\n    \n$twig-\u003eaddExtension($paginationExtension);\n```\n\n## Silex\n\nDocumentation: [http://silex.sensiolabs.org/doc/providers/twig.html](http://silex.sensiolabs.org/doc/providers/twig.html)\n\n```php\nuse DevotedCode\\Twig\\Pagination\\PaginationExtension;\nuse DevotedCode\\Twig\\Pagination\\Behaviour\\FixedLength;\nuse Silex\\Provider\\TwigServiceProvider;\n\n$app-\u003eregister(new TwigServiceProvider(), array(\n    'twig.path' =\u003e '/path/to/templates',\n));\n\n$app['twig'] = $app-\u003eshare(\n    $app-\u003eextend(\n        'twig', \n        function($twig, $app) {\n            // Configure two fixed length pagination functions, a small one and\n            // a wide one. The names are completely up to you to choose.\n            $paginationExtension = (new PaginationExtension())\n                -\u003ewithFunction('small', new FixedLength(7))\n                -\u003ewithFunction('wide', new FixedLength(21));\n        \n            $twig-\u003eaddExtension($paginationExtension)\n        \n            return $twig;\n        }\n    )\n);\n```\n\n# Usage in Twig\n\nIn the setup section above we set up two pagination functions: `small` and `wide`.\nThese functions are now available in Twig as `small_pagination` and `wide_pagination`.\n\n**Every pagination function takes the following arguments**:\n* Total pages\n* Current page\n* Omitted pages indicator (defaults to `-1`)\n\n**Important!** The current page value uses regular numbering starting from 1,\nso if you use 0-based numbering make sure to add 1 to the current page value.\n\n## Examples:\n\n**Standard usage**:\n\n```twig\n{% set totalPages = 20 %}\n{% set currentPage = 8 %}\n```\n\n```twig\n{% set paginationData = wide_pagination(totalPages, currentPage) %}\n```\n\n**Custom indicator for omitted pages**:\n\n```twig\n{% set paginationData = small_pagination(totalPages, currentPage, '...') %}\n```\n\n**Looping over the data**:\n\nThis template is just a simple example you could start from, or you could write\none from scratch depending on your requirements.\n\n```twig\n\u003cul class=\"pagination\"\u003e\n\n{% if currentPage \u003e 1 %}\n    \u003cli\u003e\u003ca href=\"...\"\u003ePrevious\u003c/a\u003e\u003c/li\u003e\n{% else %}\n    \u003cli class=\"disabled\"\u003ePrevious\u003c/li\u003e\n{% endif %}\n\n{% for paginationItem in paginationData %}\n    {% if paginationItem == -1 %} {# The value you chose for omitted chunks #}\n        \u003cli class=\"disabled\"\u003e...\u003c/li\u003e\n    {% elseif paginationItem == currentPage %}\n        \u003cli class=\"active\"\u003e\u003ca href=\"...\"\u003e{{ paginationItem }}\u003c/a\u003e\u003c/li\u003e\n    {% else %}\n        \u003cli\u003e\u003ca href=\"...\"\u003e{{ paginationItem }}\u003c/a\u003e\u003c/li\u003e\n    {% endif %}\n{% endfor %}\n\n{% if currentPage \u003c totalPages %}\n    \u003cli\u003e\u003ca href=\"...\"\u003eNext\u003c/a\u003e\u003c/li\u003e\n{% else %}\n    \u003cli class=\"disabled\"\u003eNext\u003c/li\u003e\n{% endif %}\n\n\u003c/ul\u003e\n```\n\n# Usage outside of Twig\n\nYou can easily use the display logic outside of Twig:\n\n```php\nuse DevotedCode\\Twig\\Pagination\\Behaviour\\FixedLength;\n\n$behaviour = new FixedLength(21);\n\n$totalPages = 50;\n$currentPage = 8;\n$omittedPagesIndicator = -1; // Optional, defaults to -1.\n\n$paginationData = $behaviour-\u003egetPaginationData(\n    $totalPages, \n    $currentPage, \n    $omittedPagesIndicator\n);\n```\n\nAfter this it's a matter of rendering the pagination links using a template\nlanguage of your choice.\n\n# FAQ\n\n### Why not just put everything inside a Twig template?\n\nUsing this extension, you can keep the logic for determining which page links\nto display and the actual markup for the list of links separated. This makes it\npossible to re-use the template across your application, even in places where\nthe logic for which links to display is different. It also makes it a lot \neasier to test the actual display logic.\n\nYou could even use the behaviour classes outside of Twig, and use them to\nrender pagination links in something else than HTML.\n\n### Can you add a zero-based numbering option?\n\nThis extension's purpose is to decide what page numbers should be shown to your\nend users, so it always starts counting from 1.\n\nHowever if you want to use zero-based numbering for the page parameter in your \nURLs, you can easily do so by subtracting 1 from each page number you get back.\n\nSo you would still print \"1\" for the first page, but subtract 1 and use \"0\" as \nthe page parameter in the URL to the first page.\n\n### Where are the previous / next links?\n\nYou should include those in your template, as they have no effect on what page\nnumbers to display and which to exclude. See the template example above for an\nexample on how to add previous and next links.\n\n### How do I know which page link should be disabled for the current page?\n\nYou should simply loop over the page numbers you get back, and if a given page\nnumber equals the current page value you used when generating the pagination\ndata, that's the current page that you could disable if you wanted to.\n\n# Contributing\n\nRun a full test (lint check, coding standards check and unit tests) with \n`./vendor/bin/phing test`.\n\nCheck code coverage with `./vendor/bin/phpunit --coverage-html build` and open `./build/index.html`.\n\nIn order to automatically run a full test when committing to git, install the\nincluded git hooks hook with `./vendor/bin/phing githooks`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorozgrafix%2Ftwig-pagination","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorozgrafix%2Ftwig-pagination","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorozgrafix%2Ftwig-pagination/lists"}