title: Generating dynamic strings in jinja2
body: <p>In jinja2, I want to generate strings that enumerate things, different fruits in this example. I have the following template file. Note the line with "Fruit_str not set" is there fore debugging purposes.</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      {% set fruit_str = "Fruit_str not set" %}
      {% for fruit in fruits %}
          {% if loop.first %}
              {% set fruit_str = fruit %}
          {% elif loop.last %}
              {% set fruit_str = fruit_str + " and " + fruit + "." %}
          {% else %}
              {% set fruit_str = fruit_str + ", " + fruit %}
          {% endif %}
      {% endfor %}
      You have {{fruit_str}}
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>which gets this JSON as input</p>
  
  <pre><code>{ "fruits" : ["apples", "oranges", "bananas", "pears"] }
  </code></pre>
  
  <p>The expected output would be</p>
  
  <pre><code> You have apples, oranges, bananas and pears.
  </code></pre>
  
  <p>However, the actual output is </p>
  
  <pre><code>You have Fruit_str not set
  </code></pre>
  
  <p>This indicates that the loop isn't run at all. ´I have read the jinja2 documentation on loops (<a href="http://jinja.pocoo.org/docs/2.10/templates/" rel="nofollow noreferrer">http://jinja.pocoo.org/docs/2.10/templates/</a>), but I can't find what is wrong in this example. What is wrong here?</p>