In this video we will take a quick demo of the Pagination, Filtering, and Sorting using Twig.

This demo will cover the functionality that we will implement in the four different scenarios we have:

  • Symfony 3 with Twig
  • Symfony 3 with FOSRESTBundle, as an API
  • Angular 1.5 talking to our Symfony 3 API
  • React 15.2 talking to our Symfony 3 API

In the video we will see how the site operates using Twig.

By the end of this series we will have implemented the same functionality but without having to do full page reloads whenever we change the query.

At this point I should point out that if you haven't already done so, I would recommend completing this course - or having a similar level of knowledge - before starting this course. We will use the code from that course as the starting point for this course.

All the logic will be done on the server side. This means that we must modify the query string in order to paginate, filter, and sort our data.

Here are some examples:

This is the root URL for getting access to our Blog Post data.

Once we have added the paginator bundle, we could also hit the following URLs, which would be functionally identical:


This is to prove that the query string ordering doesn't matter, and that leaving any of the parameters as blank (e.g. filter=) would mean they don't have any impact on the result.

Given this, we could then assume:

Would show us page 1 with 20 records / blog posts on the page.

Would show us page 2 with 5 records / blog posts on the page.

And so on.

We will come back to this throughout the course.


For pagination in Twig we get this as part of the template included in the KnpPaginatorBundle.

This means with just a single line of config, we are going to get ourselves a nicely styled and formatted Bootstrap 3 compatible paginator. Flippin' awesome.

We'll see just how easy this is to add into our project in the very next video.

Of course in our API we won't need this at all. However, that doesn't mean we get away with doing nothing - there will be a little work required to make it behave.

And then in React and Angular we will make use of existing Bootstrap components for the respective library / framework. More on this in the specific videos. In both cases we will need to provide extra info to the paginators so they can correctly show the amount of pages, and so on.


We need to make sure that we can sort data.

We will achieve this through clickable table row headers. On first click, the row will be sorted 'ascending', on second click 'descending', third will flip back to 'asc', and so on.

We'll need a way to add this information to the query string, and then to send the amended query to the server, so as to get back the correct results.

A likely bug will be in that sorting works correctly on page 1, but on page 2 the order reverts. Best keep an eye out for that one.


JavaScript is actually pretty fantastic at filtering and sorting data. It's kind of a shame that we won't get to really see how powerful it can be at this task.

However, we will again need a way to provide an input for our users to filter the resulting data set, and ensure that as the filter input changes, so too does the API query / response.

We will do this by watching for input changes, and then re-running the updated query accordingly.

Server Side Vs Client Side

For all of these tasks we could have just allowed our users to do a big GET request and then let them worry about it. That becomes a little impractical as your database (almost) inevitably grows.

It's really entirely dependant on your circumstances. In general, it makes sense - in my opinion - to have the server worry about this stuff. It scales better, and in the case of Symfony, KNP Paginator Bundle does a remarkably good job of making this task a cake-walk.

As ever though, use your own personal judgement.

Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.

Episodes in this series

# Title Duration
1 Intro, and Project Demo 02:27
2 Pagination with Twig and KnpPaginatorBundle 07:49
3 Sorting with Twig and KnpPaginatorBundle 02:15
4 Simple Filtering in Twig 05:00
5 API - Pagination, (Basic) Filtering, and Sorting 10:25
6 Angular - Pagination 10:14
7 Angular - Sorting 06:11
8 Angular - Limiting 05:43
9 Angular - Filtering 03:47
10 React - Pagination (Part 1) 10:45
11 React - Pagination (Part 2) 04:14
12 React - Sorting 11:11
13 React - Limiting 03:43
14 React - Filtering 03:41