Search code examples
jekyllgithub-pages

In Jekyll, how do I render custom metadata for a collection?


I have the following in my _config.yml file:

collections:
  nr_qa:
    output: true
    permalink: /:collection/:name
    title: 'Node-RED Questions and Answers'
    descriptions: 'Node-RED is a flow-based (visual) programming tool. These pages have some information that may be currently missing from the documentaiton.'
  github_pages:
    title: 'GitHub Pages and Jekyll/Liquid'
    description: 'Hints and tips on using Jekyll for publishing to GitHub Pages.'
    output: true
    permalink: /:collection/:name

and I want to create an automatic index for my collections. So I use code like this:

## {{ site.collections.github_pages.title }}

{{ site.collections.github_pages.description }}

<ul>
{% for item in site.github_pages %}
  <li>
    <a href="{{ item.url }}">{{ item.title | replace:'_',' ' }}</a>
    <p>{% if item.description %}
        {{ item.description }}
    {% else %}
        {{ item.excerpt | strip_html }}
    {% endif %}</p>
  </li>
{% endfor %}
</ul>

And yes, I know I've rather mixed up my markdown and html there. Not relevant to this question.

The problem is that {{ site.collections.github_pages.title }} and {{ site.collections.github_pages.description }} don't render anything even though I think they should.

Can anyone point out my mistake please?


Solution

  • The problem is that title and description should be included in each collection, and not in _config.yml.

    Check out Accessing Collection AttributesPermalink for further details.

    update

    title can be present in each collection metadata in _config.yml. The problem is how you are accessing those variables.

    One approach is to have a specific layout for each collection, then you can access them like:

    {% assign col = site.collections | where: 'label','github_pages' | first%}.
    TITLE: {{ col.title }}.
    
    DESCRIPTION: {{ col.description }}.