1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

middleman-blogのレイアウト設定について

Posted at

middleman-blogの記事一覧でlayoutが有効にならない

middleman-blogでlayoutの指定を行っていたのですが、なぜだか反映されません…。

config.rb
activate :blog do |blog|
  blog.name = 'blog'
  blog.prefix = 'blog'
  blog.layout = 'blog-layout' # ブログ用のカスタムレイアウトを指定。デフォルトはlayout
end

この状態で、 http://localhost:4567/blog/ にアクセスしてみても、blog-layoutが反映されておらず、layoutのままでした。

個別の記事は反映されていることが判明

ここで、2017-01-17-middleman.mdという記事を作ってみます。

blog/2017-01-17-middleman.md
---
title: 初めての記事
---

# ヘッダー1

初めての記事です。

その後、 http://localhost:4567/blog/2017/01/17/middleman.html にアクセスしてみたところ、blog-layoutが反映されていました。
middleman-blogのlayoutでの指定は、個別記事のレイアウト指定であり、一覧記事のレイアウト指定はconfig.rbでは、できないようです。

記事一覧でレイアウトを指定する方法

index.html.erbや、calendar.html.erbや、tag.html.erbのような記事一覧を扱うテンプレート側で、Frontmatterでレイアウトを指定すれば、個別でレイアウトを適用することができます。

index.html.erb
---
pageable: true
per_page: 10
layout: blog-layout
---

省略

blog.layoutが効かない!と思ったら、この辺りを変更してみるとよいかと思います。

一括でレイアウト指定する方法はある

個別記事も記事一覧も同じレイアウトにしたいということであれば、config.rbで設定することができます。

config.rb
page '/blog/*.html', layout: 'blog-layout'

このようにすると、/blog/以下は全てblog-layoutを使うようになるので、普段はこれでもいいかもしれません。
ただし、以下のようなイレギュラーなことをしようとすると、これだと行き詰まりました。

config.rb
page '/blog/*.html', layout: 'blog-layout'
page '/blog/special/*.html', layout: 'special-layout' # 適用されずにblog-layoutになってしまう

こういう場合は、個別のテンプレートファイルのFrontmatterでlayoutを指定したほうがよさそうです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?