Sitemap.where (querying the sitemap)

Can someone please provide an example of using the code below, either in the context of an ERB template tag or a helper?

sitemap.where(:tags.include => "homepage").order_by(:priority).limit(10)

http://middlemanapp.com/advanced/sitemap/

I would like to iterate over pages in my middleman site like this:

<% sitemap.resources.each do |page| %>
     <%= page.url %>
<% end %>

But filtered only to pages containing a certain value in the metadata (type==‘presentation’) and sorted by another value in the metadata (order, which is an integer).

I really appreciate the help. I think the sitemap is part of what makes middleman extremely powerful, but it’s hard to find much documentation around it.

So I did some code searching on github and stumbled across a MM project that does exactly this. (Shout out to mzgajner: http://mzgajner.github.io)

The trick is that you need to add ‘all’ in the chain, which I was missing. Here’s his example, which finds any source files marked as primary_navigation in the metadata and orders them by ‘weight’, creating the main navigation. You can really see the power of this technique in creating navigation, lists of featured content, projects by date, etc.

In layout.erb:

<% sitemap.where(:primary_navigation => true).order_by(:weight).all.each do |resource| %>
    <li<% if resource.url == current_page.url %> class="active"<% end %>>
        <%= link_to resource.metadata[:page]["title"], resource.url %>
    </li>
<% end %>

Then in a page file’s metadata (say blog.html):

---
title: Blog
weight: 1
primary_navigation: true
---
1 Like

For more examples of potential query syntax, check out document_mapper (which the sitemap query functionality is based off of).

1 Like

Thanks @toddmorey, this was super-helpful!