Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller logic and template logic, where do you draw the line with pagination?

The whole point of an MVC framework is to separate design (templates) from logic (controllers). However, template languages often afford a limited degree of "design logic" to occur. This includes basic if statements, loops, filtering, etc.

I've created a Django template tag that can take any list or QuerySet and "pagify" it. It splits the list up into pages based on a specified page size then adds the pages into the Context. The usage is as follows:

{% pagify articles by 20 as pages %}

I can then call a separate include to iterate over the pages and produce a nice list of pages wherever I needed it.

This seemed like an optimal way to do it because it allowed me to page any list in the context; I didn't have to rely on the controller to return paged results. But a colleague argued that this seemed like too much logic for the template. I thought this still fell within the realm of design-based logic since the page would still function even without paging, and determining page size feels like a template responsibility.

My question, is this too much logic for the template? or is this a clean way to be handling this?

like image 813
Soviut Avatar asked Dec 10 '22 21:12

Soviut


2 Answers

It's always been my understanding that the view isn't supposed to be devoid of logic. It's just supposed to be devoid of any controller logic. Paging just has to do with how the data is displayed which is exactly what the view logic is supposed to contain.

like image 59
Spencer Ruport Avatar answered Jan 16 '23 21:01

Spencer Ruport


Put it this way; what if you were using your data model in another medium, say, not on the web but via some kind of console-based application or background task? Wouldn't it be nice to be able to get "pages" of data through a controller (or manager) rather than having to somehow rely on a template to do this work for you?

While I'd certainly agree that the "look" of the paged data should be handled by your template, the "act" of paging should be left up to a controller (Django view) or even through some kind of custom manager (models.Manager) method.

like image 26
Ryan Duffield Avatar answered Jan 16 '23 21:01

Ryan Duffield