middleman / middleman-blog

Blog Engine Extension for Middleman
https://middlemanapp.com
MIT License
326 stars 179 forks source link

How do I display two different categories? #285

Closed NetOpWibby closed 8 years ago

NetOpWibby commented 8 years ago

I've been wracking my brain trying to figure this out and am getting no closer to solutions (I'm also new to Middleman, but all my other pages are working fine).

I basically have a blog that I want to split into two categories: Thoughts and Projects. Blog post tagged with "thoughts" and "projects" would appear in their respective categories. My problem is, I don't where my template folders for those two categories should go, and how to call upon those two pages with pagination.

Can someone point me in the right direction?

KitaitiMakoto commented 8 years ago

Hi, @NetOperatorWibby

What you should do is to set blog.tag_template property in your config.rb. Documentation on middleman-blog details tag pages: https://middlemanapp.com/basics/blogging/#tags

FYI, my blog configuration might also help you:

NetOpWibby commented 8 years ago

Hi @KitaitiMakoto, thanks for your reply. For some reason, I am unable to achieve the same results. Also, I wanted to be able to visit /thoughts and see all of my posts tagged with thoughts. The same for projects. On your rendered site, going to /tags gives me a 404.

I'll keep trying to find solutions.

EDIT:

Roughly 30 minutes later, I figured it out! In my config.rb, I create a helper, like so:

helpers do
  def pages_by_category(category)
    sitemap.resources.select do |resource|
      resource.data.category == category
    end.sort_by { |resource| resource.data.title }
  end
end

Then, in my projects and thoughts folders, I create a file (or "view") called index.html.erb. In it, I have:

---
layout: default
---

<% pages_by_category("categoryName").each do |p| %>
  <p><%= link_to p.data.title, p.url %></p>
<% end %>

Where categoryName is projects or thoughts, respectively.

I still have to figure out pagination, but I got what I wanted! When I visit /thoughts/, I see all my posts tagged with "thoughts", and the same thing happens for "projects". I'm almost done with my Jekyll port to Middleman!

EDIT 2:

Forgot to show something else in my config.rb (for those finding this in the future):

activate :blog do |blog|
  blog.permalink = "{category}/{title}.html"
end
tdreyno commented 8 years ago

There are tests in the repo showing pagination: https://github.com/middleman/middleman/blob/master/middleman-core/features/paginate.feature is a generic fashion (outside of blog)

NetOpWibby commented 8 years ago

@tdreyno Thanks man, I'll see if I can gleam some ideas from those tests. Would you happen to know how I could get snippets of my posts via my helper? I have no idea how to do that.

tdreyno commented 8 years ago

Snippets? If the resources are blog articles, then they will have a summary method.

So, p.summary in your above example.