List all pages with certain YAML tag

Hi all!

Thank you for this brilliant piece of software!

I am about to switch from Jekyll to Middleman and need to list all pages within a directory in a certain order, that contain a special YAML data tag.

<% current_page.siblings.each do |p| %>
  <% if p.data.category contains 'myCategory' order by p.data.title %>
    <p><a href="<%= p.data.url %>"><%= p.data.title %></a></p>
  <% end %>
<% end %>

Is something similar possible?

Thank you very much!

After some more investigation, it seems the multiple-blog functionality is optimal for my current projects.

I’ll wait patiently :wink:

You can use multiblog now if you want. I’m using it by pulling the master branch directly form github. You can see the source for my site here: https://github.com/epochwolf/epochwolf.com (Ignore my fork of the blog extension, the fix just got merged into the main project.)

Hi Reni,

You can add a helper in your config.rb like this:

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 your view:

  <% pages_by_category('myCategory').each do |page| %>
    <p><a href="<%= page.url %>"><%= page.data.title %></a></p>
  <% end %>
2 Likes

Made some edits to that last post.

Working example for view is:

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

How to do comma separated few categories for one page? Like tags in blogs. (I can’t use tags from middleman-blog because my pages have directory structure and tags don’t work for it if I don’t have one root directory for all pages). Thank you for answer!

@Ricky, are you asking how to support multiple categories on each page?

If so, you can change your frontmatter:

---
title: Adventure Time
category:
- tv shows
- pendleton ward
---

You can make a slight change to the code we used earlier to check that the category includes the one we’re looking for:

def pages_by_category(category)  
  sitemap.resources.select do |resource|
    resource.data.category.present? and resource.data.category.include?(category)
  end.sort_by { |resource| resource.data.title }
end
  1. I wanted to ask about different notation array with comma separated elements


    title: Adventure Time
    category: tv shows, pendleton ward

  2. Is middleman frontmatter can understand nested arrays like this:


    title: I like to watch and read
    category:
    tv shows:
    Grey’s Anatomy (Sub category of tv show)
    Supernatural (Sub category of tv show)
    books:
    Dan Brown (Sub category of books)
    Brown - Angels & Demons (Sub category of Den Browb)
    The Da Vinci Code (Sub category of Dan Brown)
    J.K. Rowling (Sub category of books)
    Harry Potter and the Goblet of Fire (Sub category of J.K. Rowling)
    How to Win Friends and Influence People by Dale Carnegie (Sub category of books)

  3. Can you tell any suggestion how to realise search form on static web site?
    With external engine like https://www.google.com/cse/?

  1. Yes, you can do something similar:
---
title: Adventure Time
category: [tv shows, pendleton ward]
---

The frontmatter is any valid YAML. If you want to know what’s possible, you can read the YAML spec.

Alternatively, you could transform this at the Ruby level:

---
category: tv shows, pendleton ward
---
current_page.data.category # => "tv shows, pendleton ward"
current_page.data.category.split(/,\s*/) # => ["tv shows", "pendleton ward"]

I would recommend sticking with valid YAML for simplicity, though.

  1. Yes, although we don’t call them nested arrays, we call them Hashes, as that’s what they’re mapped to in Ruby.

You’d write it a little differently:

category:
  tv shows:
    - Grey's Anatomy
    - Supernatural
  books:
    Dan Brown:
      - Angels and Demons
      - The Da Vinci Code
    J.K. Rowling:
      - Harry Potter and the Goblet of Fire
    Dale Carnegie:
      - How to Win Friends and Influence People

Here’s how this looks in Ruby:

current_page.data.category
# {
#   "category" => {
#     "tv shows" => ["Grey's Anatomy", "Supernatural"],
#     "books" => {
#       "Dan Brown" => ["Angels and Demons", "The Da Vinci Code"],
#       "J.K. Rowling" => ["Harry Potter and the Goblet of Fire"],
#       "Dale Carnegie" => ["How to Win Friends and Influence People"]
#     }
#   }
# }

You can access any key using this notation:

current_page.data.category.books
# {
#   "Dan Brown" => ["Angels and Demons", "The Da Vinci Code"],
#   "J.K. Rowling" => ["Harry Potter and the Goblet of Fire"],
#   "Dale Carnegie" => ["How to Win Friends and Influence People"]
# }

  1. An external search engine would do the trick, yes. There are lots of solutions available. I’m no expert at this, but you might want to check out a service like Websolr or Searchify, or CloudSearch. The advantage is you can submit custom data (like authors or ratings), and customise the weighting that each of those fields has on the results for the user (e.g. a search match on an author is more important than a match on the body of the page).
1 Like