Listing Partials to loop through

Is there a way to list all partials so that I can loop through them? I’m not too familiar with using ruby documentation to achieve this.

Basically I have partials in folders, say /source/“type of partial”/_variations.erb

the type is basically the path of the partial, then I want to take all the variations and call them in a template file. So in the end I would have a built html file with all of one type of partial, one after the other.

I would just use the sitemap but partials don’t show up in the sitemap. It would be cool to be able to also pull all the variables or other things.

Thanks!

You don’t need to use Middleman API for this; you can just read the directory to get the list of files inside using Standard Ruby API. For example:

Dir.foreach(config[:source]) do |file|
  next unless File.exist?(File.join(config[:source], file, '_variations.erb'))
  # do something here...
end

Hope that helps.

The problem is mainly syntax. I could use that, but in an .erb file I would use

<% Dir.foreach(config[:source]) do |file|
      next unless File.exist?(File.join(config[:source], file, '_variations.erb'))
     <% partial file %>
end %>

but that throws errors, doing the partial without the % sign throws errors. I just don’t know the proper way to go about doing the syntax correctly. Maybe in the config file? how would I go about doing that?

Try this:

<% Dir.foreach(config[:source]) do |file|
    next unless File.exist?(File.join(config[:source], file, '_variations.erb')) %>
  <%= partial File.join(file, 'variations') %>
<% end %>

so this works but I can’t seem to find the best way to take the _ off the front and the .erb off the ends, also this lists ‘.’ and ‘…’ which can’t be used for partials. Partials have to be just a path /source/variations would then call _variations.erb Otherwise it would seem to work well!

edit: Figured it out, now to try to make it a helper in the config file…

Thanks for your help!

<%= partial “modules/#{lang}/modifiedhead” %>

<% Dir.foreach(config[:source] + “your path here”) do |file|
next if file == ‘.’ or file == ‘…’ %>
<%= partial “your path here” + File.basename(File.join(config[:source],“your path here”, file.gsub(/[_]/, ‘’)), “.erb”) %>
<% end %>

for anyone wanting to see what I did, it’s a bit messy and if you have any ideas on how to clean it up that would be awesome . I want to be able to make a helper class now but again not sure about using those but it would definitely clean up my pages a bit!

Thanks to vvasabi for the code!