Jekyll <collection>.title: where is it documented?

The Jekyll tutorial ( Collections | Jekyll • Simple, blog-aware, static sites ) has:

collections:
  authors:
    output: true

I discovered that the following code snippet:

    {% for a in site.authors %}
    {{ a.title }}
    {%- endfor -%}

shows that a.title is either Jill or Ted (“J” and “T” are upper cases), which come from the names of the files _authors/jill.md and _authors/ted.md without .md extension and the first letter upper cased. If the title is defined inside of the file, it uses that instead.

My question is: where is this documented, or where is it coded in the module? I cannot find from the doc on collections ( Collections | Jekyll • Simple, blog-aware, static sites ), on variables, etc.

PS: I assume the same feature can be achieved using categories in the front matter of the posts, which can also handle the situation where an article is written by both authors:

categories:
- jill
- ted

Can this be handled by collections? Either collection or category can group a set of posts by a “keyword” and so display them in a separate page, so what are the difference in term of functionalities? Thanks a lot.

1 Like

I believe that is just the page variable title:

which is either the filename without the extension or the title from the frontmatter if set.

Categories and Tags have special meaning when used with posts only - I would avoid using them anywhere else as it can be confusing - though I think they both will work as normal front matter variables - you just won’t get the special things that you get if it is on a post.

1 Like

In the example your shared, it’s not actually a.title but a.name. You will see that jill.md has YAML front matter with some data in it, specifically a name of Jill Smith. That is where the capital “J” Jill is coming from.

At the top of many Jekyll files you will see YAML front matter, like the one in the example:

---
short_name: jill
name: Jill Smith
position: Chief Editor
---
Jill is an avid fruit grower based in the south of France.

All the thing between the set of three dashes are data you can filter on or display on a page.

Here are some videos I created that walk you through how to use data and variables in Jekyll. I suggest you take a look at these and it might help clear things up a little.

Working with Jekyll Data: Part 1 (slides only)

Working with Jekyll Data Part 2: Built-in data

Working with Jekyll Data, Part 3: Custom Data

1 Like

rdyar: Thanks for the answer! When when I insert the code snippet in the main index.html:

---
layout: default
title: Home
---

<h1>{{ "Hello World!" | downcase }}</h1>

{% for a in site.authors %}
{{ a.title }}
{{ page.title }}
{%- endfor -%}

The a.title shows the Jill or Ted (when no title is defined in author page jill.md and ted.md) while page.title shows Home in both loops. So a.title is not the “page title” of the page rendered by the index.html, but the “page title” of the invisible author “subpage”.

Is it the only way to define an element of a collection by creating a .md file in collection directory? In the introduction, it is:

_authors/jill.md
_authors/ted.md

I see two problems in this. One is the repetitiveness (imagine there are ten authors) , and the other is lack of order. I would like to set up a ordered collection in json file authors.json like:

- author: ted
  name: Ted Doe
  ...
- author: jill
  name: Jill Smith
  ...

Is this possible? If so, how do I tell Jekyll that this json file define my collection, and where is it documented?

PS: I use a.title to get each element of the collection to build a navigation, but I do not have control of my own order the way it is setup in the example (jill.md and ted.md).

Bill: both a.title and a.name exist. a.title is Jill or Ted, the name of the .md file leaving out the extension with first letter upper cased (when title is not defined in the front matter), and a.name is defined by the front matter as you said correctly (Jill Smith and Ted Doe in the example).

Maybe I am misunderstanding your question then. Could you clarify what you are trying to understand with a little more detail?

I think when I say it is the same as page title I mean conceptually - I think it is doing the same thing as page title but as you say if you embed the collection loop in another page then page.title is THAT PAGE’s title, not the title for the collection item. But within the collection loop, title is acting the same way just on the collection item. So if title is in the collection front matter you will get that, otherwise you get the filename without the extension and maybe capitalized?

A collection is really like a post - to be more precise, a post is a form of a collection but with extra stuff added (cats and tags) and extra stuff required (file name convention).

A collection item would normally be something with a fair amount of data - like a post has, possibly using markdown. If all you need is a couple things per data item then you would be better off (?) using a data file like a json file or csv. Another example of a good collection item would be a product - with a summary and description and lots of front matter variables. The authors example is also good and would allow each author to have their own page.

A benefit of a collection over a data file is each collection item can be output as a file/page - so each product (or author) could get its own page. You could have a list of products/authors in a data file but you would only be able to output them in a page by looping over them in that page.

A collection item can have any number of front matter variables, the name of the file or title is just one of them.

data files and collections are 2 different things - your use case would determine which is best. Sometimes they could be used together but probably not normally. Your json example would be fine as a data file, no collection needed.

Bill: Here is the clarification for my question.

In the Jekyll’s tutorial on collections ( Collections | Jekyll • Simple, blog-aware, static sites ), we have an authors collection:

collections:
  authors:

and the members of the collection are defined in:

_authors/xted.md (I renamed ted.md to xted.md)
_authors/jill.md

I put the following snippet of code in index.html:

    {% for a in site.authors %}
    {{ a.title }}
    {%- endfor -%}

and discovered that a.title is either Jill or Xted, which is the basename of the .md file that define the authors with the first letter capitalized (title is not defined in the front matter in the example). My question is where this empirically discovered fact is documented.

This is different from a page property in that a page does not have title property if it is not defined in front matter while a member of a collection does; and a a member of a collection does not have a name property if it is not defined in front matter while a page does which is the full name of file that renders the page (for example, index.html).

rdyar: the behavior of the title and name attributes are different for a member of a collection and a page which is summarized in the following table - suppose neither of them are defined in front matter.

member of collection page
title basename title cased (“Jill”) NONE
name NONE fullname without change (“index.html”)

From this I would conclude that a member of a collection and a page does not share similarities. I loop through a connection and use title to find its members, but I can use other method if there is a better way. Thanks.

Hello @puravidajekyll
Interesting topic of discussion here.

I’d like to chime in with the following:

  • Members of a collection are either documents or static files based on whether they have front matter at the beginning of file content.
  • Collection named “posts” is a special entity for Jekyll. It is hard-coded, or baked into Jekyll, and therefore always present. Its members are technically documents but colloquially called posts. For backwards-compatibility, it ended up being significantly inconsistent with user-defined collections. (Details).
  • Documents are inherently different from Pages (members of {{ site.pages }}) with very little parity between them.

Coming to the main topic of this thread: <collection>.title
Technically, you’re referring to <collection>.<item>.title. and it is equivalent to document.data["title"].

Depending on the context of usage, it can also be equivalent to {{ page.title }}.
In Liquid templates, {{ page }} always refers to the current view being rendered irrespective of whether the view is a member of site.pages or site.<collection>.
Therefore, when rendering, /authors/jack.html, (dedicated page for author Jack),
{{ page.title }} will output whatever is stored as data["title"] for the document _authors/jack.md.

(Yes, the basename of the document is the default title. Very much like how a “post” _posts/2021-10-20-hello-world.md would have a default title of Hello World. It however, isn’t explicitly documented anywhere.)

1 Like

When jill.md and ted.md are converted to html files, <title>...</title> was added in the header as meta data. However not all meta data are exposed, and what are exposed may not be in the meta data.

What metadata are you referring to? in general jekyll doesn’t do anything with the metadata - that is up to you to do in the layout or there is an seo plugin I think a lot of people use.

Jekyll does very little if anything by default, but you can do a lot on your own via includes or layouts.

By “metadata”, I mean the following:

  <head>
    <meta charset="utf-8">
    <title>Ted</title>

which is ted.html converted from ted.md. There is no “metadata” in index.html but yet page.name is exposed. So there is no reliable relationship between the “metadata” and Jekyll exposed variables, just some hint.

It comes from the page’s layout. Refer: Layouts | Jekyll • Simple, blog-aware, static sites