11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.2k stars 493 forks source link

How to get collections by tag with hyphen or space? #575

Closed sarunw closed 5 years ago

sarunw commented 5 years ago

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

jevets commented 5 years ago

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

sarunw commented 5 years ago

That's work, thanks.

dixonge commented 4 years ago

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

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

zachleat commented 4 years ago

if you use

tags:
  - "space tag"
  - hyphen-tag

it might work?

pdehaan commented 4 years ago

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?
zachleat commented 4 years ago

@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

dixonge commented 4 years ago

@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