Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty pagination data to still generate an empty results page #731

Closed
saenglert opened this issue Oct 15, 2019 · 6 comments · Fixed by #2208
Closed

Allow empty pagination data to still generate an empty results page #731

saenglert opened this issue Oct 15, 2019 · 6 comments · Fixed by #2208

Comments

@saenglert
Copy link
Contributor

saenglert commented Oct 15, 2019

Describe the bug
If pagination is handed an empty collection, the template is ignored.

To Reproduce
Steps to reproduce the behaviour:

  1. Create empty collection
  2. Setup template with pagination and above collection
  3. Run build
  4. Missing template in output

Expected behaviour
If the collection is empty, a site with one page and no entries should be generated (being able to add a custom "no items found" message would be the icing on the cake).

Environment:

  • Win 10
  • Eleventy Version 6.4.1
@Ryuno-Ki
Copy link
Contributor

Which template language?

(Nunjucks supports an else clause for {% for %} which is executed on empty collections)

@saenglert
Copy link
Contributor Author

Which template language?

(Nunjucks supports an else clause for {% for %} which is executed on empty collections)

Nunjucks indeed. Guess I ll take a look at that.

@Ryuno-Ki
Copy link
Contributor

Docs

@saenglert
Copy link
Contributor Author

Did some testing and the nunjuck else clause works as advertised however the underlying problem still continues.

If pagination receives an empty array, the template will not be parsed at all.

Looking at the Debug output the template is found:
Eleventy:TemplateWriter Found: [manyTemplatePaths]
Eleventy:TemplateWriter ./src/content/news/zweite/recap.njk added to map. +3ms

Eleventy then (correctly) recognizes that my collection is empty:
Eleventy:TemplateMap Collection: collections.newsSecondRecap size: 0 +270ms

And subsequently the specific template doesn't appear in the list of templates being written into html files (example):
Eleventy:Template Writing 'out/someTemplate.html' from 'src/content/someTemplate.njk'

@cmcknight
Copy link

cmcknight commented Nov 8, 2021

@zachleat

Would be nice to have an additional YAML parm for a default action to take on empty collections if you want the page rendered anyway.

pagination:
data: myCoolStuff
size: 25
alias: products
renderEmptyCollection: true | false

true - Render template regardless
false - Default Eleventy behavior (do not render template)(default setting)

What gets rendered in the content portion of the template would be controlled by the template if the render flag above is true.

In a template, Nunjucks in this case, one could simply test for an empty collection and insert a custom message, etc.

{% if collections.myCoolStuff | length == 0 %}
      [insert something here]
{% endif %}

@zachleat
Copy link
Member

Shipped #1698 and #2208 for this. Will be included with 2.0.0-canary.10

Docs will show up on https://www.11ty.dev/docs/pagination/ shortly

@zachleat zachleat added this to the Eleventy 2.0.0 milestone May 11, 2022
@zachleat zachleat changed the title Eleventy ignores template with empty pagination collection Allow empty pagination data to still generate an empty results page May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants