Display a pager on the top and bottom of view pages but only on the bottom of view blocks

pe flag

For a project running on Drupal 9.2.9, I want pagers on the top and bottom of view pages. I have it working on pages by an edit to a custom views-view.html.twig in the theme (using the full pager option in the Views display). Unfortunately, this resulted in double pagers on blocks using minipager also.

I think I need a little conditional logic in the views-view.html.twig template to check for if … is full pager versus if … is mini pager, or if … is page vs. if … is block. How can I achieve that?

Maybe, a better approach is a template for full pager and a different one for mini pager, but I don't understand how all these templates work together to use that approach.

in flag

Views provides theme suggestions, one of them keys off of the display type (e.g. page, block, etc.). Override the suggestion for pages (views-view--page.html.twig) and copy {% if pager %}{{ pager }}{% endif %} to wherever you want on top of the view. This will only apply to Page displays of a view, and nowhere else.

Refer to views-view.html.twig for the default markup of this template. The default placement of {{ pager }} is after the rows.

bd flag

See Views template files for details on how views templates can be overridden. To quote from that page:

All views templates can be overridden with a variety of names, using the view, the display ID of the view, the display type of the view, or some combination thereof.

So in your case, you probably want to have different templates for your views block and your views page.

These links could also help you better understand how template suggestions work, especially in the context of views:

Also Locating Template Files with Debugging can help you to enable twig debug and get information on the available template suggestions in your specific case.

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.