Add multiple categories for Custom blog collection

I am in the middle of building a site (http://bensonshaji.com) using middleman. I was using Nanoc all these years to accomplish my needs, which is a pretty good tool and later I came to know about middleman which is much more convinient and easy to use (As far I tested). Thank you guys for building such a wonderful app.

Now let me get to the point.

Here is the front matter


title: example
category: cat1 cat2 cat3

Currently I am able to generate custom collections using the front matter “category” in the post. But the output file is not as what I expected. The above front matter generates a file like “cat-1-cat-2-cat-3.html”. Is it possible to get individual files generated ?

Thank you for your time and help. :smile:

1 Like

I think you should separate categories with comma. Look at how tags work. I presume you don’t want to use tags for some reason and you’re planning to use the custom collections functionality.
The other way is to parse the category with the space separator, using Ruby’s split method and generate URLs accordingly.

I made changes as you mentioned, but still the files are getting generated as cat-1-cat-2-cat-3.html

Here is the config for custom collection :

blog.custom_collections = {
        categories: {
            link: '/category/{categories}/index.html',
            template: 'category.html'
        }
    }

Here is a simple template code

<ul>
  <% page_articles.each_with_index do |article, i| %>
    <li><%= link_to article.title, article %> <span><%= article.date.strftime('%b %e') %></span></li>
  <% end %>
</ul>

{categories} parameter will obviously get “cat1 cat2 cat3” as one string, that’s why it will generate one page named “cat-1-cat-2-cat-3.html”. So far everything is as expected. I think, what you are trying to achieve is to replicate the tags functionality. Tags work exactly as what (presumably) you are trying to do, except you must separate them with commas, not spaces. Is there any reason not to use the build-in tags methods?

I just wanted to retain the front matter of all all my posts (existing) without making any changes to the posts. I have tested by separated them adding commas between them but still it generated the file in the same format as I mentioned in my previous comment.

Front matter :

---
categories: inspiration, quotes 
---

Yes, because there is no built-in mechanism to generate custom collections with multiple items in one line. It would work just like tags. The simplest solution for you is to find-replace categories: to tags: in your posts and replace spaces with commas (using grep). Otherwise you’ll have to do little programming. I’m going to do exactly this in the near future for my wife’s blog, but I don’t have it yet. Maybe on Monday :smile:

1 Like

Thanks Komor, I don’t have any choice other than using default tags, I will go ahead with this for now. Thanks for your time. :smile:

I guess a simple solution would be to split by ‘,’ for now. My blog also relies on tags and categories. I’m sure it’s a little redundant, but this blog is being converted from an old codebase (refinery-blog) and I want to leave the data structure intact. It’d be nice in that in the future we could have a generalized to define collections in the frontmatter (like middleman-blog does with tags). I might look into implementing it if it’s not already.

Using categories and tags is not reduntant in many applications. I’m using it in food blog, think of it like 1st and 2nd tier of structure. You have categories like: breakfast, dinner, supper, cake, pizza, pasta, … and tags (the 2nd tier) would be like: tomatoes, potatoes, olive oil, nuddles, cocoa milk, …

I’m still planning to implement categories with multiple entries (so one post can have multiple categories), but for now I’m using the standard functionality (custom collection). Explanation above show why it is useful - some recipes (like pizza or pasta) should be listed as dinner and supper too. Tags have different usage in my blog – tracking ingredients.