Getting a list of categories in Middleman

Let's say I have categories 'a', 'b', ... of some currently unknown number. Without going through and manually saying which categories are there, would it be possible to find each category and basically create an array where each item contains the category name? I know it is possible to find the category of the article page that is currently being visited using current_article.data.category, but I'm not sure about other methods of using this category.

+3


source to share


2 answers


<% pages = sitemap.resources
     categories = Array.new
     for page in pages do
        categories.insert(1, page.data.category)
     end
     categories = categories.uniq
 %>

      



I believe this is one way to do it.

+1


source


Create a file helpers/categories.rb

:

def generate_categories
  sitemap.resources.map { |res| res.data.category }.uniq.sort
end

      

Restart the Middleman dev server.



Then use it like this:

<% categories = generate_categories %>

      

PS I haven't really tried it. My solution is based on the assumption that your solution is valid.

+1


source







All Articles