Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll index.html that uses 3 different _layouts

Tags:

jekyll

I have 3 different _layouts.

  • post-link.html
  • post-article.html
  • post-photo.html

I can show all my post on the index.html but they all have the same layout. Can I somehow show multiple layouts on the same page (index.html)?

like image 333
user1840885 Avatar asked Nov 21 '12 05:11

user1840885


2 Answers

A page can have only one layout, but layouts can be nested.

I have three _layouts:

  • master.html
  • default.html
  • post.html

The master layout has all of the basic structure that any page I want will need. It looks something like this:

<html>
  <head>
    <title>{{ page.title }}</title>
  </head>
  <body>
    {{ content }}
  </body>
</html>

I use the default layout for most pages that are not blog posts. I do make extensive use of a few page variables in pages' YAML front matter. The layout looks something like this:

---
layout: master
---
<h1>
  {{ page.title }}
  {% if page.subtitle %}<small>{{ page.subtitle }}</small>{% endif %}
</h1>
{% if page.description %}<p>{{ page.description }}</p>{% endif %}
{{ content }}

I use the post layout for _posts pages. It looks like this:

---
layout: default
---
<p>Posted {{ page.date }}</p>
<ul>{% for tag in page.tags %}...{% endfor %}</ul>
{{ content }}

Every blog post that I make, I use the post layout, and they inherit from all three layouts.

If you want to have snippets of reusable markup, then I would recommend using _includes.

like image 111
kzh Avatar answered Nov 03 '22 22:11

kzh


A page can only have one layout. What you need is _includes, which you can use wherever a post is to be displayed.

like image 42
Tom Clarkson Avatar answered Nov 03 '22 21:11

Tom Clarkson