Monday, May 23, 2011

Symfony2 + Twig pagination class

I was looking for some pagination class to use with Symfony2, but most of the scripts that I found included html in the class, after some googling I found this post, and it was very helpful to get what I was looking for. I wanted something like the old digg pagination so I modified this class a little bit to get something like this:

The modifications I made:

- Show a range of pages around the current page as in the old digg.- Parameters can be passed in a route, so you get a nice URL.

//current displayed pageprotected $currentpage;//limit items on one pageprotected $limit;//total number of pages that will be generatedprotected $numpages;//total items loaded from databaseprotected $itemscount;//starting item number to be shown on pageprotected $offset;//pages to show at left and right of current pageprotected $mid_range;//range initial pageprotected $start_range;//range end pageprotected $end_range;

private function setDefaults() { //If currentpage is set to null or is set to 0 or less //set it to default (1) if (($this->currentpage == null) || ($this->currentpage < 1)) { $this->currentpage = 1; } //if limit is set to null set it to default (20) if (($this->limit == null)) { $this->limit = 20; //if limit is any number less than 1 then set it to 0 for displaying //items without limit } else if ($this->limit < 1) { $this->limit = 0; }}