├── .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 | - oNe -- tags=animal
3 |
4 | - ThReE -- tags=animal,mineral
5 |
6 |
7 |
8 | - tWo -- tags=["vegetable"]
9 |
10 |
11 |
12 | - ThReE -- tags=["animal","mineral"]
13 |
14 |
15 |
16 | - oNe -- tags=["animal"]
17 |
18 | - tWo -- tags=["vegetable"]
19 |
20 | - ThReE -- tags=["animal","mineral"]
21 |
22 | - Sven -- tags=["dynamic","whimsy"]
23 |
24 |
25 |
26 | - tWo -- tags=["vegetable"] -- author=Jane
27 |
28 |
29 |
30 | - Sven -- tags=["dynamic","whimsy"]
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 | - {{ post.data.title }} -- tags={{ post.data.tags }}
8 | {% endfor %}
9 |
10 |
11 |
12 | {% for post in collections.liveColl.getFilteredByTag("vegetable") %}
13 | - {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
14 | {% endfor %}
15 |
16 |
17 |
18 | {% for post in collections.liveColl.getFilteredByTags("animal", "mineral") %}
19 | - {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
20 | {% endfor %}
21 |
22 |
23 |
24 | {% for post in collections.liveColl.getFilteredByGlob("src/posts/*.njk") %}
25 | - {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
26 | {% endfor %}
27 |
28 |
29 |
30 | {% for post in collections.liveColl.getFilteredByGlob("src/posts/*.njk") | where("data.author", "Jane") %}
31 | - {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }} -- author={{ post.data.author }}
32 | {% endfor %}
33 |
34 |
35 |
36 | {% for post in collections.liveColl.getFilteredByTag("dynamic") %}
37 | - {{ post.data.title }} -- tags={{ post.data.tags | dump | safe }}
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 | - {{ post.data.title }} -- tags={{ post.data.tags }}
16 | {% endfor %}
17 |
18 | ```
19 |
20 | Similarly, using `getFilteredByTags()` (plural):
21 | ```njk
22 |
23 | {% for post in collections.liveColl.getFilteredByTags("animal", "mineral") %}
24 | - {{ post.data.title }} -- tags={{ post.data.tags }}
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 | - {{ post.data.title }} -- tags={{ post.data.tags }}
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 | - {{ post.data.title }} -- tags={{ post.data.tags }}
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 |
--------------------------------------------------------------------------------