<p>The way I would think of it is: I print the category title, then if this is not the last category I print the separator (<code>and</code> or <code>,</code>). I would use the <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">loop variable</a> <code>revindex</code> to distinguish the next-to-last category (which gets the <code>and</code>) from the others (which get the <code>,</code>). <code>loop.revindex</code> is like <code>loop.index</code> except that it counts the number of iterations from the end. So your code starts to look like this:</p>

<pre><code>{% for category in categories %}
  {{category.title}}
  {% if not loop.last %}
    {{ loop.revindex == 2 ? ' and' : ',' }}
  {% endif %}
{% endfor %}
</code></pre>

<p>That code will leave a space before the <code>,</code>, which you can fix by sticking everything on one line:</p>

<pre><code>{% for category in categories %}
  {{category.title}}{% if not loop.last %}{{ loop.revindex == 2 ? ' and' : ',' }}{% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-07-25T16:31:30.073Z
Thanks Marion. I hadn't considered the revindex. This is useful.
Terry Upton
CC BY-SA 3.0
2016-07-25T16:36:14.510Z
I don't think I've every used revindex - but now I know what it's for!
Marion Newlevant
CC BY-SA 3.0
2016-07-25T16:58:25.620Z
First time I've heard of it, too!
Brad Bell
CC BY-SA 3.0
2016-07-25T16:22:49.507Z
2016-07-25T16:22:49.507Z
Marion Newlevant
<p>I am trying to loop through a list of categories to add some text to the page. If it is the last item I want to replace the ',' with the words 'and' but only adda a ',' if there are more than 2 categories.</p>

<pre><code>{% set categories = craft.categories.group('products') %}
{% for category in categories %}
    {{ loop.last ? 'and ' }}{{ category.title }}{{ loop.index &gt; 2 ? ', ' }}
{% endfor %}
</code></pre>

<p>if there is only category it would output:<br>
hello</p>

<p>So if there are two categories it would output:<br>
hello and world </p>

<p>if there are 3 or more it would output:<br>
hello, world and space</p>

<p>My current code looks like this. but isn't quite working correctly.
Anyone able to help? or point in the right direction?</p>

<p>Thanks.</p>

<templating><categories>
Target first and last items and append if greater than 2
# Target first and last items and append if greater than 2

(Asked by: username_0 on 2016-07-25)

I am trying to loop through a list of categories to add some text to the page. If it is the last item I want to replace the ',' with the words 'and' but only adda a ',' if there are more than 2 categories.
{% set categories = craft.categories.group('products') %}
{% for category in categories %}
    {{ loop.last ? 'and ' }}{{ category.title }}{{ loop.index > 2 ? ', ' }}
{% endfor %}

if there is only category it would output:
hello
So if there are two categories it would output:
hello and world 
if there are 3 or more it would output:
hello, world and space
My current code looks like this. but isn't quite working correctly.
Anyone able to help? or point in the right direction?
Thanks.

**Tags:** templating, categories


---

## 1 answers

### Answer by username_1 on 2016-07-25. Score: 2 | Accepted answer

The way I would think of it is: I print the category title, then if this is not the last category I print the separator (and or ,). I would use the loop variable revindex to distinguish the next-to-last category (which gets the and) from the others (which get the ,). loop.revindex is like loop.index except that it counts the number of iterations from the end. So your code starts to look like this:
{% for category in categories %}
  {{category.title}}
  {% if not loop.last %}
    {{ loop.revindex == 2 ? ' and' : ',' }}
  {% endif %}
{% endfor %}

That code will leave a space before the ,, which you can fix by sticking everything on one line:
{% for category in categories %}
  {{category.title}}{% if not loop.last %}{{ loop.revindex == 2 ? ' and' : ',' }}{% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks Marion. I hadn't considered the revindex. This is useful.  
> **username_1**: I don't think I've every used revindex - but now I know what it's for!  
> **username_2**: First time I've heard of it, too!  

---

