Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying multiple layouts to same collection item in jekyll

Tags:

jekyll

I have a jekyll collection _persons, with multiple peoples' profiles. Now I want to create multiple layouts for each person, e.g., a "Publications", and a "Bio" subpage for the same person.

How can I associate different layouts with the same person object? I'd also like to use sub-urls, such as:

  • \personA\publications\
  • \personA\bio\
like image 590
alexsb Avatar asked Apr 14 '17 04:04

alexsb


1 Answers

Assuming that "bios" are the content type that you will be editing, you might create a symlink in your project root from _bios to _publications:

# Unix/Linux
ln -s _bios _publications

If you use windows, you'll need to check out mklink.

Then set up your config.yml like this:

collections:
  bios:
    output: true
  publications:
    output: true
defaults:
  -
    scope:
      path: "_bios"
    values:
      layout: bio
      permalink: /biographies/:title/
  -
    scope:
      path: "_publications"
    values:
      layout: publication
      permalink: /publications/:title/

Edit markdown files for your people in the _bios directory, and do not specify layout or permalink in their frontmatter.

When your site builds, you'll get permalinks like example.com/publications/personA and example.com/bios/personA. You can loop through site.publications and site.bios as usual.

You'll need to define the bio and publication layouts, and these will have access to any data you define in the frontmatter of your bio collection items.

Credit for this idea: https://github.com/jekyll/jekyll/issues/3041#issuecomment-267730851

It might be more semantic to have a collection _persons as a single point of truth and make two symlinks from this collection.

Good luck!

like image 151
csknk Avatar answered Dec 04 '22 21:12

csknk