If statement, based on number of items in frontmatter

Hi everyone,

I’m building an agency website and I’m using Middleman Blog for case studies. Every article has the same items in frontmatter, such as:

project: project title goes here
tags: tag 1, tag 2, tag 3
client_logos:
    - first-client-logo.png
    - second-client-logo.png
date: date goes here

I’ve added the following (I’m using Bootstrap):

<h1>CLIENTS</h1>
<div class="row">
  <% current_article.data.client_logos.each do |logo| %>
    <div class="col-xs-6">
      <img class="img-responsive" src="<%= logo %>" />
    </div>
  <% end %>
</div>  

This all works perfectly fine. What I’d like to achieve, is a condition based on the amount of client logos. To be precise: if there is just one client logo, I’d like the header to say “client” instead of “clients”.

I’m stuck - don’t know how I can do a “if client_logos > 1” statement.

Thanks!

You could try an “each_with_index” loop instead.

You’re looking for the pluralize helper.

<h1><%= pluralize(current_article.data.client_logos.count, "CLIENT") %></h1>
1 Like

Alright, that works. Thanks! However… the number is also shown (for example “1 client” or “4 clients”. Any help on how I can make it so the number itself is not rendered?

I forgot this one existed, but there is also a similar String#pluralize method:

<h1><%= "Client".pluralize(current_article.data.client_logos.count) %></h1>

This is actually part of active_support/core_ext/string/inflections and it looks like the Padrino library also has a version. I believe it is automatically available to Middleman apps though.

Returns an error. Guess Padrino isn’t standard available. Thanks though :slight_smile:

What is the error, and which version of Middleman are you running? Try adding the following to the top of your config.rb:

require "active_support/core_ext/string/inflections"

I haven’t upgraded to 4 yet, and I know there were some changes in what was included by default.

Middleman 3.4.1. Keep getting the same error (wrong number of arguments (given 1, expected 0))…