├── .gitignore ├── www ├── posts │ ├── one │ │ └── index.html │ ├── two │ │ └── index.html │ ├── three │ │ └── index.html │ └── seven │ │ └── index.html └── index.html ├── src ├── posts │ ├── one.njk │ ├── two.njk │ ├── three.njk │ └── seven.njk └── index.njk ├── .eleventy.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /www/posts/one/index.html: -------------------------------------------------------------------------------- 1 |

oNe

2 | -------------------------------------------------------------------------------- /www/posts/two/index.html: -------------------------------------------------------------------------------- 1 |

tWo

2 | -------------------------------------------------------------------------------- /www/posts/three/index.html: -------------------------------------------------------------------------------- 1 |

ThReE

2 | -------------------------------------------------------------------------------- /www/posts/seven/index.html: -------------------------------------------------------------------------------- 1 |

Sven

2 | -------------------------------------------------------------------------------- /src/posts/one.njk: -------------------------------------------------------------------------------- 1 | --- 2 | title: oNe 3 | tags: 4 | - animal 5 | author: Dan 6 | --- 7 | 8 |

{{ title }}

9 | -------------------------------------------------------------------------------- /src/posts/two.njk: -------------------------------------------------------------------------------- 1 | --- 2 | title: tWo 3 | tags: 4 | - vegetable 5 | author: Jane 6 | --- 7 | 8 |

{{ title }}

9 | -------------------------------------------------------------------------------- /src/posts/three.njk: -------------------------------------------------------------------------------- 1 | --- 2 | title: ThReE 3 | tags: 4 | - animal 5 | - mineral 6 | author: Peter 7 | --- 8 | 9 |

{{ title }}

10 | -------------------------------------------------------------------------------- /src/posts/seven.njk: -------------------------------------------------------------------------------- 1 | --- 2 | title: Sven 3 | eleventyComputed: 4 | tags: 5 | - dynamic 6 | - whimsy 7 | --- 8 | 9 |

{{ title }}

10 | -------------------------------------------------------------------------------- /.eleventy.js: -------------------------------------------------------------------------------- 1 | const _get = require("lodash.get"); 2 | 3 | /** 4 | * @typedef {import('@11ty/eleventy/src/UserConfig')} EleventyConfig 5 | * @typedef {ReturnType} EleventyReturnValue 6 | * @type {(eleventyConfig: EleventyConfig) => EleventyReturnValue} 7 | */ 8 | module.exports = function (eleventyConfig) { 9 | eleventyConfig.addFilter("where", function (coll, key, value) { 10 | return coll.filter(item => _get(item, key) === value); 11 | }); 12 | 13 | eleventyConfig.addCollection("liveColl", (collectionApi) => collectionApi); 14 | 15 | return { 16 | dir: { 17 | input: "src", 18 | output: "www", 19 | } 20 | }; 21 | }; 22 | -------------------------------------------------------------------------------- /www/index.html: -------------------------------------------------------------------------------- 1 |
    2 |
  1. oNe -- tags=animal
  2. 3 | 4 |
  3. ThReE -- tags=animal,mineral
  4. 5 |
6 | 7 |
    8 |
  1. tWo -- tags=["vegetable"]
  2. 9 |
10 | 11 |
    12 |
  1. ThReE -- tags=["animal","mineral"]
  2. 13 |
14 | 15 |
    16 |
  1. oNe -- tags=["animal"]
  2. 17 | 18 |
  3. tWo -- tags=["vegetable"]
  4. 19 | 20 |
  5. ThReE -- tags=["animal","mineral"]
  6. 21 | 22 |
  7. Sven -- tags=["dynamic","whimsy"]
  8. 23 |
24 | 25 |
    26 |
  1. tWo -- tags=["vegetable"] -- author=Jane
  2. 27 |
28 | 29 |
    30 |
  1. Sven -- tags=["dynamic","whimsy"]
  2. 31 |
32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "11ty-collectionapi-test", 3 | "description": "This is ~~probably~~ a very bad idea. If you're using Nunjucks templating, you can do some questionable life choices in your .eleventy.js config file and create dynamic collections using this nonsense:", 4 | "version": "1.0.0", 5 | "author": "Peter deHaan ", 6 | "bugs": { 7 | "url": "https://github.com/pdehaan/11ty-collectionApi-test/issues" 8 | }, 9 | "devDependencies": { 10 | "@11ty/eleventy": "^1.0.2", 11 | "lodash.get": "^4.4.2" 12 | }, 13 | "homepage": "https://github.com/pdehaan/11ty-collectionApi-test#readme", 14 | "keywords": [], 15 | "license": "MPL-2.0", 16 | "main": "index.js", 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/pdehaan/11ty-collectionApi-test.git" 20 | }, 21 | "scripts": { 22 | "build": "eleventy", 23 | "test": "echo \"Error: no test specified\" && exit 1" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/index.njk: -------------------------------------------------------------------------------- 1 | --- 2 | title: Index 3 | --- 4 | 5 |
    6 | {% for post in collections.liveColl.getFilteredByTag("animal") %} 7 |
  1. {{ post.data.title }} -- tags={{ post.data.tags }}
  2. 8 | {% endfor %} 9 |
10 | 11 |
    12 | {% for post in collections.liveColl.getFilteredByTag("vegetable") %} 13 |
  1. {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
  2. 14 | {% endfor %} 15 |
16 | 17 |
    18 | {% for post in collections.liveColl.getFilteredByTags("animal", "mineral") %} 19 |
  1. {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
  2. 20 | {% endfor %} 21 |
22 | 23 |
    24 | {% for post in collections.liveColl.getFilteredByGlob("src/posts/*.njk") %} 25 |
  1. {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
  2. 26 | {% endfor %} 27 |
28 | 29 |
    30 | {% for post in collections.liveColl.getFilteredByGlob("src/posts/*.njk") | where("data.author", "Jane") %} 31 |
  1. {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }} -- author={{ post.data.author }}
  2. 32 | {% endfor %} 33 |
34 | 35 |
    36 | {% for post in collections.liveColl.getFilteredByTag("dynamic") %} 37 |
  1. {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
  2. 38 | {% endfor %} 39 |
40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WARNING 2 | 3 | This is ~~probably~~ a very bad idea. 4 | If you're using Nunjucks templating, you can do some questionable life choices in your .eleventy.js config file and create dynamic collections using this nonsense: 5 | 6 | ```js 7 | eleventyConfig.addCollection("liveColl", (collectionApi) => collectionApi); 8 | ``` 9 | 10 | Now, in your Nunjucks templates, you can do this: 11 | 12 | ```njk 13 |
    14 | {% for post in collections.liveColl.getFilteredByTag("animal") %} 15 |
  1. {{ post.data.title }} -- tags={{ post.data.tags }}
  2. 16 | {% endfor %} 17 |
18 | ``` 19 | 20 | Similarly, using `getFilteredByTags()` (plural): 21 | ```njk 22 |
    23 | {% for post in collections.liveColl.getFilteredByTags("animal", "mineral") %} 24 |
  1. {{ post.data.title }} -- tags={{ post.data.tags }}
  2. 25 | {% endfor %} 26 |
27 | ``` 28 | 29 | Filter by globs? Why not! 30 | ```njk 31 |
    32 | {% for post in collections.liveColl.getFilteredByGlob("src/posts/*.njk") %} 33 |
  1. {{ post.data.title }} -- tags={{ post.data.tags }}
  2. 34 | {% endfor %} 35 |
36 | ``` 37 | 38 | But wait… it get's worse! What if you set your tags dynamically, via `eleventyComputed`? 39 | 40 | ```njk 41 | --- 42 | title: Seven 43 | eleventyComputed: 44 | tags: 45 | - dynamic 46 | - whimsy 47 | --- 48 | 49 |

{{ title }}

50 | ``` 51 | 52 | Amazingly, that seems to work too! 53 | ```njk 54 |
    55 | {% for post in collections.liveColl.getFilteredByTag("dynamic") %} 56 |
  1. {{ post.data.title }} -- tags={{ post.data.tags }}
  2. 57 | {% endfor %} 58 |
59 | ``` 60 | 61 | Probably a very terrible, no good idea. But it seemed to work based on my [very limited] testing. :shrug: 62 | --------------------------------------------------------------------------------