Eleventy: How to get collections by tag with hyphen or space?

Created on 16 Jun 2019  路  7Comments  路  Source: 11ty/eleventy

If I have tag with space to hyphen is there a way to filter collection by that tag?

tags:
  - space tag
  - hyphen-tag

When I try

collections.hyphen-tag it return nothing and I don't know is there a way to filter by space tag

education

Most helpful comment

Have you tried collections['hyphen-tag']?

All 7 comments

Have you tried collections['hyphen-tag']?

That's work, thanks.

Have you tried collections['hyphen-tag']?

yes! This saved me a lot of time...

if you use

tags:
  - "space tag"
  - hyphen-tag

it _might_ work?

Confirmed that this worked:

title: tag test
tags:
  - with-hyphens
  - with spaces
  - "quoted spaces"
---

<p>tags: <pre>{{ tags | inspect }}</pre></p>
<p>collections: <pre>{{ collections | keys | inspect }}</pre></p>
<p>with-hyphens: <pre>{{ collections["with-hyphens"] | inspect }}</pre></p>
<p>with spaces: <pre>{{ collections["with spaces"] | inspect }}</pre></p>
<p><q>quoted spaces</q>: <pre>{{ collections["quoted spaces"] | inspect }}</pre></p>

Although I think the one caveat I've found is that if you're trying to refer to a collection for pagination, the bracket syntax doesn't really work, and I think you have to wrap the collection name in quotes:

---
title: Tag Archive
pagination:
  data: "collections.with spaces"
  size: 1
  alias: spaces
permalink: "tag/{{ spaces.fileSlug | slug }}/"
---

What's this?

@pdehaan it uses lodash.get to resolve that path, if that helps. Does that not support bracket notation?

Do you have to wrap in quotes? I thought YAML assumed a string there if the value starts with a letter and doesn't have a colon at the end

@pdehaan it uses lodash.get to resolve that path, if that helps. Does that not support bracket notation?

Do you have to wrap in quotes? I thought YAML assumed a string there if the value starts with a letter and doesn't have a colon at the end

I had to use collections['tag here'] syntax. Not quotation marks. Easier than search/replace for all tags in all posts...Also solved a problem I was having with breadcrumbs using tags. If the original tag is a state, for example, it's more readable and easier to use New Mexico and then the breadcrumb will be properly capitalized and spaced automatically. If the tag was newmexico then you have to do all sorts of trickery to make the breadcrumb look right. Same for menu entries. OK this is way too long for GH lol

Was this page helpful?
0 / 5 - 0 ratings