Accessing resources in a specified path using the Sitemap

Hi, I’d like to automatically generate gallery pages for my website. I created a galleries.yml file in the data directory which is structured as follows:

galleries:
  - name: g1
    date: 2014-02-01
    location: "images/2014-02-01-g1/"
  - name: g2
    date: 2014-04-20
    location: "images/2014-04-20-g2/"

What I’m trying to do is to create a set of dynamic pages from this data. In config.rb I added the following code in order to generate said pages:

ready do
  data.galleries.galleries.each do |gallery|
    proxy "galleries/#{gallery.name}.html", "gallery.html", :locals => { :gallery => gallery }, :ignore => true
  end
end

Now, from the gallery.html.erb template I’d like to access all the resources found in the specified location of a given gallery entry. Something like this:

<% sitemap.find_resources_by_path("#{gallery.location}").each do |image| %>
	<p><%= image.url %></p>
<% end %>

The method find_resources_by_path doesn’t exist but this is the functionality I need: a way to get all the resources in a specified path. Is that possible?

Thank you very much.

Try just filtering the big list:

sitemap.resources.select{ |r| r.path.start_with?(gallery.location) }.each do |image|

(You may need to adjust your location fields to include a starting slash.)

I’d love to use the .where query syntax for this, but resources don’t have path-parts as properties.

1 Like

Thank you! My temporary solution was to perform a glob on the gallery directory in config.rb using the Dir class and then passing the array of filenames to the proxy function, but I like your solution more.