6

I've got an easy one for you guys.

I want to have a featured content section where the current article is EXCLUDED

So this works using Middleman Blog with delete_if:

<% blog(content).articles.delete_if{|item| item == current_article}.each do |article| %>
  <%= article_content %>
<% end %>

However I'm using Middleman proxies so I don't have access to the current_article method...

I've got an YAML structure that holds the following mock data (amongst others) with the folder setup like: data > site > caseStudy > RANDOM-ID423536.yaml (Generated by a CMS)

enter image description here

Inside each yaml file you'll find stuff like:

:id: 2k1YccJrQsKE2siSO6o6ac
:title: Heyplace

My config.rb looks like this

data.site.caseStudy.each do | id, this |
  proxy "/cases/#{this.slug}/index.html", "/cases/cases-template.html", :locals => { this: this }, :ignore => true
end

My template file holds the following

<%= partial(:'partials/footer', :locals => {:content => data.site.caseStudy, :title => 'See more projects'}) %>

My loop that gets content

<% content.each do |id, this| %>
  <%= partial "partials/tile" %>
<% end %>

So I guess my question is, how can I use delete_if in this instance so that the current article isnt being displayed?

I have tried stuff like, but to no avail:

<% content.delete_if{|e| e.title == content.title}.each do |id, this| %>
  <%= partial "partials/tile" %>
<% end %>

Any help is appreciated! Thank you!

Solved, ended up doing

<% content.reject{ | id, item| item == this }.each do |id, this| %>
<%= partial "partials/tile", :locals => { :this => this, :dir => "/#{this.content_type_id.downcase}/", :secondary => 'View' } %>
<% end %>

Courtesy of @David Litvak

1 Answer 1

4
+50

I'm the maintainer of contentful_middleman. Looks like you could just send the this to the partial as local, then iterate through your content and make sure that you exclude the one that matches the id of this.

2
  • Hi David, thanks for taking your time to help me. Do you know roughly how I would do that with hashes (from contentful)? I'm not super familiar with how to do that. With arrays it's a bit easier
    – umbriel
    Apr 20, 2017 at 20:48
  • The following kind of worked, but now they are getting deleted in the cache. I have to reload the page a couple of times to get the tiles to reappear. Very strange <% content.reject{|key, value| key >= this.id }.sort_by{ |index| rand }.each do |id, this| %> <%= partial "partials/tile" %> <% end %>
    – umbriel
    Apr 20, 2017 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.