Skip to content

Instantly share code, notes, and snippets.

@joelhans
Last active December 12, 2015 08:39
Show Gist options
  • Save joelhans/4745894 to your computer and use it in GitHub Desktop.
Save joelhans/4745894 to your computer and use it in GitHub Desktop.
Looping over types of pages in Middleman with sitemap + regex.

Middleman doesn't yet support multiple blogs, so on my portfolio, I needed a way to loop through my "work" items in a reliable way. I didn't want to have to mess with a bunch of code just when I was adding a new project to the mix. Thanks to Middleman's sitemap feature, and a little Googling, I figured out a way. In Haml:

- sitemap.resources.each do |page|
  - if page.url =~ /^(\/work\/)/
    =link_to page.data.title, page.url

Basically, this loops through every page in the sitemap and filters out everything that doesn't contain the string in the regex. The string I'm looking for here is "/work/". Edit accordingly, and remember that forward slashes (these guys: /) must be escaped with a backslash (one of these: ).

And in ERB:

<% sitemap.resources.each do |page| %>
  <% if page.url =~ /^(\/work\/)/ %>
     <%= link_to page.data.title, page.url %>
  <% end %>
<% end %>

I have a fairly comprehensive Middleman blog tutorial over this way. Check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment