Eleventy: When setting permalink to false using Directory Data Files, exclude such items from tag collection

Created on 15 Jun 2020  路  6Comments  路  Source: 11ty/eleventy

Describe the bug
I use a directory data file to set blog posts or pages to permalink: false to exclude them from build on production, if the file is marked as a draft in my Git-based CMS.

The files marked as draft: true correctly get assigned the computed property of permalink: false. However, if accessing the collection using the following syntax, they are still included in this collection:

{% for article in collections[tag_name] reversed %}
  // do something
{% endfor %}

As a temporary workaround, I also check the permalink value manually:

{% for article in collections[tag_name] reversed %}
  {% if article.url != false %}
    // do something
  {% endif %}
{% endfor %}

To Reproduce
Steps to reproduce the behavior:

  1. Have a directory called posts with blog posts written in Markdown inside it
  2. Assign tags to this post so that Eleventy makes a tag collection
  3. Create a directory data file called posts.11tydata.js
  4. Set permalink to false on some blog posts based on your own criteria
  5. Access the collection elsewhere using the collections[tag_name] syntax.
  6. Collection will include posts which have permalink set to false

Expected behavior
collections[tag_name] should exclude files that have permalink set to false.

Environment:

  • OS and Version: Pop!_OS 20.04 LTS
  • Eleventy Version: 0.11.0
needs-triage

Most helpful comment

@edwardhorsford Thanks! In the end all the discussed solutions are workarounds for a very basic feature, and I hope that 11ty will soon have a built-in (and optimized) way to do this.

All 6 comments

@makkabi raised this issue on DEV.to as well.

I think there are usecases where you want to set permalink to false, but still NOT exclude the content from collections. For example, you could have a folder with "testimonials", and while you don't want to generate single pages for every testimonial (which you can do by setting permalink to false), you still want to have a collection with all testimonials, so you can display them on e.g. your frontpage.
So not automatically excluding content without permalink is not a bug, but a useful feature. I think a draft status, which completely excludes content in the production build, should be separate from the permalink setting.
It would be great if the docs would include a description how to implement "drafts" in an easy way. (By easy I mean without e.g. GIT features like draft branches, which may be too complicated for many users.)
Even better would be a native draft feature in eleventy, e.g. just setting "draft" to true in frontmatter.

Drafts are discussed in this feature request / issue.

@hirusi for your case, you can additionally set eleventyExcludeFromCollections wherever you set permalink: false

@edwardhorsford Thanks! If you would not set eleventyExcludeFromCollections directly to false, but use eleventyComputed to either set it to true or false, based on whether there is a frontmatter of draft: true AND you are in production mode. If you then also use eleventyComputed to set permalink to false, as described by @hirusi in her dev.to post, you pretty much have a draft status feature.

@makkabi

Here's how I use them - I reference a env var that determines whether they're true or not - no need for eleventyComputed (which didn't exist when I was working on these pages).

Screenshot 2020-08-03 at 15 20 49

I would say they're not quite a substitute for draft. Even with eleventyExcludeFromCollections, eleventy _still_ processes the file. In my case, some of my templates are very costly to process. So even though they have permalink: false and eleventyExcludeFromCollections: true, they still take ages to be processed. In the end I've had to manually add them to eleventyIgnore to work around this.

@edwardhorsford Thanks! In the end all the discussed solutions are workarounds for a very basic feature, and I hope that 11ty will soon have a built-in (and optimized) way to do this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

proog picture proog  路  3Comments

zellwk picture zellwk  路  3Comments

veleek picture veleek  路  3Comments

ndaidong picture ndaidong  路  4Comments

smaimon picture smaimon  路  3Comments