pourmesomecode
pourmesomecode

Reputation: 4368

Jekyll multiple pages using the same template

I'm aware you can use _layouts and in your pages do something like

---
layout: some_layout
title: Home
---

So say I have 20 pages. All using the same template but slightly different content and stuff inside.

Instead of creating 20 different pages.html files with different names and different permalinks.

Is there a way to create 1 page.html and based on the permalink change what's inside the {{ content }}?

Upvotes: 3

Views: 1464

Answers (3)

Alexander Oh
Alexander Oh

Reputation: 25651

I have not tried this yet, but there is a closed github issue: https://github.com/jekyll/jekyll/issues/16

There are suggestions to write/use a jekyll plugin like the ones below. These are links from the issue and their state is unknown to me.

how to write a plugin: https://jekyllrb.com/docs/plugins/

Upvotes: 0

ashmaroli
ashmaroli

Reputation: 5434

You could organize the 20 pages under a collection and assign defaults on the collection. For example, say your collection is labelled docs, then all those 20 pages need to be placed inside a directory named _docs, at the root of your source directory. Following that configure your collection to use the layout some_layout for its documents.

# _config.yml

# enable rendering on your collection(s)
collections:
  docs:
    output: true
  another_collection:
    output: true

# set defaults on your collection. (note the indentation..)
defaults:
  -
    scope:
      type: docs
      path: _docs
    values:
      layout: some_layout # The layout for this collections' files
  -
    scope:
      type: another_collection
      [...]

Upvotes: 0

Mr. Hugo
Mr. Hugo

Reputation: 12600

Just create your-slug.md files. Let them all use the same layout, like this:

---
layout: some_layout
title: Your title
---

In the layout file (some_layout.html) you put some logic, like this:

{% if page.url contains '/your-slug' %}Put this on the screen.{% endif %}

Upvotes: 1

Related Questions