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

Need advice: is it possible to pass the filter value to a collection dynamically #296

Closed
andreapernici opened this issue Oct 31, 2018 · 4 comments
Labels
education: template language Learning about a template language. waiting-to-close Issue that is probably resolved, waiting on OP confirmation.

Comments

@andreapernici
Copy link

I'm trying to use the filter capabilities like the following

module.exports = function(eleventyConfig) {
  // Get only content that matches a tag
  eleventyConfig.addCollection("myPosts", function(collection) {
    return collection.getFilteredByTag("post");
  });
};

but I'd like to make it dynamic and allow the user to pass the tag value inside for example a shortcode.

Is there some documentation on how to do that?

@zachleat
Copy link
Member

zachleat commented Nov 21, 2018

Say you have a myTag variable in your template that has the value of the dynamic collection you’re trying to retrieve. You can just use that as the object key on your collections global.

https://www.11ty.io/docs/collections/

{% for post in collections[ myTag ] %}
<!-- do something -->
{% endfor %}

Does that make sense?

@zachleat zachleat added education: template language Learning about a template language. waiting-to-close Issue that is probably resolved, waiting on OP confirmation. labels Nov 21, 2018
@jevets
Copy link

jevets commented Nov 21, 2018

@andreapernici I have a suspicion you may be referring to more of a front-end issue.

Who's "the user" in your case? If you're building a reusable module that works with Eleventy, then let's explore this further. Otherwise, I have a feeling you're facing a front-end issue (having become a little familiar with some of your other issues).

If it is a front-end thing, then you could have Eleventy generate some .json files (maybe /api/some-static-stuff.json) that your front-end JS code can consume. That or you could spit out some JSON embedded directly in the HTML document in some <script /> tags.

@andreapernici
Copy link
Author

I ended up stop using Collections for my needs as @jevets suggested.

@koitaki
Copy link

koitaki commented Apr 27, 2020

Thanks for your comment Zach. It helped me create a collections page that had a dynamic tag input based on the Front Matter in a page in each collection folder. Then I was able to print out all the posts from the files in those different collection folders. Eg:

songs/songs.md

---
title: Songs
layout: 'layouts/collection.njk'
eleventyExcludeFromCollections: true
collection: songs
---

pictures/pictures.md

---
title: Pictures
layout: 'layouts/collection.njk'
eleventyExcludeFromCollections: true
collection: pictures
---

layouts/collections.njk

{{ title }}
{%- for post in collections[collection] -%}
          <img src="{{ site.images }}{{ post.data.imageMain }}">
          <span>{{ post.data.title }}</span>
          {{ post.data.excerpt | safe }}
          <p>Updated: {{ post.date }}</p> 
        <div><a href="{{ post.url }}">Read more</a></div>
{%- endfor -%}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
education: template language Learning about a template language. waiting-to-close Issue that is probably resolved, waiting on OP confirmation.
Projects
None yet
Development

No branches or pull requests

4 participants