All Collections
Datatrics' key features explained
Advanced
Adding a minimum and maximum to an {{#each items}} loop
Adding a minimum and maximum to an {{#each items}} loop

This article explains how you can set a minimum or maximum to touchpoints that show multiple items.

Christiaan Proper avatar
Written by Christiaan Proper
Updated over a week ago

If you aren't familiar with our dynamic attributes and how you can include them in our touchpoints, please take a look at this article.

So you know how to add an each loop to a touchpoint, but want to give some extra minimum and maximum options, you're at the right place! You know this works:

{{#each items}} itemcount='4'
{{name}}
{{description}}
{{/each}}

A cleaner way of displaying this is the following (especially when using a minimum and maximum), we will use this format in the rest of the article:

{{#each items itemcount='4'}}
{{name}}
{{description}}
{{/each}}

But let's introduce two extra itemcounts, which you can use in your each loops.

Itemcount_max

First we have itemcount_max, which works the same as itemcount, but gives a better explanation of the rule when working with minimums and maximums. If you use this, it indicates the max number of items to show and Datatrics will try to reach this number.

So take this in mind, when you set a max of 6 and there are only 2 items to show, it will also display with only 2 items. A great example is an abandoned cart email, you want a maximum of 6 items in your email, but every lower number of items should also show.

Itemcount_min

However, sometimes you only want to show the touchpoint when a minimum is met. Again, this could come in handy for an abandoned cart email, or for when you want to show up-sell items, but only if there are at least two items which can be displayed for up-selling.

Examples

So how can you use a minimum and maximum in your #each items loop? This is best explained by showing you some examples. Let's dive into them.

Minimum is 1 item, maximum is 10 items

You only need to add a maximum, since it will not show with zero items. This is the same as the itemcount='10' you already know.

{{#each items itemcount_max='10'}}
{{name}}
{{description}}
{{/each}}

Minimum is 2 items, maximum is 6 items

When using a minimum, always add a maximum. If you want, you can set the maximum up to 100, but there aren't many cases you want this to happen.

{{#each items itemcount_min='2' itemcount_max='6'}}
{{name}}
{{description}}
{{/each}}

Only (show when) exactly 5 items

You can also force the #each loop to only show when there are exactly X amount of items. Simply add the same minimum as a maximum to the loop.

{{#each items itemcount_min='5' itemcount_max='5'}}
{{name}}
{{description}}
{{/each}}
Did this answer your question?