<p>I think it'll be easier and more efficient to achieve that if you use the loop functionality instead of querying the actual tags. I think this code does what you want:</p>

<pre><code>{% set topics = '' %}

{% for topic in entry.topics %}
    {% set topics = topics ~ topic ~ (loop.index &lt; loop.length-1 ? ', ' : loop.index &lt; loop.length ? ' and ') %}
{% endfor %}

&lt;p&gt;This is a sentence with {{ topics }}.&lt;/p&gt;
</code></pre>

CC BY-SA 3.0
2014-09-14T18:25:54.370Z
You can also use `loop.last` to trim that logic down even further.
Lindsey D
CC BY-SA 3.0
2014-09-14T13:09:59.990Z
2014-09-14T13:09:59.990Z
André Elvan
<p>I try to build a complete sentence based on tags as follows:</p>

<p><strong>This is a sentence with tag1, tag2 and tag3.</strong></p>

<p>My code is:</p>

<pre><code>{% set topics = entry.topics.first %}

{% for topic in entry.topics.offset(1) %}
    {% if topic != entry.topics.last %}
       {% set topics = topics ~ ', ' ~ topic %}
    {% endif %}
{% endfor %}

&lt;p&gt;This is a sentence with {{ topics }} and {{ entry.topics.last }}.&lt;/p&gt;
</code></pre>

<p>My problem is that the if clause does not work. The results is always:</p>

<p><strong>This is a sentence with tag1, tag2, tag3 and tag3.</strong></p>

<tags><array>
How to build a sentence based on tags
# How to build a sentence based on tags

(Asked by: username_0 on 2014-09-14)

I try to build a complete sentence based on tags as follows:
This is a sentence with tag1, tag2 and tag3.
My code is:
{% set topics = entry.topics.first %}

{% for topic in entry.topics.offset(1) %}
    {% if topic != entry.topics.last %}
       {% set topics = topics ~ ', ' ~ topic %}
    {% endif %}
{% endfor %}

This is a sentence with {{ topics }} and {{ entry.topics.last }}.

My problem is that the if clause does not work. The results is always:
This is a sentence with tag1, tag2, tag3 and tag3.

**Tags:** tags, array

---

## 1 answers

### Answer by username_1 on 2014-09-14. Score: 5 | Accepted answer

I think it'll be easier and more efficient to achieve that if you use the loop functionality instead of querying the actual tags. I think this code does what you want:
{% set topics = '' %}

{% for topic in entry.topics %}
    {% set topics = topics ~ topic ~ (loop.index < loop.length-1 ? ', ' : loop.index < loop.length ? ' and ') %}
{% endfor %}

This is a sentence with {{ topics }}.

> **Comments:**
> **username_2**: You can also use `loop.last` to trim that logic down even further.  

---

