-
I'm trying to add categories (named Topics) to my site, to assign a topics to a peace of content I am using Netlify CMS. I created a data file in module.exports = [
"Lifestyle",
"Business",
...
...
]; This file is is then imported in a Netlify CMS Then I've created a collection through my const topics = require("./site/_data/topics");
// Topics collection
config.addCollection("topics", () => {
const list = new Set();
topics.forEach((topic) => {
list.add(topic);
});
return Array.from(list).sort();
}); Now things became a bit blurry in my head, in regards as to how to use these Topics throughout the site as category navigation, as well we how to create a page for each Topic which would then list all of the content under it. So I created a topics folder:
// topics/index.md
---
layout: page
meta_title: topics
meta_description: Browse all topics
hide_from_sitemap: true
robots: noindex,follow
title: topics
permalink: /topics/
---
<ul class="leading-loose">
{% for topic in collections.topics %}
<li><a href="{{ topic | slug | url }}">{{ topic | capitalize }}</a></li>
{% endfor %}
</ul> // topics/topic/index.md
---
layout: topic
eleventyComputed:
title: Category "{{ topic }}"
meta_title: Category {{ topic }}
meta_description: The "{{ topic }}" topic page
--- As well as a ---
layout: page
pagination:
data: collections.products
size: 1
alias: topic
permalink: /topics/{{ topic }}/
hide_from_sitemap: true
---
<ul class="topic-{{ topic }} leading-loose">
{% for post in collections.products %}
<li><a href="{{ post.url }}">{{ post.data.title }}</a></li>
{% endfor %}
</ul>
<p class="text-400">Back to <a href="/topics/">all topics</a></p> Topics, can only be assigned to content in the ---
layout: "listicle"
topic: "Lifestyle"
title: "Cool Mattresses"
--- Now this entire setup <article id="container-center" class="column center">
<header class="page-header">
<h1 class="page-title">Category "[object Object]"</h1>
</header>
<div class="content flow">
<ul class="topic-[object Object] leading-loose">
<li><a href="/mattresses">Cool Mattresses</a></li>
</ul>
<p class="text-400">Back to <a href="/topics/">all topics</a></p>
</div>
</article> I'm sure this is a small issue, most likely due to my limited 11ty knowledge (or JS knowledge for that matter). |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Re: collection... Not sure I'm following it completely. It seems to just be a sorted copy of the global data file. const topics = require("./site/_data/topics");
// Topics collection
config.addCollection("topics", () => {
const list = new Set();
topics.forEach((topic) => {
list.add(topic);
});
return Array.from(list).sort();
}); I believe you can initialize a Set with an array, so this could probably almost be reduced to: const list = new Set(topics);
return Array.from(list).sort(); But then why bother convert from an Array to a Set then back to an Array [which is sorted]? So why not: return topics.sort(); Then we can just question why we need a global data file AND a collection if they're both just an Array, or if we can just use the global data file. If you just wanted to sort it, you can do that from the global data file like: module.exports = [
"Lifestyle",
"Business",
...
...
-];
+].sort(); // topics/topic/index.md
---
layout: topic
eleventyComputed:
title: Category "{{ topic }}"
meta_title: Category {{ topic }}
meta_description: The "{{ topic }}" topic page
--- Not sure where ---
# topic.njk
layout: page
pagination:
data: collections.products
size: 1
alias: topic
permalink: /topics/{{ topic }}/
hide_from_sitemap: true
---
<ul class="topic-{{ topic }} leading-loose">
{% for post in collections.products %}
<li><a href="{{ post.url }}">{{ post.data.title }}</a></li>
{% endfor %}
</ul>
<p class="text-400">Back to <a href="/topics/">all topics</a></p> This is where you're starting to lose me. Now we're looping over the |
Beta Was this translation helpful? Give feedback.
-
@pdehaan apologies about the confusion, the problem is I'm still trying to wrap my head around 11ty and how it even all works, plus my javascript is also not very good. So everything you see there, can without a doubt be improved, or is even totally unnecessary :D What I'm trying to do, essentially, is very easy. (In theory for me) I have an array of topics which is supposed to act as categories site-wide, and only posts in the When you click on a Topic, all of the When you then click on a PS: So I created a collection, because I thought 11ty needs that so that it builds html files for everything. And the global data file, this one I can at least explain: I use this, so that I can import the array into Netlify CMS settings where I use it for a |
Beta Was this translation helpful? Give feedback.
-
Okay! Now we're talking :) I went with your third suggestion, it just made the most sense to me. ---
pagination:
data: collections
filter:
- all
- products
- authors
- articles
- reviews
- pages
size: 1
alias: topic
addAllPagesToCollections: true
layout: page
permalink: "/{{ topic | slug }}/"
---
<ul class="topic-{{ topic }} leading-loose">
{% for post in collections[ topic ] %}
<li><a href="{{ post.url }}">{{ post.data.title }}</a></li>
{% endfor %}
</ul> And on the Netlify CMS side, I have a {
label: "Select a Topic",
name: "tags",
widget: "select",
multiple: false,
hint: "Select Landing Page Topic. Topics act as categories.",
options: topicsList,
} And nothing else was needed, no |
Beta Was this translation helpful? Give feedback.
Re: collection... Not sure I'm following it completely. It seems to just be a sorted copy of the global data file.
I believe you can initialize a Set with an array, so this could probably almost be reduced to:
But then why bother convert from an Array to a Set then back to an Array [which is sorted]?
So why not:
Then we can just question why we need a global dat…