1

I have an html template for Flask with Jinja2. The goal is print the "counter" along with name.

However, Jinja2 gives me an error for trying to convert the counter which is an int to a string. How do I get the "counter" + "name" inside of the div to work?

<p>
    {% set counter = 0 -%}
    {% for name in two_word_names %}
        <div class=flash>{{ str(counter) + name }}</div>
    {% counter += 1 -%}
    {% endfor %}
</p>
William Ross
  • 3,568
  • 7
  • 42
  • 73

2 Answers2

7

You can try using the builtin-filter string:

{{ counter|string ~ name }}
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
1

I used loop.index in a separate block because it is int. Then a separate block for the string name.

{% for name in two_word_names %}
    <div class=flash>{{ loop.index }}{{ ". " + name }}</div>
{% endfor %}
William Ross
  • 3,568
  • 7
  • 42
  • 73